Common Bugs and How to Spot Them
A surprising amount of debugging is pattern recognition โ the same handful of mistakes cause most bugs. Knowing them by sight means you fix in minutes what used to take hours.
Here's some encouraging news: a large share of all bugs come from a small set of recurring mistakes. Once you know these classics by sight, debugging speeds up enormously - you recognise the symptom and go straight to the likely cause. This session is a field guide to the usual suspects, the bugs that catch everyone, so you can spot them fast.
The usual suspects
These few patterns cause a huge fraction of everyday bugs. Learn to suspect them first.
| Bug | Symptom / cause |
|---|---|
| Off-by-one | Loop runs one too many/few; last or first item wrong |
| = vs == | Assigning when you meant to compare |
| Wrong type | Text '5' where a number 5 was expected (esp. from input) |
| Missing return | A function does the work but returns None |
| Modifying while looping | Changing a list as you loop over it misbehaves |
Notice how specific the symptoms are: a result that's wrong only at the edges screams 'off-by-one'; a function that seems to work but gives None screams 'missing return'. The symptom points at the classic cause.
Two that catch everyone
A couple deserve special mention because they're so common and so sneaky.
# 1) input() gives TEXT, not a number
age = input("Age: ") # age is the string "20", not 20
if age > 18: # TypeError, or surprising results
...
# fix: age = int(input("Age: "))
# 2) a function that forgets to return
def double(n):
n * 2 # computes it, then throws it away!
# fix: return n * 2return. A function that does its work but has no return statement gives back None, silently - and then the None causes a confusing error somewhere else, far from the real problem. Similarly, values coming from input(), files or the web are almost always text, so '5' + '3' becomes '53', not 8, until you convert. These two - missing returns and text-vs-number - account for a remarkable number of beginner bugs. Knowing them turns a baffling hour into a two-minute fix.Know the classics by sight
- Off-by-one - suspect it when only the first or last item is wrong.
- Missing return - suspect it when a value is unexpectedly None.
- Text vs number - input/files/web give text; convert before doing maths.
- = vs == and mutating while looping - classic sneaky ones.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A large share of bugs come from a small set of recurring mistakes - debugging is partly pattern recognition.
- Off-by-one (edges wrong), = vs == , wrong type, missing return, and mutating-while-looping are the usual suspects.
- The symptom points at the cause: only-edges-wrong = off-by-one; unexpected None = missing return.
- Values from input(), files and the web are text - convert before doing maths ('5' + '3' is '53').
Frequently asked
What exactly is an 'off-by-one' error and why is it so common?
An off-by-one error is when a count or index is wrong by exactly one - a loop runs one time too many or too few, or you access one position too far or not far enough. Classic examples: looping so that you miss the last item or try to access one past the end of a list (a common source of IndexError), starting a count at 1 when it should be 0 (or vice versa), or a range that includes one too many or too few values. They're extraordinarily common for a few reasons. First, humans naturally count from 1, but most programming counts positions from 0, so there's a constant translation that's easy to slip on. Second, the boundaries - the very first and very last item - are edge cases that are easy to get slightly wrong while the middle works fine, which is why the tell-tale symptom is a result that's correct except at the edges. Third, ranges and slices have conventions about whether the endpoint is included that are easy to misremember. The good news is that the specific symptom - only the first or last element is wrong, or you get an index error at the boundary - is a strong signal to check your loop bounds and indices for an off-by-one. When something works for everything except the edges, suspect off-by-one first and carefully check where your counting starts and stops. Everyone makes these, forever; recognising the pattern is what makes them quick to fix.
How do I get better at recognising these common bugs quickly?
Mostly through exposure and reflection - it's a pattern-recognition skill that builds naturally as you code, and you can accelerate it deliberately. The single best thing is to pay attention when you fix a bug: note what the symptom was and what the cause turned out to be, so your brain starts linking 'result wrong only at the end' to 'check for off-by-one' or 'got None unexpectedly' to 'look for a missing return'. Over time these links become instant, and you'll go straight to the likely cause instead of searching blindly. Practising with lots of small problems helps a great deal, because you encounter the common bugs repeatedly in low-stakes settings - tools like larzkata give you exercises with immediate feedback showing the failing case, which is excellent for building this instinct. It also helps to keep this field guide's list in mind and actively suspect these classics first when you hit a bug: run through 'could this be off-by-one? a type mismatch? a missing return?' before deeper investigation, since one of them is often the answer. And don't just fix bugs - understand them; a bug you understood the cause of teaches you to spot its pattern next time, while one you fixed by random tweaking teaches you nothing. With practice, a large fraction of bugs become things you recognise on sight and fix in moments, which is a big part of what makes experienced programmers seem fast - they're not smarter in the moment, they've just seen these same mistakes hundreds of times.
Related lessons
Isolate the Problem
The fastest way to find a bug is to shrink the search area until the bug has nowhere to hiโฆ
Debugging & Problem-SolvingThink Like a Problem-Solver
Debugging is really just problem-solving, and problem-solving is a skill you can build. Deโฆ
Intermediate PythonCleaner Code: Pythonic Idioms
Python rewards a certain style โ comprehensions, unpacking, good names and small readable โฆ