Python 中的模数和运算顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4729025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Modulo and order of operation in Python
提问by dartdog
In Zed Shaw's Learn Python the Hard Way(page 15-16), he has an example exercise
在 Zed Shaw 的Learn Python the Hard Way(第 15-16 页)中,他有一个示例练习
100 - 25 * 3 % 4
the result is 97 (try it!)
结果是 97(试试吧!)
I cannot see the order of operations that could do this..
我看不到可以执行此操作的操作顺序..
100 - 25 = 75
3 % 4 = 0
or (100-25*3) =225 % 4 = ??? but anyhow not 97 I don't think...
100 - 25 = 75
3 % 4 = 0
或 (100-25*3) =225 % 4 = ??? 但无论如何不是97我不认为......
A similar example is 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6which yields 7
一个类似的例子是3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6产生 7
In what order are the operations done?
操作是按什么顺序进行的?
采纳答案by Sven Marnach
For the first example: *and %take precedence over -, so we first evaluate 25 * 3 % 4. *and %have the same priority and associativity from left to right, so we evaluate from left to right, starting with 25 * 3. This yields 75. Now we evaluate 75 % 4, yielding 3. Finally, 100 - 3is 97.
对于第一个示例:*并且%优先于-,因此我们首先评估25 * 3 % 4。 *并且%从左到右具有相同的优先级和结合性,所以我们从左到右求值,从 开始25 * 3。这产生75. 现在我们评估75 % 4,屈服3。最后,100 - 3是97。
回答by chauncey
Multiplication >> mod >> subtraction
乘法 >> 模 >> 减法
In [3]: 25 * 3
Out[3]: 75
In [4]: 75 % 4
Out[4]: 3
In [5]: 100 - 3
Out[5]: 97
Multiplication and modulo operator have the same precedence, so you evaluate from left to right for this example.
乘法和模运算符具有相同的优先级,因此您在此示例中从左到右进行计算。
回答by duffymo
Here's how it goes:
这是它的过程:
'*' and '%' have the same precendence, so evaluate those from left to right.
'*' 和 '%' 具有相同的优先级,因此从左到右评估它们。
- 25*3 = 75
- 75 % 4 = 3 (4*18 = 72; remainder is 3)
- 100 - 3 = 97
- 25*3 = 75
- 75 % 4 = 3(4*18 = 72;余数为 3)
- 100 - 3 = 97
Q.E.D.
QED
回答by Mike
Original problem: 100 - 25 * 3 % 4
原问题: 100 - 25 * 3 % 4
Actually, evaluating 25 * 3and taking 75% of 4 is incorrect, and happened to work out conveniently for this problem.
其实,求取25 * 34的75%是不正确的,正好解决了这个问题。
What the % in python actually is is a modulus operator where x % y gives the remainder of x / y. In this case, what happened was that 75 / 4is 18, with a remainder of 3, which is why 100 - 3 = 97.
python 中的 % 实际上是一个模数运算符,其中 x % y 给出x / y. 在这种情况下,发生的情况75 / 4是 18,余数是 3,这就是为什么100 - 3 = 97.
Do not try to multiply the percentages, it's a common mistake.
不要试图乘以百分比,这是一个常见的错误。
回答by Zac
In the second exampe, %has same order as * so we get 3+2+1-5+4%2-1/4+6= 3+2+1-5+(4%2)-(1/4)+6=1+(4%2)-(1/4)+6 =1+0-(1/4)+6=1-(1/4)+6=0.75+6=6.75 and that is what it says when I try it on the console, so whatever you did you must have done something to round it.
在第二个例子中,% 与 * 的顺序相同,所以我们得到 3+2+1-5+4%2-1/4+6= 3+2+1-5+(4%2)-(1/4 )+6=1+(4%2)-(1/4)+6 =1+0-(1/4)+6=1-(1/4)+6=0.75+6=6.75 即当我在控制台上尝试它时它说的是什么,所以无论你做什么,你都必须做一些事情来解决它。
回答by howlshigh
Mathematics isn't my strong point, so yes this question got me as well, for a moment. But hope you find this useful.
数学不是我的强项,所以是的,这个问题也吸引了我,一会儿。但希望你觉得这很有用。
75 divided by 4 is 18.75
75 除以 4 是 18.75
18 multiplied by 4 is 72 (leaving 3 remaining from the 75)
18 乘以 4 是 72(75 剩下 3)
The calculation given is 100-25*3%4 with an answer of 97. Now this is how I would get it using PEMDAS as he speaks of in the question section:
给出的计算是 100-25*3%4,答案是 97。现在这就是我如何使用 PEMDAS 得到它,正如他在问题部分所说:
#!/bin/python
A = 100
B = 25
C = 3
D = 4
E = B*C # 75
F = E%D # 3
G = A-F # 97
print("B * C ="), E
print("E % D ="), F
print("A - F ="), G
I think you have to treat modulo (%) as a division,
我认为您必须将模 (%) 视为除法,
回答by Chris Lope
I figured out the answer to your second question because it was bugging me too--Zac's response is close, but the loss of the result of 1/4 is because of Python 2.X is truncating integer division results. So it's evaluating the modulo operation first, then the division (which since it isn't float, is returned as 0.
我想出了你的第二个问题的答案,因为它也困扰着我——Zac 的回答很接近,但 1/4 结果的丢失是因为 Python 2.X 正在截断整数除法结果。所以它首先评估模运算,然后是除法(因为它不是浮点数,所以返回为 0。
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
3 + 2 + 1 - 5 + (0) - (0) + 6
6 - 5 + 6
1 + 6
7
回答by Amr
Python evaluates % after * but before + or _ .
Python 在 * 之后但在 + 或 _ 之前评估 % 。
So,
所以,
(100 - 25 * 3 % 4)
(100 - 75 % 4)
(100 - 3)
(97)

