Lesson 58 of 60
โฎPrev Nextโฏ

PY-101 ยท Testing and debugging

Debugging step by step

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

58of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Debugging step by step, running
    1. def apply_discount(total, percent):
    2. discount = total * percent / 100
    3. print(f"{total=} {percent=} {discount=}")
    4. return total - discount
    5. print(apply_discount(500, 10))
    6. print(apply_discount(500, 0.1))
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideaprint(f"{total=}")Debugging is finding where the program's real behaviour first differs from what you expected.
    3. 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

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. First step when you see a bug?
    2. 2. x = 3; print(f"{x=}") prints?
    3. 3. What does breakpoint() do when run locally?
    4. 4. Why change one thing at a time?
    5. 5. What are you looking for?

    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