您的位置:首页 > 编程语言 > Python开发

python 除法 /与//在2.7*和3.*版本的区别

2017-03-06 11:43 316 查看
up vote
down
voteaccepted

In Python 3.0, 
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 behavior of 3.0

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 helps to clarify for the Python 2.x line, 
/
 is
neither floor division nor true division. The current accepted answer is not clear on this. 
/
 is
floor division when both args are int, but is true division when either or both of the args are float.

The above tells a lot more truth, and is a lot more clearer than the 2nd paragraph in the accepted answer.

from: http://stackoverflow.com/questions/183853/in-python-2-what-is-the-difference-between-and-when-used-for-division

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python