Page 146 - Computer Science V2.0 Class 11
P. 146

As  expected,  the  variable  num1  now  gets  associated  with  the  new  object  12.  The  variable  num  continues  to  be
              associated with object 10. Next, let us consider:
               >>> num = 12.4
               >>> id(num)
                    2489891082064
              Note that on the execution of the assignment statement, num = 12.4, Python associates the variable num with the
              floating-point object 12.4 having object id 2489891082064.

              It is interesting to note that Python may (not necessarily) create different objects for the same value. For example,
               >>> id(12.4)
                    2489862941840
               >>> id(12.4)
                    2489895421264
               >>> id(234)
                    140709242834464
               >>> id(234)
                    140709242834464
              Typically, a short integer in Python gets associated with a fixed object id, but the same may not be true in the case
              of  more  complex  objects  like  floating-point  numbers.  Finally,  let  us  see  some  more  examples  of  the  assignment
              statements:
              num = 10                (the variable num is assigned the value 10)
              city = 'Delhi'         (the variable city is assigned the string  'Delhi')
              price = 150.50                (the variable price is assigned the floating-point value 150.50)
              discount = 10/100*price (the variable discount is assigned the result of evaluating the expression 10/100*price.



                                          num                           10             1000X




                                           city                       "Delhi"          2000X





                                          price                       150.50           3000X




                                                                       15.05
                                         discount                                      4000X
                                                               (result of 10/100*150.50)

                                                     Fig 6.2: Variables and their values
              To experiment with variables and their values, let us write a Python program.


               Program 6.1 Playing with variables and data values

                           1    #Objective: To experiment with variables and their values

                           2    city = 'Delhi'
                           3    interestRate = 9.5
                           4    print('city =', city)
                           5    print('interestRate =', interestRate)


               132   Touchpad Computer Science (Ver. 2.0)-XI
   141   142   143   144   145   146   147   148   149   150   151