• 技术文章 >常见问题 >Python常见问题

    python division如何取整

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-11-01 10:35:00原创3425

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

    如:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    #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(精确算法):

    如:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

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

    专题推荐:python division 取整
    上一篇:python函数没有返回值吗 下一篇:python wheel镜像怎么安装

    相关文章推荐

    • python中如何取整数• python怎么对数字进行取整• python如何对一个数向上取整• python中如何取整

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网