Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 119 - 2615_KEC
P. 119

    Output
                      List of Exam Subjects: ['Math', 'Science', 'English', 'History', 'Computer']
                      List of Modified Exam Subjects: ['Math', 'French', 'English', 'Social Studies',
                      'Computer']

                     SLICING THE LIST
                     List slicing refers to a part of a list. In Python, list slicing is done by using the Slicing operator(:). The
                     syntax of slicing the list is as follows:

                     name of the list[ start : stop : step ]
                     start is the start position of the list.
                     stop is the end position of the list.
                     step is the increment of the list (the step parameter is optional and by default is 1).
                     Program 16: A program to perform the slicing of lists.

                         Program16.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[:])

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

                     LIST FUNCTIONS AND METHODS

                     Some commonly used functions and methods of List are as follows:
                        len(): This function returns the number of items in a list.

                        list(): This function converts an iterable (like a tuple, string, or generator) into a list.

                                                                                       Functions, String and List in Python  117

| 114 115 116 117 118 119 120 121 122 123 124 |