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

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





                                                                           3000X
                                        price                  150.50



                                                                    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)



                                                                                    Basics of Python Programming  127
   124   125   126   127   128   129   130   131   132   133   134