Facebook Pixel[Solved] ZeroDivisionError: division by zero | Blog | CodeWithHarry
[Solved] ZeroDivisionError: division by zero

[Solved] ZeroDivisionError: division by zero

"Understanding and handling Zero Division Error in Python: A comprehensive guide explaining what Zero Division Error is, why it happens, and how to manage it using Python. This blog includes real code examples, visual aids, and expert insights to help both beginners and seasoned programmers. Happy Coding!"

By CodeWithHarry

Updated: 5 April 2025

Zero Division Error is a common exception in Python that occurs when you try to divide a number by zero. This fundamental mathematical concept is undefined, and Python, like most programming languages, cannot handle this operation. In this blog, I will tell you what Zero Division Error is, how it happens, and how to handle it in Python.

PS C:\Users\iitia> python
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>

What is a Zero Division Error?

In mathematics, division by zero is undefined. When you try to perform this operation in Python, it results in a ZeroDivisionError. This is a type of arithmetic error that you need to be aware of when performing calculations.
For your understanding, the following video shows how to reproduce this error in Python using three lines of code:

Zero Division Error Video

Why Does It Happen?

Let's look at an example of a code snippet that will lead to this error:

a = 45
b = 0
result = a / b

Here, we're trying to divide 45 by 0, which leads to a ZeroDivisionError. The code will produce the following error message:

Zero Division Error Image

How to Handle Zero Division Error

1. Using Try-Except Block

One of the most common ways to handle this error is by using a try-except block. If you think this error might come due to user input, you should use a try-catch block like this:

a = 10
b = int(input("Enter b"))
 
try:
    result = a / b
except ZeroDivisionError:
    print("You can't divide by zero!")
    result = None

This code will handle the error gracefully, and instead of crashing, it will print the message "You can't divide by zero!".

Zero Division Error Input Image

2. Checking Before Division

You can also prevent the Zero Division Error by checking the divisor before performing the division. In the below code, I am checking variable b for its equality with 0:

a = 10
b = 0
 
if b != 0:
    result = a / b
else:
    print("You can't divide by zero!")
    result = None

If-Else Error Check Image

Conclusion

ZeroDivisionError is common but the good news is that it is an avoidable error in Python.
I hope I was able to explain to you the basic maths behind this. I get this question every once in a while from my Python course students.
At times, identifying where the division by zero is happening within a complex codebase is not always straightforward. It might require careful tracing of the code, testing individual components, and being aware of the values that variables can take.

I hope this post was able to solve the ZeroDivisionError in Python for you. Feel free to ask any questions in the comments. Happy Coding!

Tags

solvedpythonzerodivisionerror