python division如何取整

2025-12-02 0 89,810

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平台,欢迎关注!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 常见问题 python division如何取整 https://www.zhiup.top/8428.html

相关