Division Operators in Python
Consider the below statements in Python.
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. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python3
# A Python program to demonstrate the use of# "//" for integersprint (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++)
Note:
The “//” operator is used to return the closest integer value which is less than or equal to a specified expression or value. So from the above code, 5//2 returns 2. You know that 5/2 is 2.5, the closest integer which is less than or equal is 2[5//2].( it is inverse to the normal maths, in normal maths the value is 3).
Python3
# A Python program to demonstrate use of# "/" for floating point numbersprint (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.
Python3
# A Python program to demonstrate use of# "//" for both integers and floating pointsprint (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

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.


