ShareYourGroupShareYourGroupError Explainer

Python ZeroDivisionError: division by zero

A plain-words explanation of this Python message: what it means, the usual causes, and the steps that fix it.

The messageZeroDivisionError: division by zero

Make the error yourself

Run with Python 3.14.10. This is the smallest example we could find that shows it.

Python program (main.py)

marks = []
average = sum(marks) / len(marks)
print(average)

Output (the real message)

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    average = sum(marks) / len(marks)
              ~~~~~~~~~~~^~~~~~~~~~~~
ZeroDivisionError: division by zero

Why it happens

Dividing by zero has no answer, so Python stops. The cause is rarely a literal / 0: usually a count or a total that is zero because a list is empty, a filter found nothing, or a value was never set. The example calculates an average of an empty list, so len(marks) is 0. The wording is slightly different for // and % ("integer division or modulo by zero" in some versions).

How to fix it

  1. Find the divisor in the failing line and print it.
  2. Decide what the answer should be when it is zero: skip the calculation, show a message or use 0.
  3. Check before dividing: if count != 0:.
  4. Or catch the error: try: ... except ZeroDivisionError: ....

The corrected version

Python program (main.py)

marks = []
if marks:
    print(sum(marks) / len(marks))
else:
    print("no marks yet")

Output (the corrected version)

no marks yet

How to avoid it

Whenever you calculate an average, a percentage or a rate, ask what happens with no data, and handle that case on purpose.

Questions

Does float division by zero also fail?

Yes, 1 / 0.0 raises the same error in Python, unlike some other languages that return infinity.

This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.