This guide explains debugging with print() vs the debugger through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.
What You Will Learn
Print debugging exposes values at selected points; a debugger pauses execution, inspects variables and steps between statements. Both help when outcomes differ from expectations.
When and Why to Use It
The following example focuses on a practical use of Debugging with print() vs the Debugger.
Step-by-Step Example
def apply_discount(price, rate):
discounted_price = price * (1 - rate)
print("DEBUG discounted_price:", discounted_price)
return discounted_price
print(apply_discount(80, 0.25))
Understanding the Example
The debug line shows the intermediate discounted price before the function returns the same value. In PyCharm, a breakpoint can inspect it without modifying source output.
Expected Output
DEBUG discounted_price: 60.0
60.0
Important Behavior and Edge Cases
Temporary prints can leak private information and clutter production output. Use a debugger or configured logging for larger applications.
Try It Yourself
Place a breakpoint on the multiplication line and inspect price, rate and discounted_price.
Common Beginner Mistakes
- Temporary prints can leak private information and clutter production output. Use a debugger or configured logging for larger applications.
- Handling the symptom rather than identifying the failing operation, or letting a broad exception handler hide defects.
- Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.
Key Takeaways
- Print debugging exposes values at selected points; a debugger pauses execution, inspects variables and steps between statements.
- The debug line shows the intermediate discounted price before the function returns the same value.
- Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.