VOOZH about

URL: https://www.geeksforgeeks.org/python/zerodivisionerror-integer-by-zero-in-python/

⇱ Zerodivisionerror Integer by Zero in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Zerodivisionerror Integer by Zero in Python

Last Updated : 7 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 Zerodivisionerror

There, are some reasons that's why Zerodivisionerror Occurs those are following.

  • Direct Division by Zero
  • Variable Initialization Issue
  • Conditional Statements Issue

Direct Division by Zero

In this scenario, a division operation is performed where the denominator is explicitly set to zero, which leads to a ZeroDivisionError.

Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
result = numerator / denominator
ZeroDivisionError: division by zero

Variable Initialization Issue

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

Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 2, in <module>
result = numerator / denominator
ZeroDivisionError: division by zero

Conditional Statements Issue

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.

Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
result = numerator / denominator
ZeroDivisionError: division by zero

Approach/Reason to Solve Zerodivisionerror

Below, are the ways to solve the Zerodivisionerror error

  • Using if-else Condition
  • Using Try-Except Block
  • Using Conditional Expression

Using if-else Condition

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.


Output
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."


Output
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.


Output
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.


Output
Error: Cannot divide by zero.
5.0

Related articles:

Comment