Page 192 - Information_Practice_Fliipbook_Class11
P. 192

●  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 7.1.

                                       Table 7.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(lst)
                                                                       >>> 3


        7. 7 List Methods

        Python provides several methods to manipulate lists. Below, we will discuss some of these methods with an example
        list: lst. These methods do not create a new list; they modify the original list: lst. Such an operation that takes
        place on an existing object is said to be in place.
        ●  lst.append(elem): The method append()  inserts the object elem, passed as an argument,  at the end of
           the list lst.
          Example:
         >>> lst1 = [10, 20, 30, 40]
         >>> lst1.append(35)
         >>> lst1
              [10, 20, 30, 40, 35]
        ●  lst.insert(index, elem): The method insert() inserts the object elem, passed as an argument,  at
           the specified index.
         For example,

         >>> names = ['Aryan', 'Anthony', 'Sunpreet', 'Venkatesh']
         >>> names.insert(2, 'Samantha')
         >>> names
              ['Aryan', 'Anthony', 'Samantha', 'Sunpreet',  'Venkatesh']



          178  Touchpad Informatics Practices-XI
   187   188   189   190   191   192   193   194   195   196   197