Page 294 - Computer Science Class 11 Without Functions
P. 294
When used with lists, is and is not compare the identity of the lists themselves, not their contents. That means that
two lists with the same elements are not considered the same unless they are the same object.
Here is an example:
>>> 1st1 = [10, 30, 20]
>>> 1st2 = [10, 30, 20]
>>> 1st2 = 1st1
>>> print (1st1 is 1st2)
True
>>> print (1st1 is 1st2)
True
>>> print (1st2 is 1st1)
True
>>> 1st1 = [10, 30, 20]
>>> 1st2 = [10, 30, 20]
>>> 1st3 = 1stl
>>> print (1st1 is 1st2)
False
>>> print (1st1 is 1st3)
True
>>> print (1stl is not 1st2)
True
>>> print([10, 30, 20, 40, 50] is [10, 30, 20] + [40, 50])
False
In the above example, lst1 and lst2 are two different list objects with the same contents, so lst1 is lst2
returns False. lst1 and lst3, on the other hand, refer to the same object, so lst1 is lst3 returns True. lst1
is not lst2 returns True because lst1 and lst2 are two different objects. Finally, in the light of the above
discussion, the comparison between [10, 30, 20, 40, 50] and [10, 30, 20] + [40, 50] also yields False.
12.6.2 Multiplication * Operator
The * operator concatenates a list for the specified number of times and yields a new list. For example,
>>> lst = [ 4, 12, 9]
>>> print('id(lst): ', id(lst))
id(lst): 3237079334464
>>> lst = lst * 3
>>> lst
[4, 12, 9, 4, 12, 9, 4, 12, 9]
>>> print('id(lst): ', id(lst))
id(lst): 3237079396032
12.7 Using Python's Built-in Functions with List
12.7.1 min(), max() functions
The min() and max() functions yield the minimum and maximum element of a list. In the case of strings, the string
that appears first in the lexicographic order is considered to have the minimum value, and the string that appears last
in the lexicographic order is considered to have the maximum value.
292 Touchpad Computer Science-XI

