What is the result of % in Python?

前端 未结 19 1722
粉色の甜心
粉色の甜心 2020-11-22 00:57

What does the % in a calculation? I can\'t seem to work out what it does.

Does it work out a percent of the calculation for example: 4 % 2

19条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 01:28

    Modulus operator, it is used for remainder division on integers, typically, but in Python can be used for floating point numbers.

    http://docs.python.org/reference/expressions.html

    The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [2].

提交回复
热议问题