ZeroDivisionError: division by zero, python

Description

ZeroDivisionError is a Python exception that occurs when attempting to divide a number by 0. It is raised when the divisor is zero, even if the dividend is also zero.

Reason of Error

In mathematics, division by zero is undefined. Therefore, Python raises this error when attempting to divide by zero. The ZeroDivisionError is related to other exceptions such as TypeError, ValueError, and NameError.

Example Code

a = 5
b = 0


print(a/b) # ZeroDivisionError

Solution to Solve Issue

To solve this issue, you should ensure that the divisor is not equal to zero before performing a division operation. Additionally, you can use try-except block to catch the error and display a custom message.

Example:
try:
print(a/b)
except ZeroDivisionError:
print("Division by zero is not allowed.")