The % (modulo) operator yields the remainder from the division of the first argument by the second. (%(modulo)运算符从第一个参数除以第二个参数得到余数。) The numeric arguments are first converted to a common type. (数字参数首先转换为通用类型。) A zero right argument raises the ZeroDivisionError exception. (零右参数会引发ZeroDivisionError异常。) The arguments may be floating point numbers, eg, 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); (参数可以是浮点数,例如,3.14%0.7等于0.34(因为3.14等于4 * 0.7 + 0.34。)模运算符总是产生与第二个操作数(或零)具有相同符号的结果;) the absolute value of the result is strictly smaller than the absolute value of the second operand [2]. (结果的绝对值严格小于第二个操作数的绝对值[2]。)
Taken from http://docs.python.org/reference/expressions.html (取自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 ). (示例1: 6%2
评估为0
因为如果6除以2(3次)则没有余数。)
Example 2 : 7%2
evaluates to 1
because there's a remainder of 1
when 7 is divided by 2 ( 3 times ). (示例2 : 7%2
评估为1
因为当7除以2(3次)时,余数为1
。)
So to summarise that, it returns the remainder of a division operation, or 0
if there is no remainder. (总而言之,它返回除法运算的余数,如果没有余数则返回0
。) So 6%2
means find the remainder of 6 divided by 2. (所以6%2
意味着找到6的余数除以2。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…