Page 191 - Information_Practice_Fliipbook_Class11
P. 191
lst3 = lst1
print(lst1 is lst2) # False - lst1 and lst2 are different objects
print(lst1 is lst3) # True - lst1 and lst3 refer to the same object
print(lst1 is not lst2) # True - lst1 and lst2 are different objects
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.
● 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
Using Python's Built-in Functions with List
● min(), max(): 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. An error would occur
if some of the values in the list were not comparable.
>>> lst = [6, 2, 10, 8]
>>> min(lst)
2
>>> lst = ['Physics', 'Chemistry', 'Maths', 'Computer Sc.']
>>> max(lst)
'Physics'
>>> lst = [6, 4, 'hello']
>>> min(lst)
Traceback (most recent call last):
File "<pyshell#128>", line 1, in <module>
min(lst)
TypeError: '<' not supported between instances of 'str' and 'int'
● sum(lst [,num]): The sum() function returns the sum of the elements of a list. The second argument, num,
is optional. When the second argument is provided, it is also added to the sum of elements of the list. For example,
>>> lst = [ 4, 6, 2]
>>> sum(lst)
12
>>> sum(lst, 5)
17
Python Lists 177

