Errors in Python – With Input, Output, and
Explanations
This document explains the most common types of errors in Python, along with examples, causes,
and solutions. Understanding these errors helps you debug code more effectively.
1. SyntaxError
A SyntaxError occurs when Python cannot understand the code because it violates the language’s
syntax rules (e.g., missing parentheses, colons, or indentation).
Example Input:
print("hello word"
What happens: The closing parenthesis is missing.
Output:
SyntaxError: unexpected EOF while parsing
Explanation: The interpreter reached the end of the file (EOF) but expected another parenthesis.
Fixed Code:
print("hello word")
2. NameError
A NameError happens when you try to use a variable or function that has not been defined yet.
Example Input:
print(name)
Output:
NameError: name 'name' is not defined
1
Explanation: Python looks for a variable called name but cannot find it in the current scope.
Fixed Code:
name = 'Python'
print(name)
Output:
Python
3. TypeError
A TypeError occurs when an operation or function is used on an object of an inappropriate type.
Example Input:
num = 6
print('test' + num)
Output:
TypeError: can only concatenate str (not "int") to str
Explanation: Strings and integers cannot be concatenated directly.
Solutions: - Use a comma in print() (automatically adds a space) - Convert the integer to a string
Correct Examples:
print('test', num) # test 6
print('test' + str(num)) # test6
4. IndexError
An IndexError happens when trying to access a list element that doesn’t exist.
Example Input:
2
numbers = [1, 2, 3, 4, 5]
print(numbers[5])
Output:
IndexError: list index out of range
Explanation: List indices start from 0, so for 5 elements, valid indices are 0–4. Index 5 is out of range.
Fixed Code:
print(numbers[4]) # Output: 5
5. KeyError
A KeyError occurs when you try to access a dictionary key that doesn’t exist.
Example Input:
data = {
'name': 'Ali',
'age': 24
}
print(data['city'])
Output:
KeyError: 'city'
Explanation: The key 'city' does not exist in the dictionary.
Solution: Use the .get() method to safely access keys and provide a default message if the key is
missing.
Correct Example:
print([Link]('city', 'key not found'))
Output:
key not found
3
6. ValueError
A ValueError occurs when a function receives an argument of the right type but with an invalid
value.
Example Input:
name = int('hello')
Output:
ValueError: invalid literal for int() with base 10: 'hello'
Explanation: You can only convert strings that represent numbers (like '2' or '42') into integers.
Correct Example:
name = int('2')
print(name)
Output:
7. ZeroDivisionError
A ZeroDivisionError occurs when you try to divide a number by zero.
Example Input:
x = 10 / 0
print(x)
Output:
ZeroDivisionError: division by zero
Explanation: Division by zero is undefined in mathematics and not allowed in Python.
Fixed Code:
4
y = 10 / 2
print(y)
Output:
5.0
Summary Table
Error Type Common Cause Example Fix
SyntaxError Missing brackets or colons Add proper syntax
NameError Using undefined variables Define variable first
TypeError Mixing data types Convert types properly
IndexError Accessing invalid index Use valid index range
KeyError Accessing non-existent key Use .get() or check key
ValueError Wrong value type Use valid value for function
ZeroDivisionError Dividing by zero Avoid zero denominator
💡 Tips for Avoiding Errors
1. Use syntax highlighting editors (VS Code, PyCharm, etc.).
2. Test small parts of code frequently.
3. Use try-except blocks to handle predictable errors gracefully.
4. Read error messages carefully — Python tells you the type and location of the error.