Division Operators in Python
Consider below statements in Python 2.7
# A Python 2.7 program to demonstrate use of # "/" for integers print 5/2print -5/2 |
Output:
2 -3
First output is fine, but the second one may be surprising if we are coming Java/C++ world. In Python 2.7, the “/” operator works as a floor division for integer arguments. However, the operator / returns a float value if one of the arguments is a float (this is similar to C++)
# A Python 2.7 program to demonstrate use of # "/" for floating point numbers print 5.0/2print -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.
# A Python 2.7 program to demonstrate use of # "//" for both integers and floating points print 5//2print -5//2print 5.0//2print -5.0//2 |
Output:
2 -3 2.0 -3.0
How about Python 3?
Here is another surprise, In Python 3, ‘/’ operator does floating point division for both int and float arguments.
# A Python 3 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.5 -2.5 2.5 -2.5
Behavior of “//” is same Python 2.7 and Python 3. 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
Recommended Posts:
- Basic Operators in Python
- Inplace vs Standard Operators in Python
- Python Membership and Identity Operators | in, not in, is, is not
- Increment and Decrement Operators in Python
- Chaining comparison operators in Python
- Logical Operators on String in Python
- Python | Solve given list containing numbers and arithmetic operators
- Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
- Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
- Division without using '/' operator
- Program to compute division upto n decimal places
- Operators in C / C++
- Operators in Java
- What are the operators that can be and cannot be overloaded in C++?
- Basic Operators in Java



