01 / 20

pdb & IDE
Debugging

Master the art of breakpoints, stepping, and variable inspection to find and fix bugs like a professional developer.

Python

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

★ Breakpoint
A marker on a line of code where execution will pause so you can examine state.
★ Step
Execute the next line (or step into/over functions) while paused at a breakpoint.
★ Inspect
View or evaluate variable values, expressions, and call stacks at the current pause point.

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.

print() Approach
Must guess which variables to print. Need to re-run after every change. Clutters code with temporary output. Cannot pause mid-execution.
Debugger Approach
Inspect any variable on-demand. Pause and explore interactively. Zero code changes required. Step forward or backward through logic.

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.

# Method 1: Insert a breakpoint in your code import pdb; pdb.set_trace() # Method 2: Python 3.7+ built-in shortcut breakpoint()

Essential pdb Commands

n (next) Execute next line, skip over calls s (step) Step into a function call p expr Print value of expression pp expr Pretty-print complex objects c (continue) Resume until next breakpoint l (list) Show source code around current line b line# Set breakpoint at line number q (quit) Exit the debugger

Step Over vs Step Into

main() 4 x = 10 5 y = add(x, 5) 6 print(y) add(a, b) 1 result = a + b 2 return result n (step over) s (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.

debug.py ▶ Step Over Step Into Continue 1 total = 0 2 for i in range(5): 3 total += i * 2 4 print(total) VARIABLES total 6 i 3

The Debugging Workflow

1. Set Breakpoint 2. Run Program 3. Inspect Variables 4. Step Forward repeat Breakpoint → Run → Inspect → Step → Repeat
1
Set a breakpoint where you suspect the bug lives (or just before it).
2
Run the program. Execution pauses at your breakpoint.
3
Inspect variables: are the values what you expected?
4
Step forward line by line. Watch how values change.

How to Apply pdb in Practice

1
Identify the symptom: wrong output, crash, or unexpected behavior.
2
Add breakpoint(): place it a few lines before where you think the bug is.
3
Run your script normally. The (Pdb) prompt will appear.
4
Use p to inspect: type p variable_name to check values.
5
Use n to step: advance line by line, checking values after each step.
6
Find the mismatch: the line where the actual value differs from what you expected is likely the bug.

Example 1: Finding an Off-by-One Bug

def average(numbers): total = 0 for n in numbers: total += n return total / len(numbers) - 1 # Bug! breakpoint() # Pause here result = average([10, 20, 30]) print(result) # Expected: 20, Got: 19.0

Example 2: Debugging a Loop

def find_max(items): max_val = 0 # Bug: what if all items are negative? for item in items: if item > max_val: max_val = item return max_val breakpoint() print(find_max([-3, -7, -1])) # Expected: -1, Got: 0

Example 3: Tracing a KeyError

def get_grade(scores, name): breakpoint() return scores[name] data = {"Alice": 92, "Bob": 85} print(get_grade(data, "bob")) # KeyError!

Common Mistakes

✕ Forgetting to Remove Breakpoints
Leaving breakpoint() calls in production code causes the program to hang at the (Pdb) prompt. Always search for and remove them.
✕ Typing Variable Names as Commands
If your variable is named n, s, or c, pdb treats it as a command. Use p n or !n to force evaluation.
✕ Setting Breakpoints Too Late
If you place the breakpoint after the buggy line, you will miss the error. Always set it before the suspected problem.

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

What would a debugger reveal as the root cause?