ZeroDivisionError: division by zero (Fix)

ZeroDivisionError: division by zero happens when Python tries to divide by 0.

This page shows what the error means, why it happens, and the simplest ways to prevent or handle it.

Quick fix

Check the value before dividing. If zero is possible, handle that case first.

denominator = 0

if denominator != 0:
    result = 10 / denominator
    print(result)
else:
    print("Cannot divide by zero")

If the denominator is 0, the program avoids the division and prints a clear message instead.

What this error means

Python raises ZeroDivisionError when you try to divide a number by 0.

This can happen with:

  • / for normal division
  • // for floor division
  • % for modulo

When this error happens, Python stops at that line unless you handle the error with try and except. If you are new to exceptions, see Python errors and exceptions explained.

Example that causes the error

Here is the smallest possible example:

print(10 / 0)

Output:

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print(10 / 0)
ZeroDivisionError: division by zero

In a division like 10 / 0, the value on the right side is called the denominator.

Because the denominator is 0, Python raises the error.

Why it happens

This error usually means the denominator became 0 somewhere earlier in your program.

Common reasons include:

  • A variable contains 0 when you expected another number
  • User input becomes 0 after conversion
  • A calculation returns 0 and you divide by that result
  • You use // or % with 0

Example with a variable:

denominator = 0
result = 10 / denominator
print(result)

Even though you are not writing 10 / 0 directly, the result is the same because denominator stores 0.

Fix 1: Check before dividing

This is the clearest fix for beginners.

Use an if statement before the division. Only divide when the denominator is not 0.

denominator = 2

if denominator != 0:
    result = 10 / denominator
    print(result)
else:
    print("Cannot divide by zero")

Output:

5.0

Now with a zero value:

denominator = 0

if denominator != 0:
    result = 10 / denominator
    print(result)
else:
    print("Cannot divide by zero")

Output:

Cannot divide by zero

This approach is simple and easy to read. In many programs, it is better than catching the error after it happens.

Fix 2: Handle the error with try-except

Use try-except when a zero value is possible and hard to predict.

denominator = 0

try:
    result = 10 / denominator
    print(result)
except ZeroDivisionError:
    print("Cannot divide by zero")

Output:

Cannot divide by zero

This tells Python:

  • Try to run the division
  • If ZeroDivisionError happens, run the except block instead

You can also use a fallback value:

denominator = 0

try:
    result = 10 / denominator
except ZeroDivisionError:
    result = None

print(result)

Output:

None

If you want to learn this pattern in more detail, read using try-except, else, and finally in Python or how to handle exceptions in Python.

Fix 3: Validate user input

A very common cause is dividing by a number entered by the user.

After converting the input, check whether the value is 0. Do not divide until the value is valid.

number = int(input("Enter a number to divide by: "))

if number == 0:
    print("You cannot divide by zero.")
else:
    result = 10 / number
    print("Result:", result)

Example run:

Enter a number to divide by: 0
You cannot divide by zero.

This is safer than dividing immediately.

If you need help with conversion, see how to convert user input to numbers in Python and Python int() function explained.

Division operations that can fail

These operations all raise ZeroDivisionError when the denominator is 0:

/ with zero

print(10 / 0)

// with zero

print(10 // 0)

% with zero

print(10 % 0)

All three fail for the same reason: the right side is 0.

Debugging steps

If you are not sure where the zero came from, check the denominator before the failing line.

Useful debugging commands:

print(denominator)
print(type(denominator))
print('a =', a, 'b =', b)
breakpoint()

Here is a simple example:

a = 10
b = 5 - 5

print('a =', a, 'b =', b)
result = a / b
print(result)

Output before the error:

a = 10 b = 0

This tells you that b became 0.

When debugging this error:

  • Print the denominator value before the failing line
  • Trace where that value comes from
  • Check calculations that may return 0
  • Check converted input values

Common mistakes

These are common causes of ZeroDivisionError:

  • Using a variable that is 0 in a division expression
  • Dividing by user input without checking it first
  • Using the result of a subtraction or count that became 0
  • Using // or % with 0
  • Expecting a function to return a non-zero value when it returns 0

Example of a calculation that becomes zero:

total = 8
count = 8 - 8

average = total / count
print(average)

count becomes 0, so the division fails.

FAQ

Does Python allow dividing by zero?

No. Python raises ZeroDivisionError when the denominator is 0.

Can this happen with modulo?

Yes. Using % 0 also raises ZeroDivisionError.

Should I use if or try-except?

Use if when you can easily check the value first. Use try-except when the zero value is less predictable.

Can float division by 0 also fail?

Yes. In normal Python code, dividing a float by 0 still raises ZeroDivisionError.

See also