ZeroDivisionError: Division by Zero in Python

ZeroDivisionError: division by zero error occurs in Python when you tried to divide an integer value with a zero. In mathematics, dividing something with zero is not allowed, and similarly, in Python, it is also not allowed. In this short article, we will discuss how we can solve and handle ZeroDivisionError: division by zero error in Python using various methods. Moreover, we will also learn how to understand errors in Python so that we can easily solve them in the future.

ZeroDivisionError: Division by Zero

ZeroDivisionError: division by zero error occurs when we divide any numeric value with zero. The ZeroDivisionError belongs to ArithmeticError and it is thrown when a number is divided by 0. In general, in mathematics, when we divide the number by zero, we get an infinite number. It is impossible to write an infinite number so the Python interpreter raises ZeroDivisionError.

Let us take an example and see when we get ZeroDivisionError: division by zero error.

# first value
var1 = 10

# dividing by 0
result = var1/0

Output:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/tmp/ipykernel_243942/1650089224.py in <module>
      3 
      4 # dividing by 0
----> 5 result = var1/0

ZeroDivisionError: division by zero

As you can see, we got the error because we are not allowed to divide the number by zero. Now, let us go through various methods in order to solve the error:

Use a non-zero denominator

The best practice to get rid of ZeroDivisionError is to use a non-zero denominator. For example, see the example below where we are using a non-zero denominator for division.

# first value
var1 = 10

# dividing by non-zero
result = var1/1

This code will run without giving ZeroDivisionError: division by zero error because this time the denominator is a non-zero number.

Using if-else statements

If you don’t know what the value of the denominator will be, then you can use conditional statements to check the value of the variable. If it is equal to zero, then skip the division part.

Let us take an example to understand how we can use if-else statements to handle ZeroDivisionError: division by zero error.

# first value
var1 = 10

# denominator value
den = 0

if den!=0:
    # dividing by non-zero
    result = var1/0
    
# else statements
else:
    print("The denominator is zero")

Output:

The denominator is zero

Although the denominator was zero, still we didn’t get the error because the condition in the if statement was not true.

Using try-except blocks

The try-except block in Python is used to handle errors. The code inside the try block will be executed first. If there is an error in the try block, then the code inside the except block will be executed.

Let us take an example to see how we can use a try-except block to handle ZeroDivisionError: division by zero error.

# first value
var1 = 10

# denominator value
den = 0

try:
    # dividing by non-zero
    result = var1/den
    
except:
    print("The denominator is zero")
The denominator is zero

As you can see, rather than getting an error, we were able to print a message to handle the error.

Summary

In this short article, we discussed how we can solve and handle division by zero error using various methods. We discussed the main reason for getting ZeroDivisionError: division by zero error and learned a couple of methods to handle it. Moreover, we also covered how to interpret errors in Python so that in the future we can easily solve them.

Related Articles

Leave a Comment