Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - Python中%的结果是什么?(What is the result of % in Python?)

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 is apparently equal to 0. How? (例如,它是否计算出百分比的计算结果: 4 % 2显然等于0.如何?)

  ask by orange translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ). (示例27%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。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...