Using Breakpoints and Debuggers
Overview
Modern IDEs like VS Code include built-in debugging tools that let you pause execution and inspect variables. You'll practice setting breakpoints, stepping through code, and examining program state to pinpoint problems, using professional debugging techniques.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- What breakpoints are: How to pause execution at specific lines.
- Using debuggers: Professional tools for step-by-step debugging.
- Inspecting variables: Viewing variable values during execution.
- Stepping through code: Executing code line by line.
- Debugger features: Call stack, watch expressions, and more.
What Are Breakpoints?
A breakpoint is a marker you place in your code that tells the debugger to pause execution at that point. When your program hits a breakpoint, it stops, and you can inspect variables, step through code, and understand what's happening.
Why Use Breakpoints?
- Pause execution at specific points
- Inspect variable values without print statements
- Step through code line by line
- See the call stack (which functions called which)
- More powerful than print debugging
def calculate_total(price, quantity):
total = price * quantity # Set breakpoint here
return total
result = calculate_total(10, 5)
# When debugger hits breakpoint, you can:
# - See price = 10, quantity = 5
# - Step through each line
# - Inspect total before it's returned
Using a Debugger
Most modern IDEs (VS Code, PyCharm, etc.) have built-in debuggers. Here's how to use them:
Set Breakpoints
Click in the left margin next to the line where you want to pause, or press F9.
Start Debugging
Press F5 or click the debug button. Your program runs until it hits a breakpoint.
Inspect Variables
When paused, you can see all variable values in the debug panel.
Step Through Code
Use F10 (step over) or F11 (step into) to execute code line by line.
Continue or Stop
Press F5 to continue, or Shift+F5 to stop debugging.
Debugger Features
Modern debuggers offer powerful features:
Variable Inspection
See all variables and their current values
Call Stack
See which functions called which
Watch Expressions
Monitor specific expressions as code runs
Conditional Breakpoints
Only pause when a condition is true
Step Over/Into
Control how you move through code
Evaluate Expressions
Run code in the debugger console
Summary
In this lesson, you learned:
- Breakpoints: Markers that pause execution
- Debuggers: Professional tools for debugging
- Features: Variable inspection, call stack, watch expressions
- Stepping: Execute code line by line
- Benefits: More powerful than print debugging
Remember
Debuggers are powerful tools that save time. While print statements work, debuggers let you inspect code without modifying it. Learn to use your IDE's debugger—it's a skill that will serve you throughout your programming career!
End-of-Lesson Exercises
Think about these questions to reinforce what you've learned:
Exercise 1: Breakpoints
What is a breakpoint and how does it help with debugging?
Exercise 2: Debugger Features
What are some useful features of debuggers? How do they compare to print debugging?