Debugging — Python Error Guide

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.

ZeroDivisionErrorException type
len() == 0Common source
PythonLanguage

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.

LearnBug — the denominator becoming zero mid-execution
🖼 Add a LearnBug screenshot here
What Causes It

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).

Empty input

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.

Uninitialized total

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.

Unvalidated input
Walkthrough

average([]) — dividing by an empty list's length

1

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.

scores = []empty, valid
len(scores) = 0 — a valid, unremarkable value
2

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) = 0
len(scores) = 0about to divide by this
Both values computed fine — 0 / 0 is about to be attempted
3

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

Code Example

The broken code, and the traceback it produces

PythonBroken — no check for an empty list
def average(numbers):
    return sum(numbers) / len(numbers)

scores = []   # empty list — len(scores) is 0
print(average(scores))
TracebackWhat Python actually prints
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 zero
The Fix

Guard against a zero denominator explicitly

PythonFixed — check before dividing
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 crash

Quick Reference

Error classZeroDivisionErrorRaised by /, //, and % when the denominator is exactly 0
Typical causeEmpty collection / zero totalMost often len() of an empty list feeding directly into a division
Fix patternGuard clause before dividingCheck the denominator explicitly and handle the zero case separately
Applies to/, //, and %All three division-family operators raise the same error on a zero denominator

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.

Open Playground →