%在计算中是什么?我不知道它是做什么的。

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


当前回答

%是模。3% 2 = 1,4% 2 = 0

/是除法(在本例中为整数),因此:

3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
1 + 4%2 - 1/4 + 6
1 + 0 - 0 + 6
7

其他回答

与许多类c语言一样,它是余数运算或模运算。有关数字类型(int, float, long, complex)请参阅文档。

模量是一种数学运算,有时被描述为“时钟算术”。我发现把它简单地描述为一个余数是会误导和令人困惑的,因为它掩盖了它在计算机科学中被如此广泛使用的真正原因。它实际上是用来环绕循环的。

想象一个时钟:假设你看一个“军事”时间的时钟,时间范围从0点到23点59分。现在,如果你想在每天的午夜发生一些事情,你会希望当前时间mod 24为零:

如果(小时% 24 == 0):

你可以想象历史上所有的小时都围绕着24小时循环而今天的小时就是这个无限长的数字对24取余。它是一个比余数更深刻的概念,它是一种处理循环的数学方法,在计算机科学中非常重要。它还用于环绕数组,允许您增加索引,并在到达数组的末尾后使用模量返回到开头。

def绝对(c): 如果c > = 0: 返回c 其他: 返回c * 1 x=int(input("输入值")) 一个绝对(x) = print ()

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倍)。

例2:7%2的结果是1,因为当7除以2(3次)时,余数是1。

总结一下,它返回除法运算的余数,如果没有余数,则返回0。6%2意味着6的余数除以2。

它是一个模运算,除了它是一个老式的c风格字符串格式化操作符,而不是一个模运算符。详情请看这里。您将在现有代码中看到很多这样的代码。