Page 185 - Information_Practice_Fliipbook_Class11
P. 185

7                                                            PYTHON LISTS














              Chapter Outline


              7.1 Lists                                          7.2 List Assignment
              7.3 Traversing a List                              7.4 Nested Lists
              7.5 Heterogeneous List                             7.6 More Operations on Lists
              7.7 List Methods





            Introduction

            In the last chapter, we learnt about control structures. In this chapter, we will discuss the mutable type of object, lists.
            Elements of a list may be of arbitrary types. Like strings, objects in a list appear in sequence and can be accessed using
            indexes.

            7.1 Lists

            A list comprises a comma-separated sequence of objects enclosed in square brackets []. The elements of a list may be
            accessed using indexing. Let us begin by creating a list of colours.
             >>> colors = ['red', 'green', 'blue', 'pink', 'orange', 'white', 'black']
             >>> colors
                 ['red', 'green', 'blue', 'pink', 'orange', 'white', 'black']

               Non-negative      0          1           2           3             4            5            6
                  indices
                              'red'     'green'     'blue'     'pink'        'orange'     'white'      'black'

                 Negative        -7         -6          -5          -4            -3           -2          -1
                  indices



            The strings 'red', 'green', 'blue', 'pink', 'orange', 'white', and 'black' are stored at
            index 0, 1, 2, 3, 4, 5, and 6 respectively.  The indexes 0, 1, 2, 3, 4, 5, and 6, correspond to the negative
            indexes -7, -6, -5, -4, -3, -2, and -1, respectively. To access an element in a list, the list object is followed by an opening
            square bracket, the index to be accessed, and a closing square bracket.





                                                                                                   Python Lists  171
   180   181   182   183   184   185   186   187   188   189   190