Page 197 - AI Ver 1.0 Class 10
P. 197

This order of precedence in the descending order is listed below:

                                         Order of Precedence                Operators
                                                  1.            ()

                                                  2.            **
                                                  3.            +, - (Unary)

                                                  4.            *, /, %, //
                                                  5.            +, - (Binary)

                                                  6.            <=, <, >, >=
                                                  7.            ==, !=
                                                  8.            =, %=, /=, //=, -=, +=, *=, **=

                                                  9.            is, is not

                                                 10.            in, not in
                                                 11.            not, and, or

                 In the above table, as you have seen that there are few operators with the same precedence then order of evaluation
                 is from left to right except for exponential (**) which is always from right to left. For examples:

                    • 2 + 5 * (15 - 10) // 2 will be evaluated as follows:
                    2+5*(5)//2    since * and // have same precedence, it will be evaluated from left to right.
                   2+12
                   14
                    • 25%3%2
                    To evaluate the above expression it works like (25%3)%2.
                    1%2= 1

                    • 2**2**3
                    This expression is evaluated from right to left. Here it will be equivalent to 2**(2**3). The result is 256.


                              Task


                   Evaluate the following expressions:


                    10+20/5-3*2

                   (23+5)*7/5+(3*2)

                   45//2+(6+5)*2

                   16%3+56-4+(35//5)

                   12+56-16*4%3*3









                                                                                           Advance Python   195
   192   193   194   195   196   197   198   199   200   201   202