Page 385 - Ai_417_V3.0_C9_Flipbook
P. 385

Symbol                          Purpose                         Example            Output

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

                                Reboot

                      1.  What is the use of the ** operator?


                      2.  Define the relational operators.

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

                      4.  Run the given statements in interactive mode and write the output generated:
                         True+1
                         45+bool(-11)
                         2+False+bool(0)
                         12-10+bool(10)
                         True+"hello"




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

                 The order of precedence in descending order is listed below:
                                        Order of Precedence                    Operators

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


                                                                                        Introduction to Python  383
   380   381   382   383   384   385   386   387   388   389   390