What is the difference between '/' and '//' when used for division?

后端 未结 13 2184
孤独总比滥情好
孤独总比滥情好 2020-11-21 07:23

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:

>>> 6/3
2
>>> 6//3
2
13条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 07:40

    • // is floor division, it will always give you the floor value of the result.
    • And the other one / is the floating-point division.

    Followings are the difference between / and //; I have run these arithmetic operations in Python 3.7.2

    >>> print (11 / 3)
    3.6666666666666665
    
    >>> print (11 // 3)
    3
    
    >>> print (11.3 / 3)
    3.7666666666666667
    
    >>> print (11.3 // 3)
    3.0
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题