问题:用于除法时,“ /”和“ //”之间有什么区别?
将一个使用在另一个上是否有好处?在Python 2中,它们似乎都返回相同的结果:
>>> 6/3
2
>>> 6//3
2
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
回答 0
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
回答 1
Python 2.x澄清:
为了阐明Python 2.x系列,/
既不是楼层划分也不是真正的划分。当前接受的答案尚不清楚。
/
是地板除法时都 ARG游戏int
,但真正当师之一或两者的参数的个数是float
。
上面讲了更多的事实,并且比接受的答案中的第二段更清楚。
Python 2.x Clarification:
To clarify for the Python 2.x line, /
is neither floor division nor true division. The current accepted answer is not clear on this.
/
is floor division when both args are int
, but is true division when either or both of the args are float
.
The above tells more truth, and is more clear than the 2nd paragraph in the accepted answer.
回答 2
回答 3
// -浮点除法
// ->楼层划分
让我们在python 2.7和python 3.5中查看一些示例。
Python 2.7.10和Python 3.5
print (2/3) ----> 0 Python 2.7
print (2/3) ----> 0.6666666666666666 Python 3.5
Python 2.7.10和Python 3.5
print (4/2) ----> 2 Python 2.7
print (4/2) ----> 2.0 Python 3.5
现在,如果您希望在python 2.7中具有与python 3.5相同的输出,则可以执行以下操作:
Python 2.7.10
from __future__ import division
print (2/3) ----> 0.6666666666666666 #Python 2.7
print (4/2) ----> 2.0 #Python 2.7
在python 2.7和python 3.5中,Floor划分之间没有区别
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
/ –> 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
回答 4
正如大家已经回答的那样,//
是楼层划分。
之所以如此重要,是因为//
在所有2.2版本的Python版本中,包括Python 3.x版本,都明确地进行了地域划分。
的行为/
可能会有所不同,具体取决于:
- 是否有效
__future__
导入(模块本地)
- Python命令行选项,
-Q old
或者-Q new
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:
- Active
__future__
import or not (module-local)
- Python command line option, either
-Q old
or -Q new
回答 5
>>> print 5.0 / 2
2.5
>>> print 5.0 // 2
2.0
>>> print 5.0 / 2
2.5
>>> print 5.0 // 2
2.0
回答 6
Python 2.7和其他即将发布的python版本:
将左操作数除以右操作数
例: 4 / 2 = 2
操作数的除法,结果是除掉小数点后的数字的商。但是,如果其中一个操作数是负数,则结果是下限的,即从零开始舍入(朝负无穷大):
例如:9//2 = 4
和9.0//2.0 = 4.0
,-11//3 = -4
,-11.0//3 = -4.0
这两个/
师和//
地板除法运算的操作以类似的方式。
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.
回答 7
The double slash, //
, is floor division:
>>> 7//3
2
回答 8
//
是底数除法,它将始终为您提供结果的整数底数。另一个是“常规”划分。
//
is floor division, it will always give you the integer floor of the result. The other is ‘regular’ division.
回答 9
等式的答案四舍五入为下一个较小的整数,或者以.0作为小数点进行浮点运算。
>>>print 5//2
2
>>> print 5.0//2
2.0
>>>print 5//2.0
2.0
>>>print 5.0//2.0
2.0
The answer of the equation is rounded to the next smaller integer or float with .0 as decimal point.
>>>print 5//2
2
>>> print 5.0//2
2.0
>>>print 5//2.0
2.0
>>>print 5.0//2.0
2.0
回答 10
上面的答案是好的。我想补充一点。最多两个值都导致相同的商。在该楼层之后,除法运算符(//
)正常工作,但除法运算符()无效/
。
- > int(755349677599789174/2)
- > 377674838799894592 #wrong answer
- > 755349677599789174 //2
- > 377674838799894587 #correct answer
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)
- > 377674838799894592 #wrong answer
- > 755349677599789174 //2
- > 377674838799894587 #correct answer
回答 11
5.0//2
会导致2.0
,而不是2
因为运算符返回值的返回类型//
遵循python强制(类型转换)规则。
Python促进将较低数据类型(整数)转换为较高数据类型(浮点数),以避免数据丢失。
5.0//2
results in 2.0
, and not 2
because the return type of the return value from //
operator follows python coercion (type casting) rules.
Python promotes conversion of lower datatype (integer) to higher data type (float) to avoid data loss.
回答 12
//
是底数划分,它将始终为您提供结果的底数。
- 另一个
/
是浮点除法。
以下是之间的差/
和//
; 我已经在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
//
is floor division, it will always give you the floor value of the result.
- And the other one
/
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