What is the result of % in Python?

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

    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].

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

    Example 1: 6%2 evaluates to 0 because there's no remainder if 6 is divided by 2 ( 3 times ).

    Example 2: 7%2 evaluates to 1 because there's a remainder of 1 when 7 is divided by 2 ( 3 times ).

    So to summarise that, it returns the remainder of a division operation, or 0 if there is no remainder. So 6%2 means find the remainder of 6 divided by 2.

    0 讨论(0)
  • 2020-11-22 01:23

    It's a modulo operation, except when it's an old-fashioned C-style string formatting operator, not a modulo operation. See here for details. You'll see a lot of this in existing code.

    0 讨论(0)
  • 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].

    0 讨论(0)
  • 2020-11-22 01:28

    Be aware that

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

    even with the brackets results in 6.75 instead of 7 if calculated in Python 3.4.


    And the '/' operator is not that easy to understand, too (python2.7): try...

    - 1/4
    
    1 - 1/4
    

    This is a bit off-topic here, but should be considered when evaluating the above expression :)

    0 讨论(0)
  • 2020-11-22 01:31

    The modulus is a mathematical operation, sometimes described as "clock arithmetic." I find that describing it as simply a remainder is misleading and confusing because it masks the real reason it is used so much in computer science. It really is used to wrap around cycles.

    Think of a clock: Suppose you look at a clock in "military" time, where the range of times goes from 0:00 - 23.59. Now if you wanted something to happen every day at midnight, you would want the current time mod 24 to be zero:

    if (hour % 24 == 0):

    You can think of all hours in history wrapping around a circle of 24 hours over and over and the current hour of the day is that infinitely long number mod 24. It is a much more profound concept than just a remainder, it is a mathematical way to deal with cycles and it is very important in computer science. It is also used to wrap around arrays, allowing you to increase the index and use the modulus to wrap back to the beginning after you reach the end of the array.

    0 讨论(0)
  • 2020-11-22 01:31

    % is modulo. 3 % 2 = 1, 4 % 2 = 0

    / is (an integer in this case) division, so:

    3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
    1 + 4%2 - 1/4 + 6
    1 + 0 - 0 + 6
    7
    
    0 讨论(0)
提交回复
热议问题