Last Updated : 07 Jul, 2025
ZeroDivisionError is raised when a program attempts to perform a division operation where the denominator is zero. This situation is mathematically undefined, and Python, like many programming languages, raises an exception to signal the error.
Reasons for ZerodivisionerrorThere, are some reasons that's why Zerodivisionerror Occurs those are following.
In this scenario, a division operation is performed where the denominator is explicitly set to zero, which leads to a ZeroDivisionError.
Python
numerator = 10
denominator = 0
result = numerator / denominator
Output:
Variable Initialization IssueHangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
result = numerator / denominator
ZeroDivisionError: division by zero
If a variable used as the divisor is initialized with a value of zero, subsequent division operations involving that variable will lead to a ZeroDivisionError
denominator = 0
result = 20 / denominator
Output:
Conditional Statements IssueHangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 2, in <module>
result = numerator / denominator
ZeroDivisionError: division by zero
This issue arises when conditional checks are not properly implemented. For example, if a division operation is attempted without ensuring that the denominator is not zero, a ZeroDivisionError can occur.
Python
numerator = 10
denominator = 0
result = numerator / denominator
Output:
Approach/Reason to Solve ZerodivisionerrorHangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
result = numerator / denominator
ZeroDivisionError: division by zero
Below, are the ways to solve the Zerodivisionerror error
In this code example, a division operation is performed only if the denominator is not zero; otherwise, an error message is printed to prevent a ZeroDivisionError.
Python
numerator = 10
denominator = 0
if denominator != 0:
result = numerator / denominator
else:
print("Error: Cannot divide by zero.")
Error: Cannot divide by zero.Using Try-Except Block
In this example below code is perform a division operation (`numerator / denominator`), but if the denominator is zero, it catches the resulting `ZeroDivisionError` and prints an error message stating, "Error: Cannot divide by zero."
Python
numerator = 10
denominator = 0
try:
result = numerator / denominator
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
Error: Cannot divide by zero.Using Conditional Expression
In this approach, the division operation is performed only if the denominator is not zero. If the denominator is zero, it returns an error message instead.
Python
numerator = 10
denominator = 0
result = numerator / denominator if denominator != 0 else "Error: Denominator cannot be zero."
print(result)
Error: Denominator cannot be zero.Using Functions to Abstract the Logic
Another effective way to handle the ZeroDivisionError is by using a function that abstracts the division logic. This allows the code to be reused while ensuring the error is handled cleanly.
Python
def safe_divide(numerator, denominator):
if denominator == 0:
return "Error: Cannot divide by zero."
return numerator / denominator
# Test cases
numerator = 10
denominator = 0
print(safe_divide(numerator, denominator))
numerator = 10
denominator = 2
print(safe_divide(numerator, denominator))
Error: Cannot divide by zero. 5.0
Related articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4