python培训之 division如何取整

发布时间:2022-09-15 09:50:48 人气:145 作者:多测师

  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.

python培训之 division如何取整

  #导入前

  >>> 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培训之 division如何取整,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线