🐍 10 Most Common Python Errors
And How to Fix Them?
When writing Python code, encountering errors is inevitable — especially for beginners.
Understanding what these errors mean and how to fix them is crucial for effective debugging.
Here are some of the most common Python errors, complete with explanations and example fixes.
1. SyntaxError
🔍What It Means:
This occurs when the Python interpreter cannot understand your code due to a formatting issue.
❌ Example:
#text without parentheses
print("Hello world"
# or text without quotation marks
print(Hello world)✅ Fix:
print("Hello world") #Hello world🔧 Tip: Check for missing colons, parentheses, or quotation marks.
2. NameError
🔍 What It Means:
You’re trying to use a variable or function name that hasn’t been defined yet.
❌ Example:
print(message)✅ Fix:
message = "Hello!"
print(message) #Hello!🔧 Tip: Make sure all variables are defined before use and check for typos in names.
3. TypeError
🔍 What It Means:
You’re trying to perform an operation on a data type that doesn’t support it.
❌ Example:
#int valuable
number = 5
#str valuable
text = "times"
print(number + text)✅ Fix:
#int valuable
number = 5
#str valuable
text = "times"
print(str(number) + " " + text) #5 times🔧 Tip: Check data types using type() and use appropriate type conversion methods like str(), int(), or float().
4. IndexError
🔍 What It Means:
You’re trying to access an index that doesn’t exist in a list or tuple.
❌ Example:
# build colors list
colors = ['red', 'green', 'blue']
print(colors[3]) # 3-index out of the range✅ Fix:
# build colors list
colors = ['red', 'green', 'blue']
print(colors[2]) #blue #Lists are zero-indexed🔧 Tip: Use len(list) to check the valid range of indices.
5. IndentationError
🔍 What It Means:
Python uses indentation (whitespace) to define code blocks. Misalignment will raise this error.
❌ Example:
def greet():
print("Hello")
greet()✅ Fix:
def greet():
print("Hello") # include 4 spaces or a tab
greet() #Hello🔧 Tip: Consistently use 4 spaces or a tab — and don’t mix them.
6. ValueError
🔍 What It Means:
An operation receives an argument of the correct type, but an inappropriate value.
Get Busra Atasoy’s stories in your inbox
Join Medium for free to get updates from this writer.
❌ Example:
age = int("twenty")
print(age)✅ Fix:
age = int("20") # or use try-except for user input
print(age) #20🔧 Tip: Validate input values before type conversion.
7. ZeroDivisionError
🔍 What It Means:
This error occurs when you try to divide a number by zero, which is mathematically undefined.
❌ Example:
total_score = 100
number_of_students = 0
average = total_score / number_of_students
print("Average score:", average)✅ Fix:
total_score = 100
number_of_students = 0
if number_of_students != 0:
average = total_score / number_of_students
print("Average score:", average)
else:
print("Cannot calculate average — no students available.")🔧Tip: Always validate input values before performing division to avoid unexpected crashes. We should always check, especially if there is a possibility of entering 0 in the data coming from the user.
8. AttributeError
🔍 What It Means:
You’re trying to use a method or property that doesn’t exist for a given object.
❌ Example:
user = {"name": "Büşra"}
print(user.upper()) # 'dict' does not have 'upper()'✅ Fix:
user = {"name": "Büşra"}
print(user["name"].upper()) # "BÜŞRA"🔧 Tip: Use dir(object) to check which methods and attributes are available for that object type.
9. KeyError
🔍 What It Means:
You’re trying to access a dictionary key that doesn’t exist.
❌ Example:
person = {
"name": "Ahmet",
"city": "İstanbul"
}
print("Age:", person["age"]) # Key "age" does not exist✅ Fixed Version (Using .get()) :
person = {
"name": "Ahmet",
"city": "İstanbul"
}
print("Age:", person.get("age", "Not provided")) #Age: Not provided✅ Alternative Fix (Using conditional check):
person = {
"name": "Ahmet",
"city": "İstanbul"
}
if "age" in person:
print("Age:", person["age"])
else:
print("Age key not found.") #Age key not found.🔧 Tip: Use .get() method or conditional check to avoid crashes when keys might be missing.
10. ImportError / ModuleNotFoundError
🔍 What It Means:
Python cannot find the module you’re trying to import. This usually happens due to a typo or because the module hasn’t been installed yet.
❌ Example:
import numppy✅ Fix:
First, install the correct module:
pip install numpy # Use this in your terminalIf you’re using Jupyter or Google Colab, use:
!pip install numpyThen, import it correctly:
import numpy🔧 Tip: Always double-check the spelling of the module name. Use pip list to see installed packages, and install missing ones using pip install <module_name>.
📝 Final Thoughts
Errors are not the enemy — they are opportunities to understand how your code works and improve your programming skills.
With practice, you’ll learn to debug quickly and write cleaner, more efficient Python code.
💬 Did you find this guide helpful? Leave a comment or share your own debugging tips below!
The contents of external submissions are not necessarily reflective of the opinions or work of Maven Analytics or any of its team members.
We believe in fostering lifelong learning and our intent is to provide a platform for the data community to share their work and seek feedback from the Maven Analytics data fam.
Submit your own writing here if you’d like to become a contributor.
Happy learning!
-Team Maven









