/
是Python 3中的另一个运算符;在Python 2中,/
当将其应用于2个整数操作数时会更改行为,并返回下限除法的结果:
>>> 3/2 # two integer operands
1
>>> 3/2.0 # one operand is not an integer, float division is used
1.5
加:
from __future__ import division
代码的顶部以/
在Python 2中使用浮点除法,或用于//
强制Python 3使用整数除法:
>>> from __future__ import division
>>> 3/2 # even when using integers, true division is used
1.5
>>> 3//2.0 # explicit floor division
1.0
使用这些技术中的任何一种均可在Python 2.2或更高版本中使用。有关更改原因的详细信息,请参阅PEP 238。