Skip to content

Module 19 · Debugging, Testing and Best Practices — topic 2 of 15

Programming errors

The three kinds of programming errors — syntax errors, runtime errors and logic errors — how each shows up, how to find it, and examples of each in Python.

Beginner 8 min read·Lesson 280 of 306 in Python Programming

Learning objectives

  • Classify errors as syntax, runtime or logic
  • Know how each kind is detected
  • Use tests to catch logic errors

Definition

Programming errors fall into three groups: syntax errors break the language's grammar and stop the program from running; runtime errors (exceptions) occur while the program runs; logic errors make the program run without crashing but produce wrong results.

Introduction

A missing colon, a division by zero and a wrongly calculated leap year are very different problems. Recognising the kind of error tells you where to look.

Why it matters

Syntax and runtime errors announce themselves; logic errors hide. Knowing the difference makes you test for the dangerous, quiet ones.

Detailed explanation

Syntax errors are found before the program runs: Python refuses to compile the file and points at the problem (expected ':', unclosed bracket, bad indentation).

Runtime errors are exceptions raised during execution: NameError, TypeError, IndexError, ZeroDivisionError and so on. They appear only when the faulty line runs with problematic data.

Logic errors produce wrong output without any message: a wrong formula, a reversed condition, an off-by-one loop.

Editors and linters catch most syntax errors as you type.

Runtime errors are handled with try/except where recovery is possible, or fixed when they indicate a bug.

Logic errors are found by comparing actual results with expected results — by hand checks, assertions and automated tests.

Edge cases (empty input, zero, boundaries such as 1900 for leap years) are where logic errors usually hide.

Warnings (such as SyntaxWarning for invalid escape sequences) are not errors but often point to real mistakes.

Syntax

Text
if x > 1 # syntax error: missing colon
print(10 / 0) # runtime error: ZeroDivisionError
area = w + h # logic error: should be w * h
  • Syntax: won't run.
  • Runtime: crashes while running.
  • Logic: runs, but wrong.

Code examples

A syntax error caught by compile()

Python
try:
compile("for i in range(3)\n print(i)", "<demo>", "exec")
except SyntaxError as e:
print("SyntaxError:", e.msg)
Output
SyntaxError: expected ':'

Runtime errors

Python
for action in [lambda: [1][3], lambda: int("x"), lambda: undefined_name]:
try:
action()
except Exception as e:
print(type(e).__name__)
Output
IndexError
ValueError
NameError

More examples

A logic error found by edge cases

Python
def is_leap_wrong(year):
return year % 4 == 0
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
for y in [2024, 1900, 2000, 2026]:
print(y, is_leap_wrong(y), is_leap(y))
Output
2024 True True
1900 True False
2000 True True
2026 False False

Step-by-step explanation

  • Both versions agree on ordinary years such as 2024 and 2026.
  • Only the century year 1900 exposes the logic error: it is divisible by 4 but not a leap year.
  • Without that edge case, the wrong version would look correct.

Expected output

Running “A logic error found by edge cases” prints exactly:

TextOutput
2024 True True
1900 True False
2000 True True
2026 False False

Practical applications

  • Billing systems with wrong rounding — a logic error that costs money.
  • Crash reports from runtime errors in production.

Best practices

  • Let your editor flag syntax errors early.
  • Read exception messages fully.
  • Test edge cases to catch logic errors.

Common mistakes

Assuming no crash means correct

Avoid

# the program ran without errors, so it works

Prefer

# compare its output with known correct answers

Logic errors don't crash; only checks reveal them.

Important notes

  • Static type checkers find some logic errors (such as passing a string where a number is expected) without running the code.

Key takeaways

  • Syntax errors stop the program from running.
  • Runtime errors are exceptions during execution.
  • Logic errors give wrong results silently.

Try it yourself

  1. 1
    Write one example of each error type and explain how you would detect it.
  2. 2
    Find the logic error in a function that should return the largest of three numbers but fails when two are equal.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • AdvancedA program runs without crashing but reports the wrong total. What kind of error is this?
Practise this topic