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

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


当前回答

模运算符也可以用于打印字符串(就像在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')

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

其他回答

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

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

4%2 = 0

  2
2|4
 -4
  0


11%3 = 2

  3
3|11
 -9
  2

模运算符也可以用于打印字符串(就像在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')

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

模数-左手操作数除以右手操作数并返回余数。

如果有帮助的话:

1:0> 2%6
=> 2
2:0> 8%6
=> 2
3:0> 2%6 == 8%6
=> true

... 等等。