Page 293 - AI Ver 1.0 Class 9
P. 293

a = 5
                                 Remainder  of  a  number  first  and  then  assigns
                     %=                                                             b = 10               5
                                 the result to the left-hand side operand.
                                                                                    a %= b
                                                                                     a = 2
                                Exponential of a number first and then assigns
                     **=                                                             b = 4              16
                                the result to the left-hand side operand.
                                                                                    a **= b


                           Reboot


                 1.  What is the use of ** operator?

                 2.  Define the relational operators.


                 3.  Give an example to use **= operator?







                    Operator Precedence

            An expression is made up of values, variables, operators and functions. For example,

                       x-5+6 is an expression

            To evaluate an expression with multiple operators Python follows an order of precedence. This order can be
            altered by writing an expression within parenthesis.

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

                                   Order of Precedence                    Operators
                                            1                ()
                                            2                **
                                            3                */%//
                                            4                +-
                                            5                <= < > >=
                                            6                == !=
                                            7                = %= /= //= -= += *= **=
                                            8                not, and, or

            If an expression contains two or more 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 example,

               • 10 – 2 + 3 – 3 will be evaluated from left to right as + and – has the same order of precedence. So, the answer
              will be 10 – 2 will be 8 + 3 will be 11 – 3 will be 8.
               • 2  **  3  **  2  will  always  be  evaluated  from  right  to  left.  It  will  be  calculated  as  2  **  (3  **  2)  and  not  as
              (2 ** 3) ** 2. So, the answer will be 512.



                                                                                Introduction to Python  291
   288   289   290   291   292   293   294   295   296   297   298