Page 383 - Computer Science V2.0 Class 11
P. 383

13                                                    LISTS AND TUPLES















                  Chapter Outline


                   13.1 List                                          13.2 Traversing a List
                   13.3 Nested Lists                                  13.4 Heterogeneous List
                   13.5 More Operations on Lists                      13.6 List Methods
                   13.7 Tuples                                        13.8 Tuple Operations
                   13.9 Tuple Methods





                 Introduction

                 In the last chapter, we learnt about strings and their several applications. However, a string is an immutable object,
                 and we cannot modify the elements of a string. Also, the elements of a string are constrained to be characters. These
                 restrictions are too harsh to develop practical applications. For example, while dealing with the students' data, we may
                 like to store their roll numbers, names, addresses, academic records, etc. Also, the data relating to a student, such as
                 his/her address and academic record, may change over a period of time. To deal with such situations, in this chapter
                 we will discuss two types of objects, namely, lists and tuples. Elements of a list or tuple may be of arbitrary types.
                 Whereas lists are mutable, tuples are immutable. Like strings, objects in a list appear in sequence and can be accessed
                 using indexes.

                 13.1 List

                 A list comprises a comma-separated sequence of objects enclosed in square brackets []. Like strings, 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


                                                                                                     Lists and Tuples  369
   378   379   380   381   382   383   384   385   386   387   388