当用于除法时,"/"和"//"之间有什么区别? [英] What is the difference between '/' and '//' when used for division?

查看:92
本文介绍了当用于除法时,"/"和"//"之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一个相对于另一个有好处吗?在Python 2中,它们似乎都返回相同的结果:

>>> 6/3
2
>>> 6//3
2

解决方案

在Python 3.x中,5 / 2将返回2.5,而5 // 2将返回2.前者是浮点除法,后者是 底除 ,有时也称为 整数除法 .

在Python 2.2或更高版本的2.x行中,除非执行from __future__ import division,否则整数没有区别,这会导致Python 2.x采取3.x行为.

无论将来如何导入,5.0 // 2都将返回2.0,因为这是该操作的最低分割结果.

您可以在 https中找到详细的说明. ://docs.python.org/whatsnew/2.2.html#pep-238-changing-the-division-operator

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:

>>> 6/3
2
>>> 6//3
2

解决方案

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.

In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.

Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.

You can find a detailed description at https://docs.python.org/whatsnew/2.2.html#pep-238-changing-the-division-operator

这篇关于当用于除法时,"/"和"//"之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆