Table of Contents
The Three Main Types of Errors in Python
When you write Python programs, you’ll usually encounter three broad kinds of errors:
- Syntax errors
- Runtime errors (also called exceptions)
- Logic errors
Understanding the differences between them will make it much easier to fix your programs.
1. Syntax Errors
A syntax error means “your code is not valid Python grammar.”
Python cannot even start running the program because it doesn’t understand how it’s written.
Think of this like writing a sentence with completely broken grammar: the reader can’t even guess what you meant.
Common causes of syntax errors
Typical beginner mistakes include:
- Forgetting a colon
:after: if,elif,elsefor,whiledef,class- Missing or extra parentheses, quotes, or brackets
- Using
=instead of==inside conditions - Incorrect indentation in places where Python expects a block
Examples:
# Missing colon
if x > 10
print("Too big")
# Unmatched parenthesis
print("Hello"
# Wrong use of '=' in a condition
if x = 5:
print("x is 5")Python will stop immediately and show a message like:
File "example.py", line 1
if x > 10
^
SyntaxError: invalid syntaxKey points about syntax errors:
- They happen before any code runs.
- The error message usually points to the line (or near the line) where Python got confused.
- Fixing them is often about checking:
- colons
- parentheses
- quotes
- indentation
2. Runtime Errors (Exceptions)
A runtime error (or exception) occurs while your program is running.
The code is syntactically correct, so Python starts to run it, but something goes wrong during execution.
Example:
x = 10 / 0 # Division by zero
print("Done")
This code has no syntax errors, but when Python executes 10 / 0, it crashes with:
ZeroDivisionError: division by zeroCommon runtime errors include:
- ZeroDivisionError – dividing by zero
result = 5 / 0- NameError – using a variable that hasn’t been defined
print(score) # score was never assigned- TypeError – using the wrong type for an operation
result = "5" + 3 # can't add string and int- ValueError – correct type, but invalid value
number = int("hello") # "hello" can't be turned into an int- IndexError – using an index that’s out of range in a list or string
numbers = [10, 20, 30]
print(numbers[5]) # list only has 3 elements- KeyError – using a dictionary key that does not exist
ages = {"Alice": 25}
print(ages["Bob"]) # "Bob" is not a keyKey points about runtime errors:
- The program starts running, but stops at the line where the error happens.
- Python shows:
- the type of error (e.g.,
TypeError) - a message explaining what went wrong
- usually a traceback telling you which line caused it
- These can often be prevented by checking values or using
try/except(covered in another section of this chapter).
3. Logic Errors
A logic error means “your program runs without crashing, but it doesn’t do what you intended.”
Python is perfectly happy with your code – but the idea behind the code is wrong.
Example:
You want to calculate the average of two numbers:
a = 10
b = 20
average = a + b / 2
print(average)This runs and prints:
20.0
But the real average of 10 and 20 is $ (10 + 20) / 2 = 15 $.
The problem is operator precedence: b / 2 happens first, so the expression is actually:
average = a + (b / 2) # 10 + (20 / 2) = 20The correct version:
average = (a + b) / 2Other examples of logic errors:
- Using
<when you meant<= - Using
andwhen you meantorin a condition - Updating the wrong variable inside a loop
- Off-by-one mistakes, like looping 9 times when you intended 10
Key points about logic errors:
- The program doesn’t crash.
- Python usually doesn’t give any error message.
- You discover them by:
- testing your program with known inputs
- checking whether the outputs match your expectations
- adding print statements or using a debugger to inspect values
Comparing the Three Types
Here is a quick comparison:
- Syntax errors
- When: Before the program runs
- Symptom: Program won’t start;
SyntaxError - Cause: Invalid Python structure (missing colons, bad indentation, etc.)
- Fix: Correct the code so it follows Python’s rules
- Runtime errors (exceptions)
- When: During execution
- Symptom: Program starts but then crashes with an error message
- Cause: Something goes wrong with a particular operation (e.g., divide by zero)
- Fix: Handle special cases, validate input, use
try/exceptwhen appropriate - Logic errors
- When: During execution
- Symptom: Program runs to the end, but output is wrong
- Cause: The algorithm or conditions don’t match what you intended
- Fix: Rethink the logic, add tests, inspect intermediate values
Practice: Spot the Error Type
For each of these, decide which kind of error it would cause:
- :::code
if x > 10
print("Big") - :::code
x = "10"
y = 5
print(x * y) - :::code
total = 0
numbers = [1, 2, 3]
for n in numbers:
total = total + 1
print(total) # You wanted the sum of the numbers - :::code
items = [10, 20, 30]
print(items[3])
Answers (don’t peek until you’ve tried):
- Syntax error (missing colon)
- No error in Python: this runs and prints
"1010101010"– if you expected15, it’s a logic error - Logic error (you’re counting items, not summing them)
- Runtime error:
IndexError(index 3 is out of range)
Being able to quickly recognize which type of error you’re dealing with is an important debugging skill.