Print, Trace, and See What's Happening
Not every bug announces itself with an error โ sometimes code runs fine but does the wrong thing. The fix is to make the invisible visible: see what your program is actually doing.
Some bugs crash with an error - those are the easy ones. The trickier bugs are when code runs without complaint but produces the wrong answer. There's no traceback pointing at a line, so how do you find it? The answer is to make the invisible visible: look at what your program is actually doing, step by step. The humble print statement is the most-used debugging tool in the world - and tools like larztrace take it further.
Print: the universal debugger
When you don't know why code misbehaves, print out what it's actually doing - the values of variables at key points. Reality often differs from what you assumed.
def total_price(items):
total = 0
for item in items:
print("adding:", item, "running total:", total) # <- see it happen
total += item["price"]
return totalTrace: watch it run
Printing shows you specific spots; sometimes you want to watch the whole flow - every line and every change. That's what a tracer does.
Make the invisible visible
- Error-free wrong answers are found by seeing what the code actually does.
- print() is the universal debugger - show variables at key points.
- Check assumptions - bugs live in the gap between assumed and actual behaviour.
- larztrace watches every line and variable change - print turned up to eleven.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Some bugs run without errors but produce wrong answers - no traceback to guide you.
- Make the invisible visible: look at what your program is actually doing, step by step.
- print() is the world's most-used debugger - show the values of variables at key points.
- Bugs hide in the gap between what you assume the code does and what it actually does; larztrace shows the whole flow.
Frequently asked
Is using print statements to debug amateurish? Shouldn't I use a real debugger?
Not at all - print debugging is used constantly by programmers at every level, including experts at top companies, and there's no shame in it whatsoever. It's popular for good reasons: it's immediate (no setup), it works everywhere (any language, any environment), and it's perfectly suited to the core debugging question of 'what value does this variable actually have right now?'. Dedicated debuggers (tools that let you pause a program and step through it) are genuinely powerful and worth learning eventually - they let you inspect everything at a paused moment without editing code - but they're a complement to print debugging, not a replacement, and many seasoned developers reach for a print statement first because it's faster for most everyday bugs. So don't feel you must master a fancy debugger to be legitimate; strategic print statements will serve you well for a long time and remain useful forever. The skill that matters isn't which tool you use - it's the habit of making the program's actual behaviour visible and checking it against your assumptions. Start with print, add a tracer like larztrace to see fuller flow, and pick up a step debugger when you're ready; all are valid, and print is a real programmer's tool.
Where should I put print statements to find a bug efficiently?
Put them where your assumptions live - at the points where you believe something specific is true, so you can check whether it actually is. A good strategy is to bracket the problem: print a variable's value before and after the section you suspect, so you can see whether it changed as expected. If a loop is misbehaving, print inside the loop each iteration to watch how the values evolve (you'll often spot it running one time too many, or a value drifting wrong). If a function returns something surprising, print its inputs at the top and its result at the bottom. The efficient approach is a kind of binary search: rather than sprinkling prints everywhere, place a couple to narrow down which half of the code the problem is in, then move them closer to the exact spot based on what you learn - if the value is already wrong at the first print, the bug is earlier; if it's still right there but wrong later, it's in between. Print the specific things you're unsure about (the value, the type, the length of a list, whether a branch runs), run, read, and let each result tell you where to look next. And remember to remove them once you've found the bug. This bracket-and-narrow method finds bugs far faster than random changes, and it's exactly the isolation skill the next session builds on.
Related lessons
Read the Error Message
The error message is trying to tell you exactly what went wrong and where โ but only if yoโฆ
Debugging & Problem-SolvingIsolate the Problem
The fastest way to find a bug is to shrink the search area until the bug has nowhere to hiโฆ
Python Fundamentals (Hands-On)Repeating with Loops
Computers are brilliant at doing the same thing many times. Loops let you repeat actions wโฆ