Page 65 - Information_Practice_Fliipbook_Class11
P. 65

3.7 id()

            In Python, each object is assigned a unique object identifier (id). An assignment operator associates a variable with
            the object on the right-hand side. The object on the right-hand side of the assignment operator may be the result of
            evaluating an expression.
            The function id() yields the unique object identifier of an object. For example,
             >>> id(10)
                 140709242827296
             >>> num = 10
             >>> id(num)
            Note that object 10 is assigned the object id 140709242827296. On execution of the assignment statement, num
            = 10, Python associates the object id 140709242827296 with the variable num. We would like to emphasize
            that an object id relates to an instance of executing an instruction. If you invoke IDLE multiple times, id(10) is likely
            to yield different object ids.
            Next, consider:
             >>> num1 = 10
             >>> id(num1)
                 140709242827296

            It is interesting to note that each of the variables num and num1 is associated with the same object 10 of type int
            (numeric values), and both have the same id. Next, consider:
             >>> num1 = 12
             >>> id(num1)
                 140709242827360
             >>> id(num)
                 140709242827296

            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.


            3.8 L-value and R-Value

            Consider the following assignment statement:
            sum = x+20




                                                                                    Basics of Python Programming  51
   60   61   62   63   64   65   66   67   68   69   70