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

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

    5.0//2 results in 2.0, and not 2 because the return type of the return value from // operator follows python coercion (type casting) rules.

    Python promotes conversion of lower datatype (integer) to higher data type (float) to avoid data loss.

提交回复
热议问题