The ZeroDivisionError in Python is raised when we try to divide a number by 0. This is because, in mathematics, such division leads to an infinite number (∞) that cannot be physically represented/quantified.
Figure : a/b results to ZeroDivisionError if b==0 otherwise a/b is mathematically correct.
Depending on the nature of the division and the numeric type of the numerator, ZeroDivisionError can come in different forms. Here is one of the forms.
a = 9.8 b = 0 a/b
ZeroDivisionError: float division by zero
Other variants include:
- ZeroDivisionError: float modulo for 9.8%0,
- ZeroDivisionError: division by zero produced by 9/0
- ZeroDivisionError: integer division or modulo by zero produced by 9//0, and,
- ZeroDivisionError: complex division by zero, produced by complex(9, 3)/0
Before implementing any of the two solutions discussed here, ensure that the division by zero is the expected behavior.
One of the most common scenarios that lead to unexpected division by zero is rounding off, which yields zero. For example, casting a float of less than one into an integer leads to ZeroDivisionError. Let’s see that in the code.
a = 8 b = int(3/4) # b is the same as int(0.75), equal to zero. a/b
ZeroDivisionError: division by zero
Had we not rounded off 3/4 to an integer, a/b could have been computed without an Error, but int(3/4)=int(0.75)=0 hence ZeroDivisionError.
Once you are sure that division by zero is expected behavior in your computations, you can use any of the following solutions to suppress the ZeroDivisionError.
- Solution 1: Using conditional if-statement, or,
- Solution 2: Handling the error using a try-except statement.
In this case, we need to check the variable that stores the divisor. If the divisor is 0, avoid the division. For example,
a = 9.8 b = 0 if b == 0: result = -99 else: result = a/b print(result)
In the above example, the variable that stores the divisor is b. Since b==0, we set the result to -99 (you can use any other value); otherwise, perform the division (a/b).
We can reduce the if-condition in the above code snippet into a one-liner as follows:
a = 9.8 b = 0 result = a/b if b!=0 else -99 print(result)
That is to say, result = a/b if b is not equal to 0; otherwise, result=-99.
In this solution, we attempt to perform the division (say a/b), and if the division leads to ZeroDivisionError, we perform a different operation. Example.
a = 9.8 b = 0 try: result = a/b except ZeroDivisionError: result = -99 print(result)
In the above snippet, the try-block attempts to compute a/b and assigns it to the result variable. If ZeroDivisionError is raised (which is the case with our example), except-block sets the variable result to the value -99 (once again, you can use any value that suits your case).
ZeroDivisionError occurs when you divide a number by 0. The first step to solving such an error is to ensure that your computations are correct and that division by 0 is accepted behavior.
Once you are sure there is nothing wrong with your computations, you can use the two solutions suggested in this article – using an if-statement or handling the error using try-except statements.