Page 295 - Computer Science Class 11 Without Functions
P. 295

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'
            12.7.2 sum(lst [,num]) function

            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
            12.7.3 mean() function

            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 12.1.
                                          Table 12.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
                4    from statistics  It returns the mean of  the  >>> lst = [1, 2, 3, 4, 5]
                     import mean           elements of a list.             >>> from statistics import mean

                     mean(lst)                                             >>> mean(1st)
                                                                               3




                                                                                                Lists and Tuples  293
   290   291   292   293   294   295   296   297   298   299   300