Intermediate

Python debugging step by step

Find where expected and actual behavior become different, then fix the smallest cause.

Chapter goal: Find where expected and actual behavior become different, then fix the smallest cause.

Simple explanation

Debugging is detective work. You compare what you expected with what happened, then find the first place where the values became different.

In Python, this chapter is about testing one assumption at a time instead of guessing. Start with the idea above. Then connect each symbol to a value or action in the example.

Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.

Why this topic is important

Developers spend a large part of their work understanding unexpected behavior. A calm process is faster than random edits. In Python, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.

When to use it

Debug when output is wrong, a screen does not update, data is missing, or an error message appears.

Example code

def total_price(prices):
    total = 0
    for price in prices:
        print("adding price:", price)  # debug trace
        total += price
    return total

print("result:", total_price([10, 20, 5]))

Line-by-line explanation

What the output means

adding price: 10
adding price: 20
adding price: 5
result: 35

The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.

Mistake example

def total_price(prices):
    total = None # several lines are changed before the original cause is identified
    for price in prices:
        print("adding price:", price)  # debug trace
        total += price
    return total

print("result:", total_price([10, 20, 5]))

This version intentionally shows how several lines are changed before the original cause is identified. The changed assignment stores a missing value, or a required line is removed, so later code cannot complete its job safely.

Fixed version

def total_price(prices):
    total = 0
    for price in prices:
        print("adding price:", price)  # debug trace
        total += price
    return total

print("result:", total_price([10, 20, 5]))

The corrected version restores the real value or required operation. It fixes the chapter-specific problem: several lines are changed before the original cause is identified.

Common mistakes

Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.

Real use cases

Practice exercise

  1. Introduce one intentional bug, then find it using print or log statements.
  2. Change only one line at a time while re-testing after each change.
  3. Write down the exact moment the expected and actual results diverged.

Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.

Mini quiz

  1. Match the term to its meaning: "root cause" versus "symptom".
  2. Find the bug: given a wrong total, which single line would you check first?
  3. Explain a real scenario: a teammate says "it just doesn't work" — what is the first question you ask them?

How to read AI-generated code

Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.

Language reading check

Find the program entry point, follow function calls one at a time, and keep track of each value's type. Do not jump into a class or helper until you know who calls it.

Before you move on

Next topic

Next, learn files, JSON, and local data. Before opening it, explain this chapter out loud in under one minute.

Open the interactive lesson →
← Python basic projectPython files, JSON, and local data →