Page 436 - AI Ver 3.0 class 10_Flipbook
P. 436

-=          Subtracts first and then assigns a  Num1=5
                                   new value.
                                                                    Num2=10
                                                                    Num2-=Num1
                                                                    print(Num2)                           5

                       *=          Multiplies first and then assigns  Prod=5
                                   a new value.                     Prod*=5

                                                                    print(Prod)                          25

                       /=          Divides  first  and  then  assigns  a  Value=25
                                   new value.                       Value/=5

                                                                    print(Value)                         5.0
                       //=         Floor  division  first  and  then  Value1=37
                                   assigns a new value.             print(Value1//5)                      7

                       %=          Remainder of a number first and  N1=39
                                   then assigns a new value.        N2=5
                                                                    N1%=N2

                                                                    print(N1)                             4
                       **=         Exponential of a number first and  N1=2
                                   then assigns a new value.        N2=4
                                                                    N1**=N2

                                                                    print(N1)                            16
              Operator Precedence


              An expression is made up of values, variables, operators and functions. For example, 22/7*5.5 is an expression.
              To evaluate an expression with multiple operators we follow an order of precedence in Python. This order can be
              altered by writing an expression within parenthesis.

              This order of precedence in the descending order is listed below:
                                      Order of Precedence                 Operators
                                                1             ()

                                                2             **
                                                3             *, /, %, //
                                                4             +, -

                                                5             <=, <, >, >=, ==, !=
                                                6             =, %=, /=, //=, -=, +=, *=, **=
                                                7             not
                                                8             and

                                                9             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.

                    434     Touchpad Artificial Intelligence (Ver. 3.0)-X
   431   432   433   434   435   436   437   438   439   440   441