%在计算中是什么?我不知道它是做什么的。
它能算出计算的百分比吗例如4% 2显然等于0。如何?
%在计算中是什么?我不知道它是做什么的。
它能算出计算的百分比吗例如4% 2显然等于0。如何?
当前回答
Python -基本操作符 http://www.tutorialspoint.com/python/python_basic_operators.htm
模数-左手操作数除以右手操作数并返回余数
A = 10, b = 20
B % a = 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 -基本操作符 http://www.tutorialspoint.com/python/python_basic_operators.htm
模数-左手操作数除以右手操作数并返回余数
A = 10, b = 20
B % a = 0
请注意
(3 +2 + 1 - 5) + (4 % 2) - (1/4) + 6
即使使用括号,在Python 3.4中计算的结果也是6.75,而不是7。
'/'操作符也不是那么容易理解(python2.7): try…
- 1/4
1 - 1/4
这有点离题,但在计算上面的表达式时应该考虑:)
模运算符也可以用于打印字符串(就像在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')
这似乎有点离题,但它肯定会帮助到某人。