Python中%的结果是什么? [英] What is the result of % in Python?

查看:118
本文介绍了Python中%的结果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%在计算中是什么?我似乎无法弄清楚它的作用.

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

例如,它计算出计算的百分比吗:4 % 2显然等于0.如何?

Does it work out a percent of the calculation for example: 4 % 2 is apparently equal to 0. How?

推荐答案

%(模)运算符从第一个参数除以第二个参数得出余数.首先将数字参数转换为通用类型.零权限参数引发ZeroDivisionError异常.参数可以是浮点数,例如3.14%0.7等于0.34(因为3.14等于4 * 0.7 + 0.34.)模运算符始终产生与第二个操作数具有相同符号的结果(或为零);结果的绝对值严格小于第二个操作数[2]的绝对值.

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

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

示例1: 6%2的计算结果为0,因为如果将6除以2(3次),则没有余数.

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

示例2 :7%2的计算结果为1,因为当7除以2(3次)时,1的余数仍然存在.

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

总而言之,它返回除法运算的余数,如果没有余数,则返回0.因此6%2表示找到6的余数除以2.

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.

这篇关于Python中%的结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆