ZeroDivisionError —
Obvious in theory, invisible inside a function.
You divided by zero — simple to spot in 5 / 0, but far less obvious when the denominator is a variable that only happens to be zero for certain inputs, like an empty list's length. Watch the denominator become zero on LearnBug and see exactly which call triggered it.
What is ZeroDivisionError?
Python raises this whenever a division or modulo operation (/, //, %) has a denominator of exactly zero — mathematically undefined, so Python refuses to produce a result rather than returning something misleading like infinity or 0.
Why visualization helps
The line total / count looks completely safe by itself — the bug is entirely in whatever produced count, often several lines earlier. LearnBug shows the denominator's actual value right at the moment of division, so you can trace back to exactly which earlier step allowed it to become zero.
Three ways a denominator becomes zero
An average of an empty list
sum(x) / len(x) when x is empty — len(x) is 0, and dividing by it crashes, even though sum([]) alone is perfectly valid (it's 0).
A rate or ratio where the total is zero
Computing a percentage (correct / total * 100) when nothing has happened yet and total is still 0.
A user-provided denominator
A calculator or converter function that divides by a value the user types in directly, without checking that it isn't zero.
average([]) — dividing by an empty list's length
scores = [] — an empty list, perfectly valid on its own
There's nothing wrong with an empty list by itself. The problem only appears once it's used in a way that assumes at least one element exists.
sum(scores) = 0 — also perfectly valid, the sum of nothing is 0
Neither len([]) nor sum([]) raises any error. Both are well-defined, correct results — the bug is still one step away.
sum(scores) / len(scores) — 0 / 0 is mathematically undefined, Python raises immediately
This is the exact moment the crash happens — not when the list was created empty, not when sum/len were computed, but specifically at the division itself.
Result: ZeroDivisionError: division by zero ✗
The broken code, and the traceback it produces
def average(numbers):
return sum(numbers) / len(numbers)
scores = [] # empty list — len(scores) is 0
print(average(scores))Traceback (most recent call last):
File "average.py", line 5, in <module>
print(average(scores))
^^^^^^^^^^^^^^^
File "average.py", line 2, in average
return sum(numbers) / len(numbers)
~~~~~~~~~~~~~~^~~~~~~~~~~~~~
ZeroDivisionError: division by zeroGuard against a zero denominator explicitly
def average(numbers):
if len(numbers) == 0:
return 0 # or raise a clearer, custom error — your call
return sum(numbers) / len(numbers)
scores = []
print(average(scores)) # 0, no crashQuick Reference
Frequently asked questions
Why doesn't Python just return infinity like some calculators do?
Division by zero is genuinely undefined in standard arithmetic, not just "very large" — treating it as infinity would be mathematically incorrect and could silently propagate a meaningless value through the rest of your program. Raising an exception forces you to decide explicitly what should happen in that case.
Does this apply to floating-point division too?
Yes — 5.0 / 0.0 raises the same ZeroDivisionError as 5 / 0. (This differs from some other languages and from the IEEE 754 floating-point standard used at the hardware level, where float division by zero can produce inf or nan — Python's built-in division deliberately raises instead.)
What's the cleanest way to guard against this in real code?
An explicit if denominator == 0: check right before the division, returning a sensible default or raising a more descriptive custom error, is usually clearest. A try/except around the division also works, but reads slightly less obviously as "this is an expected edge case" versus "something unexpected went wrong."
Why is this specifically an "empty collection" bug so often?
Because len() of an empty list, string, or dict is always exactly 0, and averages/rates/percentages very commonly divide by a count derived from len(). Any code path that can receive empty input and then compute an average is a candidate for this exact bug.
Watch the denominator's value right before it crashes
Paste your code into LearnBug and trace exactly which call produced a zero.