品易云推流 关闭
文章详情页
文章 > Python常见问题 > python division如何取整

python division如何取整

头像

爱喝马黛茶的安东尼

2019-11-01 10:35:003013浏览 · 0收藏 · 0评论

1、在python2 中导入division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/"操作符执行的只能是整除,也就是取整数,只有当我们导入division(精确算法)以后,"/"执行的才是精确算法。

如:

#python 2.7.6
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
#导入前
>>> 1/2
0
>>> 10/3
3
#导入后
>>> from __future__ import division
>>> 1/2
0.5
>>> 10/3
3.3333333333333335
#导入后如果要去整数,加'//'
>>> 10//3
3

2、但是在python3中已经支持了精确算法,所以无需再导入division(精确算法):

如:

#python3.4.4
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 1/2
0.5
>>> 10/3
3.3333333333333335
#如果需要取整数,加'//'
>>> 10//3
3

python学习网,免费的在线学习python平台,欢迎关注!

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图