Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 11 / Topic 2 of 12

Debugging

Debugging means finding out why the code does the wrong thing, and fixing it. Like a doctor: first read the report (traceback), then examine the patient step by step (debugger).

Reading a traceback (recap of Part 6)

Read it from the bottom up:

  1. Last line: the error type and message, e.g. ZeroDivisionError: division by zero.
  2. The line above it: the exact file, line number and code that failed.
  3. Lines further up: who called that function (the call chain).
Traceback (most recent call last):
  File "02_debugging_tour.py", line 28, in show_traceback
    daily_report(data)
  File "02_debugging_tour.py", line 22, in daily_report
    print(f"{shop}: ₹{average_price(prices):,.2f}")
  File "02_debugging_tour.py", line 17, in average_price
    return sum(prices) / len(prices)  # bug: crashes when prices is empty
ZeroDivisionError: division by zero

Here average_price was called with an empty list for "Nagpur store". (Python 3.13 also draws ~~~^^^ marks under the exact part of the line that failed.)

print vs breakpoint()

  • print debugging: add print(x) lines, run, read, delete them again. Fine for a quick look, but easy to forget in the code.
  • breakpoint(): a built-in function that pauses the program on that line and opens pdb, the Python debugger. While paused you can look at any variable and run the code one line at a time.
pdb command Meaning
n next: run this line, stop at the next one
s step: go into the function called on this line
c continue: run until the next breakpoint (or the end)
p total print the value of total
q quit the debugger and stop the program

Try it: python examples/02_debugging_tour.py --debug (macOS/Linux: python3 …)

> .../02_debugging_tour.py(39)step_through()
-> breakpoint()  # type n (next line), p total (print), c (continue), q (quit)
(Pdb) n
> .../02_debugging_tour.py(40)step_through()
-> total = 0
(Pdb) p prices
[120, 80, 100]
(Pdb) n
> .../02_debugging_tour.py(41)step_through()
-> for price in prices:
(Pdb) c
total = 300

The arrow -> shows the line that will run next. Always remove breakpoint() before you commit your code.

The VS Code debugger

The same idea, with buttons (needs the Python extension from Part 1):

  1. Click left of a line number to set a breakpoint (a red dot).
  2. Press F5 and choose Python Debugger: Current File.
  3. The program stops at the red dot. Look at the Variables panel on the left.
  4. F10 = step over (n), F11 = step into (s), F5 = continue (c), Shift+F5 = stop (q).
  5. Use the Debug Console to type an expression, like p in pdb.

🔍 Going deeper: python -m pdb script.py starts any script inside pdb from its first line. Setting the environment variable PYTHONBREAKPOINT=0 makes every breakpoint() do nothing.

Read more: the official pdb documentation.

Done here? The practice set for Part 11 — 7 exercises with solutions — is on the part page.

Search the guide