Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 149 - 2620_Birla Open Mind C-8
P. 149

    Negative Indexing

                     In negative indexing, the indexing starts from –1 which is the last elements of the list, the index of -2
                     refers to the second last element. So, negative indexing is done from right to left.

                     Program 5: To traverse a list by using negative indexing

                         Program5.py
                      File  Edit  Format  Run   Options  Window    Help

                      list1=[13, 25, 41, 63, 82]
                                                                                      Output
                      print(list1[-2])                                             63
                      print(list1[-1])                                             82
                      print(list1[-4])                                             25

                     Traversing Nested Lists

                     Nested list can be traversed by using the index operator.

                     Program 6: To traverse the nested lists

                         Program6.py
                      File  Edit  Format   Run   Options   Window     Help

                      list1=[2, [2002, "Orange", 65.5], [4002, "Orange Education", 46.5]]

                      print(list1[2][1])
                      print(list1[1][2])

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

                         Output

                      Orange Education
                      65.5

                               Interdisciplinary Learning

                               #English

                       Create a list of ten most common English idioms and phrases in Python.

                                                                                                               #Lab Activity

                          SLICING THE LIST

                     List slicing refers to extract a specific section of a list. In Python, list slicing is done by using the Slicing
                     operator(:).

                                                                                                         #List in Python 147

| 144 145 146 147 148 149 150 151 152 153 154 |