Common Python Errors Explained (Beginner Guide)
Python error messages can look confusing at first. This page gives you a simple map of the errors beginners see most often.
You will learn:
- what common error types usually mean
- how to read a traceback
- how to tell syntax, type, value, name, index, and file errors apart
- which specific error page to open next
This is an overview page. It is meant to help you identify the error category quickly, not replace detailed fix guides for each exact error.
Quick way to identify the error
If you are not sure what error your code is raising, use this pattern to print the exact error type and message:
try:
# your code here
pass
except Exception as e:
print(type(e).__name__)
print(e)
Use this to see the exact error type and message before trying to fix it.
What this page is for
This guide helps beginners:
- get a simple overview of common Python errors
- understand the difference between major error categories
- move quickly to the right detailed fix page
- avoid getting stuck in deep debugging too early
If you want a broader introduction to exceptions, see Python errors and exceptions explained.
How to read a Python error
When Python shows an error, do this first:
- Look at the last line first. It shows the error type and message.
- Read the line number in the traceback.
- Check the code on that line and the lines just above it.
- Do not guess from the message alone. Confirm which variable or value caused it.
For example:
numbers = [10, 20, 30]
print(numbers[5])
Output:
Traceback (most recent call last):
File "example.py", line 2, in <module>
print(numbers[5])
IndexError: list index out of range
How to read it:
IndexErroris the error typelist index out of rangeis the messageline 2is where Python failed
If you are new to this process, a full beginner guide to debugging Python code can help.
Syntax errors
Syntax errors happen when Python cannot understand the structure of your code.
Common causes:
- missing
: - missing quote
- wrong indentation
- unmatched
(,[, or{
These errors usually appear before your program fully runs.
Typical examples:
SyntaxErrorIndentationError
Example:
name = "Sam
print(name)
Output:
SyntaxError: unterminated string literal
Another example:
if True
print("Hello")
Output:
SyntaxError: expected ':'
If your problem is about code structure, start with:
Name and variable errors
These happen when Python cannot find a variable, or when a variable is used in the wrong scope.
Common causes:
- misspelled variable names
- using a variable before assigning it
- scope problems inside functions
Typical examples:
NameErrorUnboundLocalError
Example:
age = 25
print(ag)
Output:
NameError: name 'ag' is not defined
This often means:
- the name is misspelled
- the variable was never created
- the variable is not available where you are using it
For a step-by-step fix, see NameError: name is not defined.
Type errors
Type errors happen when you use an operation with the wrong kind of value.
Common causes:
- adding a string and an integer
- calling a list like a function
- using a string where Python expects an integer
Typical example:
TypeError
Example:
print("Age: " + 25)
Output:
TypeError: can only concatenate str (not "int") to str
Another example:
items = [1, 2, 3]
items(0)
Output:
TypeError: 'list' object is not callable
Type errors are often fixed by:
- checking the value with
type() - converting values when needed
- making sure you are using the right object
A common beginner case is covered in TypeError: unsupported operand types for +.
Value errors
Value errors happen when the type is acceptable, but the actual value is not.
Common causes:
- converting non-numeric text with
int() - unpacking the wrong number of values
- invalid input format
Typical example:
ValueError
Example:
text = "hello"
number = int(text)
Output:
ValueError: invalid literal for int() with base 10: 'hello'
Here, text is a string, and int() can accept strings. The problem is that "hello" is not a valid number.
For this specific case, see ValueError: invalid literal for int() with base 10.
If you are unsure whether you have a type problem or a value problem, read TypeError vs ValueError in Python.
Index and key errors
These happen when you try to access data that does not exist.
Common causes:
- using a list index that is too large
- using a tuple index that is too large
- asking for a dictionary key that is missing
Typical examples:
IndexErrorKeyError
List example:
letters = ["a", "b", "c"]
print(letters[3])
Output:
IndexError: list index out of range
Dictionary example:
user = {"name": "Mia"}
print(user["age"])
Output:
KeyError: 'age'
Use this rule:
IndexErroris usually for lists and tuplesKeyErroris usually for dictionaries
Detailed pages:
- IndexError: list index out of range
- KeyError in Python: causes and fixes
- KeyError vs IndexError in Python
Attribute errors
Attribute errors happen when you try to use a method or attribute that an object does not have.
Common causes:
- calling string methods on a list
- using methods on
None - misspelling a method name
Typical example:
AttributeError
Example:
items = ["a", "b", "c"]
items.upper()
Output:
AttributeError: 'list' object has no attribute 'upper'
In this example, .upper() is a string method, not a list method.
This kind of error is explained in AttributeError: object has no attribute.
Import and module errors
These happen when Python cannot find a module, or cannot import something from it.
Common causes:
- a package is not installed
- the module name is wrong
- the file structure is wrong
- a local file has the same name as a real module
Typical examples:
ImportErrorModuleNotFoundError
Example:
import request
Output:
ModuleNotFoundError: No module named 'request'
This often means:
- the module name is misspelled
- the package is not installed
- you meant a different module, such as
requests
For help with this category, see ModuleNotFoundError: No module named x.
File and operating system errors
These happen when your code works with files or folders and the path or permissions are wrong.
Common causes:
- wrong file path
- file does not exist
- no permission
- using a directory path as if it were a file
Typical examples:
FileNotFoundErrorPermissionErrorIsADirectoryErrorNotADirectoryErrorOSError
Example:
with open("missing_file.txt", "r") as file:
data = file.read()
Output:
FileNotFoundError: [Errno 2] No such file or directory: 'missing_file.txt'
This usually means the file path is wrong or the file is not where you think it is.
For the most common file-path problem, see FileNotFoundError: Errno 2 No such file or directory.
Math and runtime errors
These happen while the program is running.
Common causes:
- dividing by zero
- recursion that goes too deep
- invalid math input
- internal runtime problems
Typical examples:
ZeroDivisionErrorRecursionErrorRuntimeErrorOverflowError
Example:
print(10 / 0)
Output:
ZeroDivisionError: division by zero
These errors usually mean your code is syntactically correct, but something went wrong during execution.
A simple debugging checklist
When you hit an error, use this checklist:
- Read the full error name
- Check the exact line number
- Print important variables before the failing line
- Confirm value types with
type() - Reduce the code to a small example
- Open the specific error page for the exact message
Useful debugging commands:
print(variable)
print(type(variable))
print(len(my_list))
print(my_dict.keys())
dir(object_name)
help(function_name)
What these do:
print(variable)shows the current valueprint(type(variable))shows the data typeprint(len(my_list))helps when checking indexesprint(my_dict.keys())shows available dictionary keysdir(object_name)shows available attributes and methodshelp(function_name)shows built-in help
Common causes of Python errors
Many beginner errors come from a small set of problems:
- misspelled variable or function names
- wrong indentation
- using the wrong data type in an operation
- trying to access an item that does not exist
- bad user input
- wrong file path
- module not installed
- using
Nonewhere a real value is expected
If you check these first, you will often find the problem much faster.
FAQ
What is the most common Python error for beginners?
SyntaxError, NameError, TypeError, and IndexError are some of the most common beginner errors.
Should I read the first or last line of the traceback first?
Start with the last line because it shows the actual error type and message.
What is the difference between TypeError and ValueError?
TypeError means the kind of value is wrong. ValueError means the kind is acceptable, but the actual value is not.
Why does Python say a name is not defined?
Usually because the variable or function name is misspelled, not created yet, or not available in that scope.
How do I know which error page to read next?
Match the exact error name and message from your traceback to the closest specific error page.