Python部门

Python supports two division operators: / and //. But Why?

Python支持两个除法运算符:/和//。 但为什么?

Actually, there is a history behind it. There was only one division operator (/) in the initial releases of Python. However, its working was ambiguous. For integers, it used to return integer value by floor division whereas, for floats, it was returning float values. There was no true-division operator in Python.

实际上,背后有一段历史。 在最初的Python版本中,只有一个除法运算符(/)。 但是,其工作模棱两可。 对于整数,它用于按底数划分返回数值,而对于浮点数,则返回浮点值。 Python中没有真除法运算符。

In order to fix this – Python 2.2 introduced a new floor-division operator (//) and allowed developers to migrate their applications to use it wherever they wanted floor integer division. This change was performed under PEP-238. Finally, in Python 3, the division operator (/) started working as a true-division operator.

为了解决这个问题,Python 2.2引入了一个新的楼底除法运算符(//),并允许开发人员迁移其应用程序以在需要楼底整数除法的任何地方使用它。 此更改是在PEP-238下执行的。 最终,在Python 3中,除法运算符(/)开始作为真除法运算符工作。

Let’s look into some simple code snippets to understand Python division operators.

让我们看一些简单的代码片段,以了解Python除法运算符。

Python 2除法运算符 (Python 2 Division Operators)

$ python2.7
Python 2.7.10 (default, Aug 17 2018, 19:45:58) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9/2
4
>>> -9/2
-5
>>> 9.0/2
4.5
>>> -9.0/2
-4.5
>>> 9//2
4
>>> -9//2
-5
>>> 9.0//2
4.0
>>> -9.0//2
-5.0
>>>

Note that if you are on Python 2.1 or lesser version then // will not work.

请注意,如果您使用的是Python 2.1或更低版本,则//将不起作用。

Python 3除法运算符 (Python 3 Division Operators)

$ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9/2
4.5
>>> -9/2
-4.5
>>> 9.0/2
4.5
>>> -9.0/2
-4.5
>>> 9//2
4
>>> -9//2
-5
>>> 9.0//2
4.0
>>> -9.0//2
-5.0
>>>

Below table shows the output and explanation for better understanding.

下表显示了输出和说明,以使您更好地理解。

Division ExpressionPython 2Python 3Explanation
9/244.5For integers, Python 2 always returns int and returns floor value. Whereas Python 3 returns float value
-9/2-5-4.5Since Python 2 returns floor value, it’s returning -5.
9.0/24.54.5With floats, both Python 2 and Python 3 returns float and their behavior is same.
-9.0/2-4.5-4.5Same as above.
9//244Floor division operator, works same way in both Python 2 and Python 3.
-9//2-5-5
9.0//24.04.0
-9.0//2-5.0-5.0
除法表达式 Python 2 Python 3 说明
9/2 4 4.5 对于整数,Python 2始终返回int并返回下限值。 而Python 3返回浮点值
-9/2 -5 -4.5 由于Python 2返回的是底值,因此返回的是-5。
9.0 / 2 4.5 4.5 使用浮点数时,Python 2和Python 3都返回浮点数,并且它们的行为相同。
-9.0 / 2 -4.5 -4.5 同上。
9 // 2 4 4 楼层除法运算符在Python 2和Python 3中的工作方式相同。
-9 // 2 -5 -5
9.0 // 2 4.0 4.0
-9.0 // 2 -5.0 -5.0

结论 (Conclusion)

If you are on Python 2 and planning to migrate to Python 3 then please look at the way your division operators are being used. If needed, change them to use floor division operator or else leave them to work as a true division operator.

如果您使用的是Python 2,并计划迁移到Python 3,请查看使用除法运算符的方式。 如果需要,请更改他们以使用楼层除法运算符,否则让他们充当真正的除法运算符。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/25012/python-division

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值