Consider below statements in Python 2.7
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++)
The real floor division operator is “//”. It returns floor value for both integer and floating point arguments.
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)
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
- Inplace vs Standard Operators in Python
- Keywords in Python | Set 1
- Operator Functions in Python | Set 2
- How to check if a string is a valid keyword in Python?
- Operator Functions in Python | Set 1
- Any & All in Python
- Ternary Operator in Python
- Loops and Control Statements (continue, break and pass) in Python
- Python | end parameter in print()
- What is the maximum possible value of an integer in Python ?
- Important differences between Python 2.x and Python 3.x with examples
- Interesting facts about strings in Python | Set 1
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)