HOWTO · Python

How to Fix ZeroDivisionError: Float Division by Zero in Python

We will demonstrate how to resolve zero division error in Python.

On this page

Python raises ZeroDivisionError when a division operation has a divisor equal to zero. This can happen with integers or floats, including when a calculation produces the divisor. This guide explains the cause, shows integer and float behavior, and presents two ways to manage the result. The examples below show the observed results for ordinary division, zero divisors, and both handling patterns.

What causes ZeroDivisionError?

Run this example:

print(14 / 2)
7.0

For a failing operation, Python reports ZeroDivisionError: division by zero. The exception is raised by the division operator; it is not specific to float division. An integer divisor of 0 and a float divisor of 0.0 both cause it. In the following self-contained example, subtracting equal values makes the divisor zero. The handler prints the exception’s type and message so they remain selectable text when you run the code. The observed output shows the same exception type and message for integer and float division by zero.

for numerator, divisor in [(17, 8 - 8), (17.0, 8.0 - 8.0)]:
    try:
        print(numerator / divisor)
    except ZeroDivisionError as error:
        print(f"{type(error).__name__}: {error}")
ZeroDivisionError: division by zero
ZeroDivisionError: division by zero

The expression that supplies the divisor matters as much as a variable’s initial value. A calculation, user input converted to a number, or a later update may produce zero. Floats do not make division by zero valid, and the exception message does not mean the numerator is the problem. Check the value used on the right side of / at the point where the operation occurs.

Check the divisor before dividing

When zero is an expected value, test the divisor first and choose a result that makes sense for the application. The example below uses None to signal that a quotient is unavailable; another program might skip the calculation, show a message, or apply a domain-specific fallback. Do not substitute an arbitrary number unless that value has a valid meaning in the calculation.

numerator = 17
divisor = 8 - 8

if divisor == 0:
    quotient = None
else:
    quotient = numerator / divisor

print(quotient)
None

This check avoids performing the division when its divisor is zero. It is useful when zero is a normal input and the program must decide what to do before calculating. Compare the divisor to zero, not to a positive threshold: negative divisors are valid for ordinary arithmetic. If the value can change between checking and dividing, keep both operations close together or use exception handling around the operation.

Handle division with try and except

Use a try clause when the division is attempted and handle ZeroDivisionError with a matching except clause. Catching this specific exception keeps unrelated programming errors visible instead of treating every failure as division by zero. This runnable example shows both a successful division and the zero-divisor path, with the observed output shown below.

for divisor in (4, 0):
    try:
        print(20 / divisor)
    except ZeroDivisionError:
        print("Cannot divide by zero.")
5.0
Cannot divide by zero.

The try and except keywords form Python’s exception-handling syntax; they are not methods. Keep the code that may raise the exception inside the try block, and put the recovery action in the matching handler. If no recovery is appropriate, allow the exception to propagate so the caller can handle it.

Choose a strategy for your code

Prefer a pre-division check when zero is an expected input and you already know the correct alternative behavior. Use try/except ZeroDivisionError when attempting the operation is the clearest path and the exceptional case has a useful recovery. Both approaches should preserve the meaning of the calculation rather than merely hide the error.

Neither approach guarantees that the whole application cannot fail: other operations can raise other exceptions, and a fallback can still be wrong for the domain. Avoid a bare except, which can hide unrelated defects. If you are diagnosing an existing failure, trace the divisor back to its source and inspect the arithmetic or input that produced it. Then choose whether zero should be rejected, represented as an unavailable result, or handled through a domain-specific rule.