Python 上的操作顺序是如何进行的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48937457/
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
How do order of operations go on Python?
提问by Ashton
My question looks like this:
我的问题是这样的:
10-7//2*3+1
I am supposed to solve the equation.
我应该解方程。
And my answer seems to come out as 8 when using PEMDAS
使用 PEMDAS 时,我的答案似乎是 8
First its's 2*3 = 6; 10-7//6+1
second = 7//6= 1; 10-1+1
Third = 10-8 = 8;
But when putting it into python, I get a 2. Why?
但是当把它放入 python 时,我得到一个 2。为什么?
It seems the programs order is as such:
程序顺序似乎是这样的:
first: 7//2=3; 10-3*3+1
second: 3*3=9; 10-9+1
third:10-9+1= 2; 2
I don't get it
我不明白
回答by Martijn Pieters
PEMDAS
is P
, E
, MD
, AS
; multiplication and division have the same precedence, and the same goes for addition and subtraction. When a division operator appears before multiplication, division goes first.
PEMDAS
是P
, E
, MD
, AS
; 乘法和除法具有相同的优先级,加法和减法也是如此。当除法运算符出现在乘法之前,除法先进行。
The order Python operators are executed in is governed by the operator precedence, and follow the same rules. Operators with higher precedence are executed before those with lower precedence, but operators have matchingprecedence when they are in the same group.
Python 运算符的执行顺序由运算符优先级控制,并遵循相同的规则。优先级较高的运算符在优先级较低的运算符之前执行,但运算符在同一组中时具有匹配的优先级。
For 10-7//2*3+1
, you have 2 classes of operators, from lowest to higest:
对于10-7//2*3+1
,您有 2 类运算符,从最低到最高:
+, -
(correlating withAS
== addition and subtraction)*, @, /, //, %
(correlating withMD
, so multiplication and division).
+, -
(与AS
==加法和减法相关)*, @, /, //, %
(与 相关MD
,所以乘法和除法)。
So //
and *
are executed first; multiplication and division fall in the same group, not a set order here (MD
doesn't mean multiplication comes before division):
所以//
和*
首先被执行;乘法和除法属于同一组,这里不是固定顺序(MD
并不意味着乘法在除法之前):
10 - ((7 // 2) * 3) + 1
So 7 // 2
is executed first, followed by the multiplication by 3. You then get the subtraction from ten and adding one at the end.
So7 // 2
首先执行,然后乘以 3。然后从 10 中减去,最后加 1。
We've glossed over an issue that doesn't affect your particular case, but is very important for writing real Python programs. PEMDAS isn't really about orderof operations; it doesn't decide what order things are evaluated in. It's really about argument grouping. PEMDAS says that a + b + c * d
is evaluated as (a + b) + (c * d)
, but it doesn't say whether a + b
or c * d
is evaluated first.
我们掩盖了一个不会影响您的特定情况但对于编写真正的 Python 程序非常重要的问题。PEMDAS与操作顺序无关;它不决定事物的评估顺序。它实际上是关于参数分组的。PEMDAS 说它a + b + c * d
被评估为(a + b) + (c * d)
,但它没有说明是否首先评估a + b
或c * d
。
In math, it doesn't matter what you evaluate first, as long as you respect the argument grouping. In Python, if you evaluted b()
and c()
first in a() + (b() + c())
just because they're in parentheses, you could get a completely different result, because Python functions can have side effects.
在数学中,您首先评估什么并不重要,只要您尊重参数分组即可。在Python中,如果你evalutedb()
并c()
首先在a() + (b() + c())
仅仅因为他们是在括号中,你可以得到完全不同的结果,因为Python的功能可能有副作用。
Python expression evaluation mostly works from left to right. For example, in a() + b() + (c() * d())
, evaluation order goes as follows:
Python 表达式求值主要从左到右进行。例如,在 中a() + b() + (c() * d())
,评估顺序如下:
a()
b()
- the first
+
, now that its arguments are ready c()
d()
- the
*
, now that its arguments are ready - the second
+
, now that its arguments are ready
a()
b()
- 第一个
+
,现在它的参数已经准备好了 c()
d()
- 的
*
,现在它的参数已经准备好 - 第二个
+
,现在它的参数已经准备好了
This is despite the high precedence of *
and the parentheses around the multiplication.
尽管*
乘法有高优先级和括号。
回答by Johannes Overmann
This is documented here(Python Documentation / Expressions / Operator-Precedence):
这在此处记录(Python 文档/表达式/运算符优先级):
- Multiplication and division, including integer division, happen on the same precedence level, so the order is determined by the direction in which operands are grouped:
- Evaluation order of all the multiplications and divisions is left to right (like for most binary operations, except exponentiation).
- 乘法和除法(包括整数除法)发生在相同的优先级上,因此顺序由操作数分组的方向决定:
- 所有乘法和除法的计算顺序是从左到右(就像大多数二元运算一样,除幂运算)。
回答by Rory Daulton
PEMDAS is better expressed as
PEMDAS 更好地表示为
P Parentheses, then
E Exponents, then
MD Multiplication and division, left to right, then
AS Addition and subtraction, left to right
So in your expression, the division should be done before the multiplication, since it is to the left of the multiplication. After those are done, then do the subtraction then the addition.
所以在你的表达式中,除法应该在乘法之前完成,因为它在乘法的左边。做完这些之后,先做减法,再做加法。