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 <module>
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 <module>
c = a / b
ZeroDivisionError: division by zero
```

```
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 3, in <module>
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

`0`

## 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

`0`

## 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
```

#### Output

`0`

## 2 Comments

Comments are closed.