What is the result of % in Python?

前端 未结 19 1697
粉色の甜心
粉色の甜心 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:10

    It's a modulo operation http://en.wikipedia.org/wiki/Modulo_operation

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

    So with order of operations, that works out to

    (3+2+1-5) + (4%2) - (1/4) + 6

    (1) + (0) - (0) + 6

    7

    The 1/4=0 because we're doing integer math here.

提交回复
热议问题