Page 342 - Computer Science Class 11 With Functions
P. 342
id(lst): 3237079334464
>>> lst = lst * 3
>>> lst
[4, 12, 9, 4, 12, 9, 4, 12, 9]
>>> print('id(lst): ', id(lst))
id(lst): 3237079396032
● 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 lexographic order is considered to have the minimum value, and the
string that appears last in the lexographic 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
● mean(): The mean()method yields the mean of the elements of a list. As the function mean() is available in the
statistics module, we need to import the statistics module first. For example,
>>> import statistics
>>> lst = [ 4, 12, 7, 9]
>>> statistics.mean(lst)
8
We summarise the discussion of the above methods in Table 13.1.
Table 13.1: Commonly Used Built-in Functions on Lists
S. No. Function Description Examples
1. max(list) It returns the largest element >>> lst=[3, -2, 0, 78, 25]
from a list. >>> max(lst)
78
2. min(list) It returns the smallest element >>> lst=[3, -2, 0, 78, 25]
from a list. >>> min(lst)
-2
3. sum(lst [,num]) It returns the sum of all numeric >>> lst =[1, 3, 5, 7]
values in the given list. The >>> sum(lst)
optional argument num, when
16
provided, is added to the sum of
>>> sum(lst,5)
the elements of the list.
21
340 Touchpad Computer Science-XI

