expression and order of evaluation example
Hi – a question about something in the docs.
In order of math operations we all use PEMDAS with parentheses coming first.
On the page related to that in the docs: https://docs.python.org/3/reference/expressions.html
I find this example in section 6.15 – Evaluation Order, which states that in the examples given the expressions will be evaluated in the arithmetic order of their suffixes:
expr1 + expr2 * (expr3  expr4)
I thought with PEMDAS being the rule (for that simple expression) that the order of operations would actually be:
expr4 + expr3 * (expr1 – expr2)
since the parentheses should force that operation (item1 – item2) to happen first, followed by the multiplication by item3 and finally adding item4.
So which is correct? As listed(1,2,3,4) or the version I’ve typed below that (4,3,1,2)?
Followup question – to do this PEMDAS and other evaluation is it correct to assume that Python keeps rescanning the expression from left to right (with the exception of exponentiation) over and over while it reduces down the values represented until it has a single workable string to process into its single final value?
Thanks,
John Sullivan
Systems Engineer
Mercury Paper, Inc.
Office: (540) 4657733
participants (1)

John Sullivan