Page 128 - Computer Science Class 11 With Functions
P. 128
For example, consider the following statements appearing in a program, one after the other:
x = 23 #s1
x = 23.25 #s2
x = 'hello' #s3
In statements s1, s2, and s3, the variable x denotes an integer (23), a floating point number (23.25), and a string
('hello'), respectively. Thus, unlike C++ like languages, a variable does not have a type associated with it. Instead, it
implicitly gets associated with the type object it refers to.
6.3.1 Be Careful While Naming Variables
Although Python allows you to use the predefined identifiers for any purpose, it is definitely a bad idea to do so
because you would lose access to Python's predefined identifiers. For example,
>>> print = 23
>>> print('Hello')
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
print('Hello')
TypeError: 'int' object is not callable
Note that on executing the statement:
print = 23
The identifier print refers to an integer. So, the meaning assigned to the identifier print by Python has been
overwritten. So, we are unable to print the string 'Hello' because an attempt to execute the statement yields an
error.
6.3.2 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)
140709242827296
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
126 Touchpad Computer Science-XI

