Debugging with print() in Python

Debugging with print() in Python: The First Tool Every Developer Reaches For Before you learn debuggers, breakpoints, or logging frameworks, there is a simpler tool that professional developers still use every day: the debugging print statement. The idea is straightforward. When your code isn’t behaving as expected, you add temporary print() calls at strategic points to see what the program is actually doing, not what you think it’s doing. The Helsinki MOOC introduces this through a concrete example. A program calculates daily wages and should double them on Sundays. The logic looks right, but the output is wrong. The first instinct might be to re-read the code looking for the mistake. A faster approach is to let the program tell you where it’s failing. You add print statements around the suspicious section: hourly_wage = 20.0 hours = 6 day = "Sunday" daily_wages = hourly_wage * hours print("condition:", day == "sunday") if day == "sunday":     print("wages before:", daily_wages)     daily_wages * 2     print("wages after doubling:", daily_wages) The output reveals the condition is evaluating to False, which means the if block never runs. The program isn’t doubling anything. Now you know exactly where to look. The actual bug turns out to be a capitalisation mismatch. The input contained "Sunday" but the condition was checking for "sunday". One character difference. Without the debugging print, that could take much longer to find. This pattern scales. In a more complex program, you might not know which section is failing. Print statements let you narrow it down systematically, confirm what works, isolate what doesn’t, fix the right thing. Two things worth remembering: print statements should be removed once the bug is fixed, and they are a starting point, not the whole toolkit. But for a developer at any level, knowing how to use them well is not optional. The professionals use them too. #Python #PythonMOOC2026 #BackendDevelopment #SoftwareEngineering #LearningInPublic #UniversityOfHelsinki

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories