ZeroDivisionError: float division | Python

In mathematics, the division of any non-zero number, whether positive or negative, by zero is considered undefined, as there is no valid value for such an operation. This is due to the fundamental fact that attempting to define the result of division by zero leads to a contradiction or inconsistency within the mathematical framework. Consequently, Python raises the "ZeroDivisionError: float division" to prevent such invalid calculations and maintain the integrity of mathematical principles in computations.


The "ZeroDivisionError" is a built-in Python exception that belongs to the "ArithmeticError" superclass. It is raised when attempting to divide a number by zero, which results in an infinite number in mathematics. Since representing infinite numbers physically is impossible, Python raises the "ZeroDivisionError" to avoid invalid calculations. When implementing program logic involving division operations, it is essential to handle potential "ArithmeticError" or "ZeroDivisionError" scenarios to prevent the program from terminating abruptly.

Handling ZeroDivisionError in Python

How to Make Division by Zero to Zero in Python?
Wrap it in try-except
try: z = x / y except ZeroDivisionError: z = 0
Or check before you do the division:
if y == 0: z = 0 else: z = x / y

The above code can be reduced to:

z = 0 if y == 0 else (x / y)

Reproducing the error

x = 5 y = 0 z = x/y print(z)
Traceback (most recent call last): File "./", line 3, in <module> ZeroDivisionError: division by zero

You can solve the ZeroDivisionError by the following ways:

Wrap it in try except

x = 5 y = 0 try: z = x/y except ZeroDivisionError: z=0 //handle here print(z) //output as 0

Check before you do the division

x = 5 y = 0 if y == 0: z = 0 else: z = x / y print(z) //output as 0

The above code can be reduced to:

x = 5 y = 0 z = 0 if y == 0 else (x / y) print(z) //output as 0

Different Variation

In Python, the Zero Division Error:division by zero is thrown in various forms in different contexts. They are given below:

  1. ZeroDivisionError: division by zero
  2. ZeroDivisionError: float division by zero
  3. ZeroDivisionError: integer division or modulo by zero
  4. ZeroDivisionError: long division or modulo by zero
  5. ZeroDivisionError: complex division by zero


Using conditional statements or other appropriate checks to avoid division by zero, you can prevent the "ZeroDivisionError: float division" and ensure your Python code executes without any errors.