PY-101 ยท Testing and debugging
Debugging step by step
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Debugging is finding where the program's real behaviour first differs from what you expected. Reproduce the bug, then check values step by step with print() or a debugger (breakpoint() locally, or this course's Watch it scene), until you find the first wrong value. Fix that line, then run the code again to confirm.
- Reproduce a bug reliably
- Inspect values with print and stepping
- Fix the first wrong value, then retest
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
def apply_discount(total, percent):discount = total * percent / 100print(f"{total=} {percent=} {discount=}")return total - discountprint(apply_discount(500, 10))print(apply_discount(500, 0.1))
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
total=500 percent=10 discount=50.0
450.0
total=500 percent=0.1 discount=0.5
499.5 ๐ฏ average() returns the wrong answer for [2, 4, 6] (it should be 4.0). Find and fix the bug.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
Print total inside the loop. It should grow: use total += n.
- The problemAn invoice total is โน10 too high, but only sometimes. Guessing wastes hours. Printing the running total at each step shows the exact line where the extra โน10 sneaks in.
- The Python idea
print(f"{total=}")Debugging is finding where the program's real behaviour first differs from what you expected. - In real lifeThe second call shows a classic bug: someone passed 0.1 meaning 10%. Printing the values made the mismatch obvious in one run.
Start with a question
An invoice total is โน10 too high, but only sometimes. Guessing wastes hours. Printing the running total at each step shows the exact line where the extra โน10 sneaks in.
Every developer spends a large part of their time debugging. A method beats guesswork every time.
Explain it like I'm 10
Debugging is being a detective: follow the clues one step at a time until you find where the story stopped making sense.
The technical explanation
Write down what you expected and what actually happened.
Add print(f"{x=}") to see a variable's name and value together. Python 3.8+ supports the = shortcut.
Locally, breakpoint() pauses the program so you can inspect values. Here, the Watch it tab steps through for you.
Syntax
print(f"{total=}")
breakpoint() # pauses when run locally Real-world scenario
The second call shows a classic bug: someone passed 0.1 meaning 10%. Printing the values made the mismatch obvious in one run.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: assert โQuestions people ask
Is print debugging bad practice?
No. It is quick and effective. Remove the prints when you are done, or use the logging module for messages you want to keep.
How do I use the debugger in VS Code for Python?
Click in the margin to set a breakpoint, press F5 to start debugging, then step over lines and watch variables in the side panel.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Is print debugging bad practice?
No. It is quick and effective. Remove the prints when you are done, or use the logging module for messages you want to keep.
beginner How do I use the debugger in VS Code for Python?
Click in the margin to set a breakpoint, press F5 to start debugging, then step over lines and watch variables in the side panel.
intermediate First step when you see a bug?
Reproduce it reliably. You cannot fix what you cannot repeat.
intermediate x = 3; print(f"{x=}") prints?
x=3. The = shortcut shows name and value.
Quick revision
Remember
- Reproduce a bug reliably
- Inspect values with print and stepping
- Fix the first wrong value, then retest
- Change one thing at a time and rerun. Changing five things at once means you will not know which one fixed it, or broke something else.
Syntax
print(f"{total=}")
breakpoint() # pauses when run locally Official documentation
- pdb โ the Python debugger (docs.python.org)