Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:
>>> 6/3
2
>>> 6//3
2
In Python 3.x, 5 / 2
will return 2.5
and 5 // 2
will return 2
. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division
, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2
will return 2.0
since that's the floor division result of the operation.
You can find a detailed description at https://docs.python.org/whatsnew/2.2.html#pep-238-changing-the-division-operator
//
implements "floor division", regardless of your type. So
1.0/2.0
will give 0.5
, but both 1/2
, 1//2
and 1.0//2.0
will give 0
.
See https://docs.python.org/whatsnew/2.2.html#pep-238-changing-the-division-operator for details
/ --> Floating point division
// --> Floor division
Lets see some examples in both python 2.7 and in Python 3.5.
Python 2.7.10 vs. Python 3.5
print (2/3) ----> 0 Python 2.7
print (2/3) ----> 0.6666666666666666 Python 3.5
Python 2.7.10 vs. Python 3.5
print (4/2) ----> 2 Python 2.7
print (4/2) ----> 2.0 Python 3.5
Now if you want to have (in python 2.7) same output as in python 3.5, you can do the following:
Python 2.7.10
from __future__ import division
print (2/3) ----> 0.6666666666666666 #Python 2.7
print (4/2) ----> 2.0 #Python 2.7
Where as there is no differece between Floor division in both python 2.7 and in Python 3.5
138.93//3 ---> 46.0 #Python 2.7
138.93//3 ---> 46.0 #Python 3.5
4//3 ---> 1 #Python 2.7
4//3 ---> 1 #Python 3.5
As everyone has already answered, //
is floor division.
Why this is important is that //
is unambiguously floor division, in all Python versions from 2.2, including Python 3.x versions.
The behavior of /
can change depending on:
__future__
import or not (module-local)-Q old
or -Q new
Python 2.7 and other upcoming version of python:
/
)Divides left hand operand by right hand operand
Example: 4 / 2 = 2
//
)The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):
Examples: 9//2 = 4
and 9.0//2.0 = 4.0
, -11//3 = -4
, -11.0//3 = -4.0
Both /
Division and //
floor division operator are operating in similar fashion.
The above answers are good. I want to add another point. Up to some values both of them result in the same quotient. After that floor division operator (//
) works fine but not division (/
) operator:
>>> int(755349677599789174 / 2) # wrong answer
377674838799894592
>>> 755349677599789174 // 2 # correct answer
377674838799894587
//
is floor division, it will always give you the floor value of the result./
is the floating-point division.Followings are the difference between /
and //
;
I have run these arithmetic operations in Python 3.7.2
>>> print (11 / 3)
3.6666666666666665
>>> print (11 // 3)
3
>>> print (11.3 / 3)
3.7666666666666667
>>> print (11.3 // 3)
3.0
Python 3.x Clarification
Just to complement some previous answers.
It is important to remark that:
a // b
Is floor division. As in:
math.floor(a/b)
Is not int division. As in:
int(a/b)
Is not round to 0 float division. As in:
round(a/b,0)
As a consequence, the way of behaving is different when it comes to positives an negatives numbers as in the following example:
1 // 2 is 0, as in:
math.floor(1/2)
-1 // 2 is -1, as in:
math.floor(-1/2)
Operation Result Notes x / y
quotient of x and y x // y
floored quotient of x and y (1) Notes:
- Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity:
1//2
is0
,(-1)//2
is-1
,1//(-2)
is-1
, and(-1)//(-2)
is0
.
Operation Result Notes x / y
quotient of x and y (1) x // y
(floored) quotient of x and y (4)(5) Notes:
1. For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.
4. Deprecated since version 2.3: The floor division operator, the modulo operator, and the divmod()
function are no longer defined for complex numbers. Instead, convert to a floating point number using theabs()
function if appropriate.5. Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int.
//
operator to do integer division. – Sriram