How to Read Documentation and Error Messages
After completing this topic, you will:
Learn how to read Python error messages (tracebacks) and find the location of the problem, and develop a habit of quickly finding the information you need in the official documentation.
Where to look when an error occurs
Traceback (most recent call last):
File "main.py", line 12, in <module>
result = calculate(data)
File "main.py", line 7, in calculate
return total / count
ZeroDivisionError: division by zeroWhen you're a beginner, red text can be intimidating. However, Python's error messages are very helpful. There's a specific order to reading them:
- Start from the bottom line β
ZeroDivisionError: division by zero. This tells you what went wrong. - The line above β
return total / count. This shows the code where the error occurred. - The line above that β
File "main.py", line 7. This tells you the file name and line number.
Tracebacks are read from bottom to top. The bottom line is the actual error, and the lines above trace the "call stack" β where the function was called from.
5 Common Errors
| Error | Meaning | Common Cause |
|---|---|---|
NameError | Variable does not exist | Typo, using a variable before it's defined |
TypeError | Incorrect type | "hello" + 5, incorrect number of function arguments |
IndexError | Index out of range | lst[10] when the list only has 3 elements |
KeyError | Key not found in dictionary | d["name"] when the key "name" doesn't exist |
AttributeError | Object doesn't have the method/attribute | None.split(), incorrect type |
These five errors account for more than 80% of the errors beginners encounter.
Searching with Error Messages
The fastest way to solve an error:
- Copy the entire last line β
ZeroDivisionError: division by zero - Search for it directly
- Look at solutions from people who have encountered the same error on Stack Overflow or in the official documentation.
The important thing is to exclude your own variable names when searching. my_data is a name you use specifically, so it won't be helpful in the search. Search only for the error type + the error message.
How to Read Official Documentation
The Python official documentation (docs.python.org) can seem difficult to read at first. Don't try to read it all. Find only the parts you need.
# "I want to remove a specific value from a list"# β Search: "python list remove"# β docs.python.org/3/tutorial/datastructures.html
# list.remove(x)# Remove the first item from the list whose value# is equal to x. It raises a ValueError if there# is no such item.The official documentation tells you:
- What it does β Removes the first matching item.
- Things to be aware of β Raises a ValueError if the item is not found.
- Return value β (Here, None β modifies the original list)
You only need to check these three things. It's most effective to read the documentation while testing in your own environment.
Useful Built-in Help
# View documentation directly in the terminalhelp(str.split)
# View the list of methods an object hasdir(str)
# Check the typetype(my_variable)help() can be used in the interactive shell (REPL). You can view the documentation without an internet connection.
dir() is used when you want to know "what does this object have?". The list that comes out will reveal methods like .sort() and .reverse().
Debugging Habits
When there are no errors but the result is unexpected:
# 1. Print intermediate valuesprint(f"data: {data}")print(f"count: {count}")result = total / count
# 2. Check the typeprint(type(result)) # <class 'float'> vs <class 'str'>
# 3. Test in small units# Instead of running the entire function, check each line in the REPLPrinting for debugging is primitive, but it's the most reliable method at the beginner level. Don't guess what's in a variable β print it and see.
Key Takeaways
Error messages are read from bottom to top. Bottom line = what went wrong, top = where it occurred. When searching, copy only the error type + message and exclude your own variable names. Don't read the entire official documentation β check only what it does, things to be aware of, and the return value.