ZeroDivisionError: division by zero
A ZeroDivisionError means the number on the right of a division was zero. Division by zero has no answer in mathematics, so Python stops instead of inventing one. All three division operators raise it.
print(10 / 0)What Python says
Traceback (most recent call last):
File "example.py", line 1, in <module>
print(10 / 0)
~~~^~~
ZeroDivisionError: division by zeroprint(10 % 0)What Python says
Traceback (most recent call last):
File "example.py", line 1, in <module>
print(10 % 0)
~~~^~~
ZeroDivisionError: division by zeroThe error type is the same for all three division operators: /, // and %. Some versions of Python word the message a little differently for each.
Where it really comes from
Nobody writes 10 / 0 on purpose. The zero arrives inside a variable, and usually in one of these ways.
An average of nothing
marks = []
average = sum(marks) / len(marks)What Python says
Traceback (most recent call last):
File "example.py", line 2, in <module>
average = sum(marks) / len(marks)
~~~~~~~~~~~^~~~~~~~~~~~
ZeroDivisionError: division by zeroThe list is empty, so its length is 0. This is the most common cause of all. It appears when a filter matched nothing, a file was empty, or the user entered no values.
A percentage of a zero total
scored = 0
total = 0
print(scored / total * 100)What Python says
Traceback (most recent call last):
File "example.py", line 3, in <module>
print(scored / total * 100)
~~~~~~~^~~~~~~
ZeroDivisionError: division by zeroA counter that has not moved yet
A loop that divides by a counter starting at 0, or a rate worked out when the time elapsed is still 0, fails on the first round.
A value typed by the user
The user was asked how many people will share the bill, and typed 0.
Fix 1: check before dividing
A plain if is the clearest cure. Decide what the program should say when there is nothing to divide by.
marks = []
if marks:
print(sum(marks) / len(marks))
else:
print("No marks entered yet")Output
No marks entered yetA one-line form is useful inside a longer calculation.
marks = []
average = sum(marks) / len(marks) if marks else 0
print(average)Output
0Think about whether 0 is an honest answer, though. An average of 0 looks like real data. Sometimes "no data" is the truthful result, and None or a message is better.
Fix 2: try and except
def share(bill, people):
try:
return bill / people
except ZeroDivisionError:
return None
print(share(1200, 4))
print(share(1200, 0))Output
300.0
NoneThis is neat when the division is one step of several and you want one place to handle the failure.
Fix 3: validate the input
If the zero comes from a person, refuse it at the door. The person here typed 0 and then 4.
while True:
people = int(input("How many people? "))
if people > 0:
break
print("There must be at least one person.")
print("Each pays", 1200 / people)Output
How many people? 0
There must be at least one person.
How many people? 4
Each pays 300.0What about floats?
Dividing by 0.0 raises the same error. A divisor that is tiny but not zero does not, and gives a huge result instead.
print(1 / 0.000001)Output
1000000.0So if a result looks absurdly large, check whether the divisor is nearly zero.
Finding the zero
The traceback gives the line. If the line has more than one division, print each divisor just before it.
total_time = 0
print("total_time =", total_time)Output
total_time = 0Once you know which variable is zero, follow it back to where it got its value. The cause is nearly always missing data, not wrong arithmetic.
Other error messages
Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.