In Mathematics, dividing any number by zero yields undefined result, because it is impossible to divide any number into zero parts.   Python raises the ZeroDivisionError exception when we attempt to divide a number by zero.

ExampleEdit & Run
print(10 / 0)
Output:
Traceback (most recent call last):  File "<string>", line 1, in <module>ZeroDivisionError: division by zero[Finished in 0.011284879874438047s]

The ZeroDivisionError is a subclass of the built-in ArithmeticError exception. This set of exceptions are generally raised when an illegal  mathematical operation is performed.

ExampleEdit & Run
print(issubclass(ZeroDivisionError, ArithmeticError))
Output:
True[Finished in 0.010561436880379915s]

Handling the ZeroDivisionError Exception

When a ZeroDivisionError occurs, it is usually accompanied by an error message indicating what operation was attempted. So you will easily tell that a division by zero was attempted and fix it.

It is also generally best practice to use a try...except statement to catch the error and handle it appropriately in cases where you expect the error to be raised. This allows the program to continue executing normally rather than crashing.

ExampleEdit & Run
num1 = input("Enter the first number:")
num2 = input("enter the second number:")

try:
   print(int(num1) / int(num2) )
except ZeroDivisionError:
     print("Division by Zero is not allowed.")
Output:
Enter the first number:Traceback (most recent call last):  File "<string>", line 1, in <module>EOFError: EOF when reading a line[Finished in 0.010168980108574033s]

In the above example we get two input numbers from the user. If the second number is zero, we catch the ZeroDivisionError exception that is raised and instead show the user a  friendly error message.