博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python部门
阅读量:2530 次
发布时间:2019-05-11

本文共 3048 字,大约阅读时间需要 10 分钟。

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 value by floor division whereas, for floats, it was returning 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 . Finally, in Python 3, the division operator (/) started working as a true-division operator.

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

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

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

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

$ python2.7Python 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 darwinType "help", "copyright", "credits" or "license" for more information.>>> 9/24>>> -9/2-5>>> 9.0/24.5>>> -9.0/2-4.5>>> 9//24>>> -9//2-5>>> 9.0//24.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.7Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> 9/24.5>>> -9/2-4.5>>> 9.0/24.5>>> -9.0/2-4.5>>> 9//24>>> -9//2-5>>> 9.0//24.0>>> -9.0//2-5.0>>>

Below table shows the output and explanation for better understanding.

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

Division Expression Python 2 Python 3 Explanation
9/2 4 4.5 For integers, Python 2 always returns int and returns floor value. Whereas Python 3 returns float value
-9/2 -5 -4.5 Since Python 2 returns floor value, it’s returning -5.
9.0/2 4.5 4.5 With floats, both Python 2 and Python 3 returns float and their behavior is same.
-9.0/2 -4.5 -4.5 Same as above.
9//2 4 4 Floor division operator, works same way in both Python 2 and Python 3.
-9//2 -5 -5
9.0//2 4.0 4.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,请查看使用除法运算符的方式。 如果需要,请更改他们以使用楼层除法运算符,否则让他们充当真正的除法运算符。

. 检出完整的python脚本和更多Python示例。

翻译自:

转载地址:http://cmmzd.baihongyu.com/

你可能感兴趣的文章
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>