您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python 3中的除法结果与Python 2中的除法结果不同

Python 3中的除法结果与Python 2中的除法结果不同

/是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

python 2022/1/1 18:44:03 有285人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶