轉載:http://www.cnblogs.com/yhleng/p/9223944.html
1.python2和python3除法的最大區別:
python2:
print 500/1000
python2結果:取整數部分,小數並沒有保留
0 Process finished with exit code 0
python3:
print 500/1000
python3結果:得到真實結果,小數保留
0.5 Process finished with exit code 0
2.如果python2想保留小數部分,要怎么做呢?
(1)只需要增加一個導入包.就可以了.並不需要其它操作
from __future__ import division #用於/相除的時候,保留真實結果.小數
增加導入包后的,python2操作:
#coding:utf-8 from __future__ import division print 500/1000
結果:
0.5 Process finished with exit code 0
(2)另一種方式.將除數或被除數兩個其它至少一個轉換成float型:
print float(500)/1000
結果:
0.5 Process finished with exit code 0