Python 2 中“//”和“/”的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37082597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "//" and "/" in Python 2
提问by Ryan Haining
Python 2 has two division operators: /
and //
. The following output:
Python 2 有两个除法运算符:/
和//
。以下输出:
========================================= RESTART: Shell =========================================
>>> for x in range(10):
for y in range(1, 10):
print x,'//',y,'=',x//y
print x,'/',y,'=',x/y
print
0 // 1 = 0
0 / 1 = 0
0 // 2 = 0
0 / 2 = 0
0 // 3 = 0
0 / 3 = 0
0 // 4 = 0
0 / 4 = 0
0 // 5 = 0
0 / 5 = 0
0 // 6 = 0
0 / 6 = 0
0 // 7 = 0
0 / 7 = 0
0 // 8 = 0
0 / 8 = 0
0 // 9 = 0
0 / 9 = 0
1 // 1 = 1
1 / 1 = 1
1 // 2 = 0
1 / 2 = 0
1 // 3 = 0
1 / 3 = 0
1 // 4 = 0
1 / 4 = 0
1 // 5 = 0
1 / 5 = 0
1 // 6 = 0
1 / 6 = 0
1 // 7 = 0
1 / 7 = 0
1 // 8 = 0
1 / 8 = 0
1 // 9 = 0
1 / 9 = 0
2 // 1 = 2
2 / 1 = 2
2 // 2 = 1
2 / 2 = 1
2 // 3 = 0
2 / 3 = 0
2 // 4 = 0
2 / 4 = 0
2 // 5 = 0
2 / 5 = 0
2 // 6 = 0
2 / 6 = 0
2 // 7 = 0
2 / 7 = 0
2 // 8 = 0
2 / 8 = 0
2 // 9 = 0
2 / 9 = 0
3 // 1 = 3
3 / 1 = 3
3 // 2 = 1
3 / 2 = 1
3 // 3 = 1
3 / 3 = 1
3 // 4 = 0
3 / 4 = 0
3 // 5 = 0
3 / 5 = 0
3 // 6 = 0
3 / 6 = 0
3 // 7 = 0
3 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= 2
9 // 5 = 1
9 / 5 = 1
9 // 6 = 1
9 / 6 = 1
9 // 7 = 1
9 / 7 = 1
9 // 8 = 1
9 / 8 = 1
9 // 9 = 1
9 / 9 = 1
>>>
proves (almost?) all the time a/b
equals a//b
. Is there any time that it isn't? If not, why did Python 2 include two operators that do the same thing?
证明(几乎?)一直a/b
等于a//b
。有什么时候不是吗?如果不是,为什么 Python 2 包含两个做同样事情的运算符?
回答by Ryan Haining
//
is the floored-division operator in Python. The result will be easily noticeable if you try dividing floating point values
//
是 Python 中的地板除法运算符。如果您尝试对浮点值进行除法,结果将很容易被注意到
# Python 2
>>> 10.0 / 3
3.3333333333333335
>>> 10.0 // 3
3.0
In Python2, dividing two ints uses integer division, which ends up getting you the same thing as floored division. However, you can still use //
to get a floored result of floating point division.
在 Python2 中,将两个整数相除使用整数除法,最终得到与地板除法相同的结果。但是,您仍然可以使用//
来获得浮点除法的地板结果。
In Python3, dividing two ints results in a float, but using //
acts as integer division.
在Python3 中,将两个整数相除会产生一个浮点数,但 using//
充当整数除法。
# Python3
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3
If you are (still) working in Python2, but want to someday convert to Python3, you should always use //
when dividing two ints, or use from __future__ import division
to get the Python3 behavior in Python2
如果您(仍然)在 Python2 中工作,但想有一天转换为 Python3,则应始终//
在除以两个整数时使用,或用于from __future__ import division
在 Python2 中获取 Python3 行为
回答by MrGeek
In python 2.7, to do real division you'll need to import division from a module named future:
在 python 2.7 中,要进行真正的除法,您需要从名为 future 的模块导入除法:
from __future__ import division
Then, the /
will be the real (floating) division, i.e.:
然后,/
将是真正的(浮动)除法,即:
15 / 4 => 3.75
And the //
will be the integer division (the integer part of the real division), i.e.:
并且//
将是整数除法(实数除法的整数部分),即:
15 // 4 => 3