DEV Community

Monika Pareek
Monika Pareek

Posted on

5 Common Python Errors and How to Fix Them (With Examples)

5 Common Python Errors and How to Fix Them (With Examples)

Python is one of the easiest programming languages to get started with. But even a small mistake can produce an error that leaves you wondering what went wrong.

If you're learning Python or building projects, understanding common errors can save you a lot of debugging time.

Let's look at five common Python errors, why they happen, and how to fix them.

1. TypeError: Can only concatenate str (not "int") to str

Example:

age = 25
print("My age is " + age)
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

Python cannot directly concatenate a string and an integer using +.

Solution:

Convert the integer into a string:

age = 25
print("My age is " + str(age))
Enter fullscreen mode Exit fullscreen mode

Or use an f-string:

age = 25
print(f"My age is {age}")
Enter fullscreen mode Exit fullscreen mode

2. NameError: name is not defined

Example:

print(username)
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

The variable username hasn't been defined before being used.

Solution:

username = "Alex"
print(username)
Enter fullscreen mode Exit fullscreen mode

Always check variable names and make sure they are defined before use.

3. IndexError: list index out of range

Example:

numbers = [10, 20, 30]
print(numbers[5])
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

The list contains only three elements, with indexes 0, 1, and 2.

Solution:

numbers = [10, 20, 30]
print(numbers[2])
Enter fullscreen mode Exit fullscreen mode

You can also check the list length before accessing an index.

4. SyntaxError: invalid syntax

Example:

if age > 18
    print("Adult")
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

The colon : is missing after the if condition.

Solution:

if age > 18:
    print("Adult")
Enter fullscreen mode Exit fullscreen mode

Python syntax is strict, so check colons, brackets, quotation marks, and indentation.

5. ZeroDivisionError: division by zero

Example:

a = 10
b = 0

print(a / b)
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

Python does not allow division by zero.

Solution:

a = 10
b = 0

if b != 0:
    print(a / b)
else:
    print("Cannot divide by zero")
Enter fullscreen mode Exit fullscreen mode

Bonus: Use AI to Understand Python Errors

Sometimes error messages are long or confusing, especially when you're working with unfamiliar code.

An AI-powered debugging tool can help explain the traceback, identify the likely cause, and suggest possible fixes.

You can try the free XoneAI Python Error Fixer:

https://www.xoneai.in/tools/python-error-fixer

Paste your Python error and get AI-powered assistance without creating an account or logging in.

Final Thoughts

Python errors are part of learning to program. Instead of simply copying a solution, try to understand why the error happened.

The better you understand errors, the better developer you become.

Which Python error do you find most frustrating? Share it in the comments!

Top comments (0)