- ZeroDivisionError: division by zero
- Different Variation of the error
- Root Cause
- How to reproduce this issue
- Output
- Solution 1
- Output
- Solution 2
- Output
- Solution 3
- Python dividing by zero
- # Table of Contents
- # ZeroDivisionError: float division by zero in Python
- # Checking if the value we are dividing by is not 0
- # Using a try/except statement to handle the error
- # Figure out where the variable got set to 0
- # Handling a number from user input with a try/except statement
- # Table of Contents
- # ZeroDivisionError: integer modulo by zero in Python
- # Figuring out where the variable got assigned a zero value
- # Check if the value is not 0 before using modulo
- # Using a try/except statement to handle the error
- # ZeroDivisionError: division by zero in Python
- # Checking if the value you are dividing by is not zero
- # Using a try/except statement to handle the error
- # Additional Resources
ZeroDivisionError: division by zero
ZeroDivisionError occurs when a number is divided by a zero. In Mathematics, when a number is divided by a zero, the result is an infinite number. It is impossible to write an Infinite number physically. Python interpreter throws “ZeroDivisionError: division by zero” error if the result is infinite number.
You can divide a number by another number. The division operation divides a number into equal parts or groups. Dividing a number into zero pieces or zero groups is meaning less. In mathematics, dividing a number by zero is undefined.
If a number is divided by zero, the result wil be infinite number. Python can not handle the infinite number because the infinite number is difficult to write in concrete form. In this case, Python throws “ZeroDivisionError: division by zero”. The error would be thrown as like below.
Traceback (most recent call last): File "/Users/python/Desktop/test.py", line 3, in c=a/b; ZeroDivisionError: integer division or modulo by zero [Finished in 0.1s with exit code 1]
Different Variation of the error
In different contexts the Zero Division Error-division by zero is thrown in various forms in python. The numerous variations of ZeroDivisionError are given below.
ZeroDivisionError: integer division or modulo by zero ZeroDivisionError: long division or modulo by zero ZeroDivisionError: float division by zero ZeroDivisionError: complex division by zero ZeroDivisionError: division by zero
Root Cause
The zero division error is due to either a number being divided by zero, or a number being modulo by zero. The denominator of the division operation should be a non zero numeric value. If the demonimator is zero then ZeroDivisionError will be thrown by python interpreter.
Dividing a number into zero pieces or zero groups does not make sense. The result is infinite number not representable in python. Therefore, python throws “ZeroDivisionError: integer division or modulo by zero”. This error occurs for all numbers such as integer, long, float and complex number
How to reproduce this issue
A number must be divided by an another non zero number. Python interpreter will throw ZeroDivisionError if you create a simple program with a number divided by zero. If the division denominator is set to zero, then this error will occur.
The example code below shows how this issue can be reproduced.
a = 8 b = 0 c = a / b print c
Output
Traceback (most recent call last): File "C:\Users\User\Desktop\python.py", line 3, in c = a / b ZeroDivisionError: division by zero
Traceback (most recent call last): File "/Users/python/Desktop/test.py", line 3, in c = a / b ZeroDivisionError: integer division or modulo by zero [Finished in 0.0s with exit code 1]
Solution 1
Python can not divide a number by zero. Before doing a division or modulo operation, the denominator must be verified for nonzero. The code below shows how to handle a denominator when it is zero.
a = 8 b = 0 c = ( a / b ) if b != 0 else 0 print c
Output
Solution 2
If the program is not sure of the denominator value, the denominator value may be zero in some rare cases. In this case, handle the ZeroDivisionError when it occurs. The example below shows how to handle the exception of ZeroDivisionError in the code.
try: a = 8 b = 0 c = a / b except ZeroDivisionError: c = 0 print c
Output
Solution 3
In the program, if the denominator is zero, the output of the division operation can be set to zero. Mathematically, this may not be correct. Setting zero for the division operation will solve this issue in real-time calculation. The following code shows how to set the zero for the division operation.
a = 8 b = 0 if b == 0: c = 0 else: c = a / b print c
Python dividing by zero
Last updated: Feb 16, 2023
Reading time · 6 min
# Table of Contents
# ZeroDivisionError: float division by zero in Python
The Python «ZeroDivisionError: float division by zero» occurs when we try to divide a floating-point number by 0 .
To solve the error, use an if statement to check if the number you are dividing by is not zero, or handle the error in a try/except block.
Here is an example of how the error occurs.
Copied!a = 15.0 b = 0 # ⛔️ ZeroDivisionError: float division by zero result = a / b
It’s unclear what value is expected when we divide by 0 , so Python throws an error.
When we divide a number by 0 , the result tends towards infinity.
# Checking if the value we are dividing by is not 0
One way to solve the error is to check if the value we are dividing by is not 0 .
Copied!a = 15.0 b = 0 if b != 0: result = a / b else: result = 0 print(result) # 👉️ 0
We check if the b variable doesn’t store a 0 value and if it doesn’t, we divide a by b .
Otherwise, we set the result variable to 0 . Note that this could be any other value that suits your use case.
If setting the result variable to 0 if b is equal to 0 suits your use case, you can shorten this to a single line.
Copied!a = 15.0 b = 0 result = b and a / b print(result) # 👉️ 0
The expression x and y first evaluates x , and if x is falsy, its value is returned, otherwise, y is returned.
Since 0 is a falsy value, it gets returned if the b variable in the example stores a 0 value, otherwise, the result of dividing a by b is returned.
# Using a try/except statement to handle the error
Alternatively, you can use a try/except statement.
Copied!a = 15.0 b = 0 try: result = a / b except ZeroDivisionError: result = 0 print(result) # 👉️ 0
We try to divide a by b and if we get a ZeroDivisionError , the except block sets the result variable to 0 .
# Figure out where the variable got set to 0
The best way to solve the error is to figure out where the variable gets assigned a 0 and check whether that’s the expected behavior.
Here are some common ways you might get a zero value unexpectedly.
Copied!print(int()) # 👉️ 0 print(int(0.9)) # 👉️ 0
You might also get a zero value if you multiply any number by 0 .
Copied!num_1 = 5 num_2 = num_1 * 0 print(num_2) # 👉️ 0
Make sure you haven’t assigned the result of multiplying a number by 0 to a variable.
# Handling a number from user input with a try/except statement
If you need to take a number from user input, use a try/except statement to handle the potential ZeroDivisionError .
Copied!num_1 = 15.0 try: num_2 = int(input('Enter a number: ')) result = num_1 / num_2 print(f'The result of the division is: result>') except (ZeroDivisionError, ValueError): print('Specify a positive integer value')
If the user passes an invalid integer, a ValueError is raised and is then handled by the except block.
Similarly, if the user enters 0 , a ZeroDivisionError is raised and is handled by the except block.
Otherwise, the result of the division gets printed in the try block.
# Table of Contents
# ZeroDivisionError: integer modulo by zero in Python
The Python «ZeroDivisionError: integer division or modulo by zero» occurs when we use the modulo % operator with an integer and a zero.
To solve the error, figure out where the 0 comes from and correct the assignment.
Here is an example of how the error occurs.
Copied!a = 6 b = 0 # ⛔️ ZeroDivisionError: integer division or modulo by zero # ZeroDivisionError: integer modulo by zero result = a % b
We tried using the modulo % operator with a zero.
It’s unclear what value is expected when we divide by 0 , so Python throws an error.
When we divide a number by 0 , the result tends towards infinity.
# Figuring out where the variable got assigned a zero value
The best way to solve the error is to figure out where the 0 value comes from and correct the assignment.
Here are some unexpected sources of 0 .
Copied!import random print(int()) # 👉️ 0 print(int(0.9)) # 👉️ 0 print(random.randint(0, 10)) # 👉️ 0 print(list(range(0, 5))) # 👉️ [0, 1, 2, 3, 4]
If you use the random.randint() method or the range() class, make sure to start from 1 , and not from 0 .
Copied!import random print(random.randint(1, 10)) # 👉️ 4 # 👇️ [1, 2, 3, 4] print(list(range(1, 5)))
# Check if the value is not 0 before using modulo
You can also conditionally check if the variable doesn’t store a 0 value before using the modulo operator.
Copied!a = 6 b = 0 if b != 0: result = a % b else: # 👇️ this runs print('b is equal to 0')
The if statement checks if the b variable doesn’t store a 0 value before using the modulo % operator.
# Using a try/except statement to handle the error
Alternatively, you can use a try/except statement.
Copied!a = 6 b = 0 try: result = a % b print(result) except ZeroDivisionError: pass
We use the modulo operator and if we get a ZeroDivisionError , the except block is run.
You can set the result variable to a value that suits your use case in the except block or simply pass.
The modulo (%) operator returns the remainder from the division of the first value by the second.
Copied!print(10 % 2) # 👉️ 0 print(10 % 4) # 👉️ 2
If the value on the right-hand side is zero, the operator raises a ZeroDivisionError exception.
The left and right-hand side values may also be floating point numbers.
If the left-hand side value is a float and the right-hand side value is 0 , you would get a «ZeroDivisionError: float modulo» error.
Copied!# ⛔️ ZeroDivisionError: float modulo print(10.5 % 0) # 👉️ 0
# ZeroDivisionError: division by zero in Python
The Python «ZeroDivisionError: division by zero» occurs when we try to divide a number by 0 .
To solve the error, use an if statement to check if the number you are dividing by is not zero, or handle the error in a try/except block.
Here is an example of how the error occurs.
Copied!a = 5 b = 0 # ⛔️ ZeroDivisionError: division by zero result = a / b
It’s unclear what value is expected when we divide by 0 , so Python throws an error.
When we divide a number by 0 , the result tends towards infinity.
# Checking if the value you are dividing by is not zero
One way to solve the error is to check if the value we are dividing by is not 0 .
Copied!a = 5 b = 0 if b != 0: result = a / b else: result = 0 print(result) # 👉️ 0
We check if the b variable doesn’t store a 0 value and if it doesn’t, we divide a by b .
Otherwise, we set the result variable to 0 . Note that this could be any other value that suits your use case.
If setting the result variable to 0 , if b is equal to 0 suits your use case, you can shorten this to a single line.
Copied!a = 5 b = 0 result = b and a / b print(result) # 👉️ 0
The expression x and y first evaluates x , and if x is falsy, its value is returned, otherwise, y is returned.
Since 0 is a falsy value, it gets returned if the b variable in the example stores a 0 value, otherwise the result of dividing a by b is returned.
# Using a try/except statement to handle the error
Alternatively, you can use a try/except statement.
Copied!a = 5 b = 0 try: result = a / b except ZeroDivisionError: result = 0 print(result) # 👉️ 0
The try/except block is known as «asking for forgiveness, rather than permission».
We try to divide a by b and if we get a ZeroDivisionError , the except block sets the result variable to 0 .
The best way to solve the error is to figure out where the variable gets assigned a 0 and check whether that’s the expected behavior.
Here are some common ways you might get a zero value unexpectedly.
Copied!print(int()) # 👉️ 0 print(int(0.9)) # 👉️ 0
You might also get a zero value by multiplying a number by 0 .
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.