Page 119 - Trackpad_V2.1_class8
P. 119

The extend( ) Function
                 Using the append( ) function, we can only add one element at a time. For adding more than one

                 element, we use the extend( ) function. Syntax of using extend() function is:
                     list_name.extend(iterable value)
                 Program 17: To add elements to an existing list using extend() function

                     Program17.py

                  File  Edit  Format   Run    Options   Window    Help
                  a=[1,2,3,4,5]
                  a.extend([6,7,8,9])
                  print (a)




                     Output
                  [1, 2, 3, 4, 5, 6, 7, 8, 9]




                 THE del STATEMENT

                 We use the del statement to remove an element or a slice of element from a list. Syntax for using
                 del statement is:

                     del list_name[start : stop : step]
                 Program 18: To delete elements from the list

                     Program18.py
                  File  Edit  Format   Run    Options   Window    Help

                  a=[1,2,3,4,5,6,7]
                  del a[1:3:2]
                  print (a)




                     Output
                  [1, 3, 4, 5, 6, 7]



                 Program 19: To delete the whole list


                     Program19.py
                  File  Edit  Format   Run    Options   Window    Help

                  a=[1,2,3,4,5,6,7]
                  del a




                 The whole list will be deleted.


                                                                                   Functions, String and List in Python  117
   114   115   116   117   118   119   120   121   122   123   124