Page 150 - 2622_Delhi Police Public School_C-8
P. 150

Program 9: To use pop, copy and clear built-in methods on the list

                   Program9.py
                File  Edit  Format   Run   Options   Window    Help

                list1 = [1, 2, 3, 4, 5]
                list1.pop(3)  # Remove and return the last element
                print(list1)                                                             Output
                list1.copy()  # Create a shallow copy of the list
                                                                                      [1, 2, 3, 5]
                print(list1)
                                                                                      [1, 2, 3, 5]
                list1.clear()  # Remove all elements from the list
                print(list1)                                                          []




                       Factbot


                 The ‘+’ operator when used with lists needs both the operands to be of list type, otherwise it will produce
                 an error.





                       Topic Flashback                                                   21 st  Century   #Information Literacy
                                                                                             Skills
                     Match the methods with their use.
                     1. extend( )             a. Removes the first occurrence of the given argument from the list

                     2. append( )             b. Reverses the order of the elements in the list

                     3. remove( )             c. Adds the element at the end of the existing list
                     4. sort( )               d.  Returns the count of occurrence (number of times) of a particular
                                               element in the list

                     5. reverse( )            e. Sorts all the elements in the list

                     6. count( )              f.  Adds more than one element (in sequential order or elements of the list)
                                               to the end of the existing list


              PYTHON FUNCTIONS


              Python also provides various built-in Python list functions. The following table describes various Python
              built-in functions:

                             Function                                Explanation


                          len(list)        Returns the total length of the list

                          max(list)        Returns the largest element from the given list

                          min(list)        Returns the smallest element from the given list





                        Premium Edition-VIII
                148
   145   146   147   148   149   150   151   152   153   154   155