Skip to main content

Errors you’ll encounter

Here are the most common Python errors and how to fix them. Understanding these will save you hours of debugging.

FileNotFoundError

Happens when a file doesn’t exist:
Fix:

ValueError

Happens when a value is wrong type or format:
Fix:

KeyError

Happens when a dictionary key doesn’t exist:
Fix:

IndexError

Happens when accessing invalid list position:
Fix:

TypeError

Happens when operations use wrong types:
Fix:

AttributeError

Happens when accessing non-existent attributes:
Fix:
Error messages are your friends! They tell you:
  • What went wrong (error type)
  • Where it happened (file and line number)
  • Why it happened (error message)
Always read the full error message before trying to fix it.

Reading error messages

Python error messages show a “traceback”:
Read from bottom to top:
  1. Error type: ZeroDivisionError
  2. Error message: division by zero
  3. Location: script.py, line 5
  4. Code that failed: result = 10 / 0

Common mistake patterns

What’s next?

Now that you understand error handling, let’s learn about classes - Python’s way of organizing code and data together.

Classes

Object-oriented programming