pdb & IDE
Debugging
Master the art of breakpoints, stepping, and variable inspection to find and fix bugs like a professional developer.
What Is Debugging?
Debugging is the systematic process of finding and resolving defects (bugs) in your code. Instead of guessing where things go wrong, a debugger lets you pause execution, examine the program state, and step through code one line at a time.
The Three Core Concepts
Why Not Just Use print()?
Many beginners scatter print() statements through their code to check values. While this works for trivial bugs, it has serious limitations as programs grow.
Introducing pdb: Python's Built-in Debugger
pdb stands for Python DeBugger. It ships with every Python installation: no downloads, no setup. You interact with it via a command-line prompt.
Essential pdb Commands
Step Over vs Step Into
IDE Debugging: The Visual Way
IDEs like VS Code, PyCharm, and Thonny provide a graphical interface for the same breakpoint/step/inspect workflow. Instead of typing commands, you click buttons and see variable values in side panels.
The Debugging Workflow
How to Apply pdb in Practice
Example 1: Finding an Off-by-One Bug
Example 2: Debugging a Loop
Example 3: Tracing a KeyError
Common Mistakes
pdb vs IDE Debugger
| pdb (Terminal) | IDE (VS Code / PyCharm) | |
|---|---|---|
| Setup | Zero: built into Python | Install IDE + Python extension |
| Interface | Text commands in terminal | Graphical buttons and panels |
| Set Breakpoint | breakpoint() in code | Click the gutter (left margin) |
| Inspect Variables | Type p variable | Hover mouse or Variables panel |
| Step Controls | n, s, c | Toolbar buttons or F-keys |
| Remote / SSH | Works anywhere Python runs | Requires remote extension |
| Best For | Servers, quick checks, scripts | Large projects, visual learners |
Question 1 of 5
Which pdb command executes the next line without entering any function calls on that line?
Question 2 of 5
What is the simplest way to set a breakpoint in Python 3.7+?
Question 3 of 5
You are paused in pdb and your variable is named c. You type c and press Enter. What happens?
Question 4 of 5
In an IDE debugger, what does the red dot in the left margin of the code editor represent?
Question 5 of 5
A student writes the following function and gets unexpected results:
def count_vowels(text):
count = 0
vowels = "aeiou"
for char in text:
if char in vowels:
count += 1
return count
When called with count_vowels("HELLO"), it returns 0 instead of 2.
What would a debugger reveal as the root cause?