Page 38 -
P. 38

2.7. Order of operations                                                     15

                           miles = 26.2
                           print miles * 1.61
                           This behavior can be confusing at first.
                           A script usually contains a sequence of statements. If there is more than one statement, the
                           results appear one at a time as the statements execute.
                           For example, the script

                           print 1
                           x = 2
                           print x
                           produces the output

                           1
                           2
                           The assignment statement produces no output.
                           Exercise 2.2. Type the following statements in the Python interpreter to see what they do:
                           5
                           x = 5
                           x + 1
                           Now put the same statements into a script and run it. What is the output? Modify the script by
                           transforming each expression into a print statement and then run it again.



                           2.7   Order of operations


                           When more than one operator appears in an expression, the order of evaluation depends
                           on the rules of precedence. For mathematical operators, Python follows mathematical
                           convention. The acronym PEMDAS is a useful way to remember the rules:

                              • Parentheses have the highest precedence and can be used to force an expression to
                                evaluate in the order you want. Since expressions in parentheses are evaluated first,
                                2 * (3-1) is 4, and (1+1)**(5-2) is 8. You can also use parentheses to make an
                                expression easier to read, as in (minute * 100) / 60 , even if it doesn’t change the
                                result.

                              • Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is
                                3, not 27.

                              • Multiplication and Division have the same precedence, which is higher than
                                Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not
                                4, and 6+4/2 is 8, not 5.
                              • Operators with the same precedence are evaluated from left to right (except exponen-
                                tiation). So in the expression degrees / 2 * pi , the division happens first and the
                                result is multiplied by pi. To divide by 2π, you can use parentheses or write degrees
                                / 2 / pi .

                           I don’t work very hard to remember rules of precedence for other operators. If I can’t tell
                           by looking at the expression, I use parentheses to make it obvious.
   33   34   35   36   37   38   39   40   41   42   43