首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python from __future__ import division的作用

python from __future__ import division的作用

作者头像
狼啸风云
修改2022-09-03 19:55:32
修改2022-09-03 19:55:32
2.4K0
举报

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

代码语言:javascript
复制
#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

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

代码语言:javascript
复制
#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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档