Page 341 - Computer Science Class 11 With Functions
P. 341
Note that the list student includes three types of objects, namely, str, int, and list.
Next, we define another heterogeneous list mixedList:
>>> mixedList = [1, [2, 3], 'three', 4.0]
Interestingly, the elements of a list may also be names of the functions. For instance, in the following statements, we
first define two functions, namely, f1() and f2(). Subsequently, we create a list lst comprising the names f1
and f2. Finally, we invoke these two functions using elements at index 0 and 1 of the list lst.
>>> def f1(n):
... return n**2
...
>>> def f2(n):
... return n**3
...
>>> functions = [f1, f2]
>>> print('f1(5): ', functions[0](5))
f1(5): 25
>>> print('f2(5): ', functions[1](5))
f2(5): 125
>>>
13.5 More Operations on Lists
● Concatenation Operator + : The + operator concatenates a pair of lists. The concatenated list comprises the
elements of the first list on the left hand side of the + operator, followed by the elements of the second list on the
right hand side of the + operator.
>>> lst1 = ['Red', 'Green']
>>> lst2 = ['Blue']
>>> print('id(lst1): ', id(lst1))
id(lst1): 3237079561600
>>> print('id(lst2): ', id(lst2))
id(lst2): 3237079384384
>>> lst1 = lst1 + lst2
>>> lst1
['Red', 'Green', 'Blue']
>>> print('id(lst1): ', id(lst1))
id(lst1): 3237079385664
Note that the concatenation operator creates a new object for the concatenated list.
We have seen above that the assignment
lst1 = lst1 + lst2
creates a new object which is assigned to lst1. However, the Python operator += updates the existing list lst1. For
example,
>>> lst1 = [10, 5, 20]
>>> lst2 = [9, 7]
>>> print('id(lst1): ', id(lst1))
id(lst1): 3237079560384
>>> lst1 += lst2
>>> lst1
[10, 5, 20, 9, 7]
>>> print('id(lst1): ', id(lst1))
id(lst1): 3237079560384
● Multiplication Operator *: The * operator concatenates a list the specified number of times and yields a new list.
For example,
>>> lst = [ 4, 12, 9]
>>> print('id(lst): ', id(lst))
Lists and Tuples 339

