[docs] expression and order of evaluation example

John Sullivan john.sullivan at mercurypaper.com
Thu Nov 30 15:49:29 EST 2017


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)?



Follow-up 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) 465-7733
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20171130/bf555619/attachment.html>


More information about the docs mailing list