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

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


当前回答

我发现掌握模运算符(%)最简单的方法是通过长除法。它是余数,在确定一个数是偶数还是奇数时很有用:

4%2 = 0

  2
2|4
 -4
  0


11%3 = 2

  3
3|11
 -9
  2

其他回答

这是一个模运算 http://en.wikipedia.org/wiki/Modulo_operation

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

根据运算的顺序,结果是

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

(1) + (0) - (0) + 6

7

1/4=0,因为我们在做整数运算。

在大多数语言中%表示模数。Python也不例外。

%是模。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)请参阅文档。

模运算符也可以用于打印字符串(就像在C中一样),定义在谷歌https://developers.google.com/edu/python/strings上。

      # % operator
  text = "%d little pigs come out or I'll %s and %s and %s" % (3, 'huff', 'puff', 'blow down')

这似乎有点离题,但它肯定会帮助到某人。