Kahibaro
Discord Login Register

Types of errors

The Three Main Types of Errors in Python

When you write Python programs, you’ll usually encounter three broad kinds of errors:

  1. Syntax errors
  2. Runtime errors (also called exceptions)
  3. 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:

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 syntax

Key points about syntax errors:

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 zero

Common runtime errors include:

  result = 5 / 0
  print(score)  # score was never assigned
  result = "5" + 3  # can't add string and int
  number = int("hello")  # "hello" can't be turned into an int
  numbers = [10, 20, 30]
  print(numbers[5])  # list only has 3 elements
  ages = {"Alice": 25}
  print(ages["Bob"])  # "Bob" is not a key

Key points about runtime errors:

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) = 20

The correct version:

average = (a + b) / 2

Other examples of logic errors:

Key points about logic errors:

Comparing the Three Types

Here is a quick comparison:

Practice: Spot the Error Type

For each of these, decide which kind of error it would cause:

  1. :::code
    if x > 10
    print("Big")
  2. :::code
    x = "10"
    y = 5
    print(x * y)
  3. :::code
    total = 0
    numbers = [1, 2, 3]
    for n in numbers:
    total = total + 1
    print(total) # You wanted the sum of the numbers
  4. :::code
    items = [10, 20, 30]
    print(items[3])

Answers (don’t peek until you’ve tried):

  1. Syntax error (missing colon)
  2. No error in Python: this runs and prints "1010101010" – if you expected 15, it’s a logic error
  3. Logic error (you’re counting items, not summing them)
  4. 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.

Views: 13

Comments

Please login to add a comment.

Don't have an account? Register now!