Common Python Errors

What are the 10 most common Python errors?

Answer

There are two groups: pre-runtime errors (caught before execution) and runtime errors (raised while the program runs).

Pre-runtime errors:

  • SyntaxError: Invalid Python syntax. Example: if x 5:

  • IndentationError: Incorrect indentation. Example: missing or extra spaces in blocks.

Runtime errors:

  • TypeError: Operation on incompatible types. Example: 1 + "a".

  • ValueError: Right type, wrong value. Example: int("abc").

  • KeyError: Accessing a missing dictionary key. Example: d["missing"].

  • IndexError: Index out of range. Example: lst[10] when list has fewer elements.

  • NameError: Using a variable that is not defined. Example: print(x) without defining x.

  • AttributeError: Accessing an attribute that doesn't exist. Example: "hi".append.

  • ZeroDivisionError: Dividing by zero. Example: 5 / 0.

  • FileNotFoundError: Trying to open a file that does not exist. Example: open("abc.txt").

Back to collection