Page 134 - TP_Play_V2.2_Class8
P. 134

Syntax of slicing the list:
              name of the list[ start : stop : step ]
                   start is the beginning position of the list.
                   stop is the end position of the list. The slicing stops one element before the stop index.

                   step is the increment of the list (the step parameter is optional and by default is 1).
              Program 7: To perform the slicing of lists

                   Program7.py

                File  Edit  Format   Run   Options   Window    Help

                list1=[13, 25, 41, 63, 82]

                print(list1[0:4:2])
                print(list1[1:3])
                print(list1[2:4])
                print(list1[:-3])
                print(list1[-1:])
                print(list1[:])



              On running the above program, you will get the following output:

                   Output

                [13, 41]
                [25, 41]
                [41, 63]
                [13, 25]
                [82]
                [13, 25, 41, 63, 82]



                   LIST METHODS

              To make the use of lists easier, Python provides various built-in methods. These methods are associated
              with specific list objects and are called using the dot (.) notation.  Let’s explore them.
              The following table describes the various built-in methods:


                        Method                                   Explanation
                        append( )  Adds a single element at the end of the existing list
                                   Adds more than one element (in sequential order or elements of the list) to the
                        extend( )
                                   end of the existing list
                        insert(x, a) Inserts element ‘a’ at the ‘x’ index of the list
                        remove( )  Removes the first occurrence of a specified element from a list.

                        index( )   Finds the index of the first occurrence of a specified element in a list.

                        count( )   Returns the occurrence (number of times) of a particular element in the list
                        pop([i])   Removes and returns item at the ‘i’ location in the list




                  132  Plus (Ver. 4.0)-VIII
   129   130   131   132   133   134   135   136   137   138   139