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

后端 未结 13 2149
孤独总比滥情好
孤独总比滥情好 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:57

    Python 2.7 and other upcoming version of python:

    • Division (/)

    Divides left hand operand by right hand operand

    Example: 4 / 2 = 2

    • Floor Division (//)

    The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):

    Examples: 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0

    Both / Division and // floor division operator are operating in similar fashion.

提交回复
热议问题