10 Common Python Errors and How to Fix Them
The ten Python errors beginners meet most, each with its usual cause and a short code fix, plus how to read a traceback.
Short answer: The most common Python errors for beginners are SyntaxError, IndentationError, NameError, TypeError, IndexError, KeyError, ValueError, AttributeError, ZeroDivisionError and ModuleNotFoundError. Each one has a clear cause and a quick fix, and Python's error message tells you the line to look at. Here are 10 common Python errors and how to fix them.
How do you read a Python error message?
Read the traceback from the bottom. The last line names the error type and explains it. The lines above show the file and line number where it happened. Start there instead of guessing.
What are the 10 most common Python errors?
In the snippets below, each "wrong" block raises the named error and each "fixed" block works. Examples that reuse a variable from an earlier line, such as items and user, should be run together with that line.
1. SyntaxError
Python could not parse the line. Most often a missing colon, bracket or quote.
# wrong
if x == 1
print("one")
# fixed
if x == 1:
print("one")2. IndentationError
Python needs consistent indentation after :. Use four spaces, and never mix tabs and spaces.
# wrong
def greet():
print("hi")
# fixed
def greet():
print("hi")3. NameError
You used a name that does not exist yet: a typo, a different capitalization, or a variable defined later.
# wrong
print(total)
# fixed
total = 3
print(total)4. TypeError
You combined incompatible types, such as text and a number. Convert with str() or int(), or use an f-string.
# wrong
print("Age: " + 25)
# fixed
print("Age: " + str(25))5. IndexError
You asked for a position beyond the end of a list. Valid positions run from 0 to len(items) - 1.
# wrong
items = [1, 2, 3]
print(items[3])
# fixed
print(items[2]) # or items[-1]6. KeyError
The dictionary has no such key. Check the spelling, or use .get() with a default.
# wrong
user = {"name": "Ana"}
print(user["age"])
# fixed
print(user.get("age", "unknown"))7. ValueError
The right type but a value that cannot be converted, like turning "abc" into a number.
# wrong
int("abc")
# fixed
int("42")8. AttributeError
That type has no such method. Often the variable holds a different type than you think; print type(n).
# wrong
n = 5
n.append(1)
# fixed
n = [5]
n.append(1)9. ZeroDivisionError
You divided by zero. Check the divisor first.
# wrong
print(10 / 0)
# fixed
d = 0
print(10 / d if d else "cannot divide by zero")10. ModuleNotFoundError
Python cannot find the module: a typo in the name, or a package that is not installed in the environment you are running.
# wrong
import requestss
# fixed
import requests # after: pip install requestsHow can you avoid these errors?
Run small pieces of code often, so a new error points to a few new lines.
Use an editor that highlights syntax problems and checks indentation.
Print values and types when something surprises you.
Catch an error only when you know how to handle it, for example
except ValueError:around user input. Do not hide errors with a bareexcept:.
If you are still getting started, read What is Python and how long it takes to learn it.
Common mistakes when debugging
Reading only the first line of the traceback instead of the last.
Changing several things at once, so you cannot tell which change fixed it.
Copying a fix without understanding why it works.
Key Takeaway
An error message is a hint, not a failure. Read the last line, find the line number, and match the error type to its usual cause.
Ready to start? Practise reading and fixing errors while you build real programs, from the first line of Python. Begin with the free course Python for AI: From Zero to Your First AI App.
Related articles
More in Python
Jupyter vs VS Code vs Google Colab
Where should you write your first Python code? Compare Jupyter Notebook, VS Code and Google Colab, with a simple rule for choosing.
4 min readNov 15, 2026Your First Python Project: Build a Calculator
Build a command-line calculator in about 30 lines of Python, learning functions, loops, conditions and error handling along the way.
3 min readNov 12, 2026NumPy vs Pandas: What's the Difference?
NumPy handles fast numerical arrays and Pandas handles labelled tables built on top of it. See the differences, small code examples and which to learn first.
4 min readNov 6, 2026Python vs C++ for Beginners: Which Should You Learn First?
Python and C++ differ in how they run, how they handle types and memory, and what they are used for. Here is a fair comparison and a guide to choosing by goal.
4 min readNov 3, 2026Python vs JavaScript: Which Should You Learn First?
Python or JavaScript as a first language? Compare purpose, syntax, ecosystems and learning curve, then pick by your goal.
3 min readNov 1, 2026What Is Python and What Can You Build With It?
Python is a readable, general-purpose language used for AI, data analysis, automation and web backends. Here is what it is and what you can build.
4 min readOct 29, 2026
Comments
Sign in to join the discussion.

