Page 233 - Ai_V1.0_Class9
P. 233

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


                         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                = %= /= //= -= += *= **=
                                                 8                not, and, or

                 If an expression contains two or more operators with the same precedence, then the 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.


                         Variables


                 A variable is a name given to a memory location to hold a specific value. It is just like a container that can hold
                 any type of data that can be changed later in the program.

                 Variable has:
                    • A name that follows the identifier naming conventions and is case-sensitive.

                    • A value that can be integer, float, string, boolean, etc.

                                                                                        Introduction to Python  231
   228   229   230   231   232   233   234   235   236   237   238