Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
ZeroDivisionError: float division by zero in Python
Next article icon

ZeroDivisionError: float division by zero in Python

Last Updated : 22 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see what is ZeroDivisionError and also different ways to fix this error.

What is ZeroDivisionError?

A ZeroDivisionError in Python occurs when we try to divide a number by 0. We can't divide a number by 0 otherwise it will raise an error. Let us understand it with the help of an example. In this example, we are dividing a number from 0 and we can see that it raises a ZeroDivisionError.

Python
a = 20 b = 0 print(a / b) 

How to Fix ZeroDivisionError in Python

Below are the following ways by which we can fix ZeroDivisionError in Python:

  • Using conditional statement
  • Using a try/except statement
  • Handling user input

Checking if the value we are dividing by is not 0

The ZeroDivisionError' can be fixed by using a conditional statement in which we can check whether the denominator is zero or not. If the denominator is zero then we print that the Denominator is zerootherwise normal calculation would be performed.

In this another example, we are checking if the denominator is zero or not. In this case, the denominator is 0 but by using if-else condition, we will handle the error.

Python
a = 15.0 b = 5.0  if b == 0:     print("Denominator is zero") else:     print(a / b) 

Output
3.0 

Python fix ZeroDivisionError using a try/except Statement

We have used an exception handler here that sets the result to 0 if such an error occurs, and it then prints the result, which will be 0

Python
a = 16.0 b = 0  try:     res = a / b except ZeroDivisionError:     res = 0  print(res)  

Output
0 

Handling user input with Python try/except Statement

This code takes an input number from the user and attempts to divide 'a' by it. If the user enters a valid number, it calculates and prints the result. However, it has an exception handler that catches two types of errors: ZeroDivisionError (if the user enters 0) and ValueError (if the user enters something that's not a valid number). In case of either error, it prints a message asking the user to enter a number greater than zero.

Python
a = 18.0  try:     b = int(input('Enter a number: '))     ans = a / b     print(f'Answer is: {ans}') except (ZeroDivisionError, ValueError):     print('Please enter a number greater than zero') 

Output:

Screenshot-2023-09-14-at-44239-PM
Division by zero

Next Article
ZeroDivisionError: float division by zero in Python

M

mayankvashxme4
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Zerodivisionerror Integer by Zero in Python
    Python, a versatile and powerful programming language, is widely used for various applications, from web development to data analysis. One common issue that developers often encounter is the ZeroDivisionError, which occurs when attempting to divide a number by zero. In this article, we will explore
    3 min read
    Convert String to Float in Python
    The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif
    2 min read
    How To Fix Valueerror Exceptions In Python
    Python comes with built-in exceptions that are raised when common errors occur. These predefined exceptions provide an advantage because you can use the try-except block in Python to handle them beforehand. For instance, you can utilize the try-except block to manage the ValueError exception in Pyth
    4 min read
    Cannot Convert String To Float in Python
    Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
    3 min read
    Convert hex string to float in Python
    Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences