zoukankan      html  css  js  c++  java
  • python——“/”运算符和“//”运算符的区别

    首先先看单斜杆的用法:举几个例子

    >>> print(5/3),type(5/3)
    1.6666666666666667
    (None, <class 'float'>)

    >>> print(6/3),type(6/3)
    2.0
    (None, <class 'float'>)

    >>> print 5.0/3,type(5.0/3)
    1.66666666667 <type 'float'>
    >>> print 5/3.0,type(5/3.0)
    1.66666666667 <type 'float'>
    >>> print 5.0/3.0,type(5.0/3.0)
    1.66666666667 <type 'float'>

     可以看出,无论数值是不是能整除,或者说A/B中A和B是不是int型,单斜杠的作用全是float型,结果是保留若干位的小数,是我们正常思维中的除法运算

    在看看双斜杆的例子:

    >>> print 5//3,type(5//3)
    1 <type 'int'>
    >>> print 5.0//3,type(5.0//3)
    1.0 <type 'float'>
    >>> print 5//3.0,type(5//3.0)
    1.0 <type 'float'>
    >>> print 5.0//3.0,type(5.0//3.0)
    1.0 <type 'float'>
    >>> 

    可以看出,在A//B的返回类型取决与A和B的数据类型,只有A和B都为int型时结果才是int(此时表示两数正除取商) 

    //取的是结果的最小整数,而/取得是实际的除法结果,这就是二者的主要区别啦

  • 相关阅读:
    bzoj2876 [Noi2012]骑行川藏
    关于线性基的一些理解
    bzoj2115 [Wc2011] Xor
    bzoj2884 albus就是要第一个出场
    bzoj2460 [BeiJing2011]元素
    bzoj2005 [Noi2010]能量采集
    关于积性函数的一些理解
    bzoj4300 绝世好题
    Servlet—文件上传
    JNDI—目录接口名
  • 原文地址:https://www.cnblogs.com/carlber/p/9368320.html
Copyright © 2011-2022 走看看