Division Operators in Python

What are division operators in Python?


If you tried to equally divide an unequal amount of candies, you would be left with a few remaining candies. In division, those that remain are called remainders.

What do we do with this remainder? Do we display the number as a floating point or do we round it down to the closest whole number?

Python has an answer with its division operators.

Types of division operators in Python

In Python, there are two types of division operators:

  • /: Divides the number on its left by the number on its right and returns a floating point value.
  • //: Divides the number on its left by the number on its right, rounds down the answer, and returns a whole number.

The illustration below shows how this works. 5/2 5//2 2.5 2

Examples

Now that we know how the operators work and what kind of division each operator performs; let’s look at an example of division in Python.1234567891011121314151617181920212223242526# Dividing an integer by an integerprint(“The answer for 5/2: “)print(5/2)print(“The answer for 5//2: “)print(5//2)print(“\n”)# Dividing an integer by a floatprint(“The answer for 5/2.75: “)print(5/2.75)print(“The answer for 5//2.75: “)print(5//2.75)print(“\n”)#Dividing a float by an integerprint(“The answer for 5.5/2: “)print(5.5/2)print(“The answer for 5.5//2: “)print(5.5//2)print(“\n”)#Dividing a float by an floatprint(“The answer for 5.5/2.5: “)print(5.5/2.5)print(“The answer for 5.5//2.5: “)print(5.5//2.5)

Division Operators in Python
Consider the below statements in Python.

filter_none
edit
play_arrow

brightness_4

A Python program to demonstrate the use of

“//” for integers

print (5//2)
print (-5//2)
Output:

2
-3
The first output is fine, but the second one may be surprised if we are coming Java/C++ world. In Python, the “//” operator works as a floor division for integer and float arguments. However, the operator / returns a float value if one of the arguments is a float (this is similar to C++)

filter_none
edit
play_arrow

brightness_4

A Python program to demonstrate use of

“/” for floating point numbers

print (5.0/2)
print (-5.0/2)
Output:

2.5
-2.5
The real floor division operator is “//”. It returns floor value for both integer and floating point arguments.

filter_none
edit
play_arrow

brightness_4

A Python program to demonstrate use of

“//” for both integers and floating points

print (5//2)
print (-5//2)
print (5.0//2)
print (-5.0//2)
Output:

2
-3
2.0
-3.0
See this for example.

This article is contributed by Arpit Agrawal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

Leave a Reply

Your email address will not be published. Required fields are marked *

Psst. Yeah, you. Want some marketing secrets "gurus" never share?

Get them every month from your favorite teen entrepreneur - for free. After all, sharing is caring. ❤️🚀

🔒 No spam. I promise. 🔒