Using Print Statements for Debugging
Overview
One of the simplest ways to debug is by printing variable values and flow checkpoints. You'll learn how to strategically place print statements to understand what your program is doing at every step, gaining insight into program behavior.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- Using print() for debugging: How to add print statements to understand code flow.
- Strategic placement: Where to put print statements for maximum insight.
- What to print: Variables, function calls, and flow checkpoints.
- Debugging techniques: Using print statements effectively.
- Best practices: When to use print debugging vs other tools.
Why Print Statements?
The simplest debugging tool is already built into Python: the print() function. Adding print statements lets you see what's happening inside your code as it runs.
Why Print Debugging Works
- No special tools needed - print() is always available
- Quick to add and remove
- Shows exact values at specific points
- Helps trace program flow
- Works everywhere Python runs
def calculate_total(price, quantity):
print(f"DEBUG: price = {price}, quantity = {quantity}")
total = price * quantity
print(f"DEBUG: total = {total}")
return total
result = calculate_total(10, 5)
print(f"DEBUG: result = {result}")
What to Print
Knowing what to print is key to effective debugging:
Variable Values
See what values variables have at different points
Function Entry/Exit
Confirm functions are being called and when they return
Loop Iterations
See which iterations execute and what values change
Conditional Branches
Verify which if/else branches execute
Calculations
Check intermediate calculation results
Data Structures
Inspect lists, dictionaries, and other data
Strategic Print Placement
Place print statements strategically to trace program flow:
def find_max(numbers):
print(f"DEBUG: Function called with {numbers}")
if not numbers:
print("DEBUG: Empty list, returning None")
return None
max_value = numbers[0]
print(f"DEBUG: Starting with max_value = {max_value}")
for num in numbers:
print(f"DEBUG: Checking {num} against {max_value}")
if num > max_value:
max_value = num
print(f"DEBUG: New max_value = {max_value}")
print(f"DEBUG: Returning {max_value}")
return max_value
Pro Tip: Use Prefixes
Prefix debug prints with "DEBUG:" or ">>>" so you can easily find and remove them later. Some developers use print(">>>", variable) for quick identification.
Practice: Trace a Loop
Try It YourselfThe function below should add the numbers up, but it prints 3 instead of 6. Add a DEBUG print inside the loop that shows running on every pass, run it, and use what the trace tells you to fix the bug. Leave both the debug print and the last line in place.
What happened? The trace shows running being replaced on every pass instead of added to. One print inside the loop turned an answer that was merely wrong into a sequence you can read.
Summary
In this lesson, you learned:
- Print debugging: Simple but effective debugging technique
- What to print: Variables, function calls, loops, conditionals
- Strategic placement: Put prints at key points in your code
- Best practices: Use prefixes, remove when done
Remember
Print debugging is great for quick debugging, but for complex programs, consider using a debugger (covered in the next lesson). However, print statements are always useful and often the fastest way to understand what's happening!
End-of-Lesson Exercises
Think about these questions to reinforce what you've learned:
Exercise 1: Print Debugging
How can print statements help you debug code? What should you print to understand program flow?
Exercise 2: Strategic Placement
Where would you place print statements to debug a function that's not working correctly?