Page 165 - Computer Science Class 11 Without Functions
P. 165

Table 7.7: Precedence of Operators in Python

                Order Of Precedence                 Operators                             Description
                         1            **                                     Exponentiation
                         2            ~, +, -                                Complement, unary plus,  and  unary
                                                                             minus
                         3            *, / , %, //                           Multiply, divide, modulo, integer division
                         4            +, -                                   Addition, subtraction
                         5            in, not in, is, is not, <, <=,  Membership,  identity  and  relational
                                      >, >=,!=, ==                           operators
                         6            not, and, or                           Logical Operator
                         7            =,  %=,  /=,  //=,  -=,  +=,  *=,  Assignment operators

                                      **=
            The parenthesis () is used to override the precedence of operators. For example, in the expression 4*(3+7), even
            though the * operator has precedence over the + operator, the + operator will get precedence over the * operator
            because the subexpression 3+7 is enclosed within parentheses. If two binary operators have the same precedence,
            then the expression is evaluated from left to right. However, the binary operator ** is an exception to the general
            rule and is evaluated right to left. Thus, the expression 2**3**3 would be evaluated as if it were parenthesized as
            2**(3**3) and yield  the value 134217728. All unary operators are evaluated from right to left, for example, the
            expression not not True yields True. Next, let us consider some more examples of expressions.

            Evaluate the expressions given below:
            (i)  45*6+3/5
                Solution:
                45*6+3/5

                = 270+3/ 5
                = 270+0.6
                = 270.6

            (ii)  45*(6+3)/5
                Solution:
                = 45*(9)/5       …… expression within the () will be evaluated first

                = 405/5
                = 81.0

            (iii)  4 >= 5*3//4
                Solution:
                = 4 >= 15//4     …………*  is evaluated
                = 4 >= 3         ………. // is evaluated

                = True           ……..   >=  3 is evaluated
            (iv)  True and False or not True
                Solution:

                = True and False or not True
                = False or False
                =  False


                                                                                        Data Types and Operators  163
   160   161   162   163   164   165   166   167   168   169   170