Page 158 - CodePilot V5.0 C8
P. 158

Program 18 A program to perform the slicing of lists.

                      Program18.py
                   File  Edit  Format   Run    Options   Window    Help

                   list1=[24, 28, 47, 53, 12]
                   print(list1[0:4:2])
                   print(list1[1:3])
                   print(list1[2:4])
                   print(list1[:-3])
                   print(list1[-1:])
                   print(list1[:])





                  The output of the preceding code would be as follows:

                      Output
                   [24, 47]
                   [28, 47]
                   [47, 53]
                   [24, 28]
                   [12]
                   [24, 28, 47, 53, 12]





                  LIST METHODS
                  To make the use of lists easier, Python provides various built-in methods. These methods are
                  associated with list objects and are called using the dot (.) notation.

                  Python has the following various built-in list methods:
                      append(): It adds a single item to the end of the list.

                      extend(): It adds all items from another iterable to the list.
                      insert(): It inserts an item at a specified index.

                      remove(): It removes the first occurrence of a specified item.
                      pop(): It removes and returns the item at the given index (last item by default).

                      clear(): It removes all items from the list.
                      index(): It returns the index of the first occurrence of a specified value.

                      count(): It returns the number of times a value appears in the list.
                      sort(): It sorts the list in ascending order by default.
                      reverse(): It reverses the order of the list.

                      copy(): It returns a copy of the list.



                  156
                        CodePilot (V5.0)-VIII
   153   154   155   156   157   158   159   160   161   162   163