Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 117 - 2615_KEC
P. 117

    LIST

                     In Python, a list is a type of container that is used to store a list of values of any type. One can
                     store an integer, string as well as objects in a single list. Each element in a list is assigned an index

                     number. The first index is 0, the second index is 1, the third is 2, and so on.

                     CREATING A LIST

                     Lists can be created by inserting the elements in square brackets []. The elements in the list are
                     separated by a comma.

                     Program 10: To create a list

                         Program10.py
                      File  Edit  Format   Run   Options   Window     Help

                      a= [10, 20, 30, 40, 50]                                      Output
                      print (a[2])                                              30

                     Let us study about some of the ways through which we can create different types of lists.

                     Empty List

                     An empty list in Python is created using square brackets [ ]. You can also create a empty list by
                     using the list() function.

                     Program 11: To create an empty list

                         Program11.py
                      File  Edit  Format   Run   Options   Window     Help
                      picnic=[]
                      print (picnic)                                               Output
                      exam= list()                                              []
                      print (exam)
                                                                                []

                     Mixed Data Type List
                     Mixed data type list can be created to place different data types such as integers, strings,
                     double, etc.
                     Program 12: To create a mixed data type list

                         Program12.py
                      File  Edit  Format   Run   Options   Window     Help

                      a=['Ravi', 'Bhatt', 12, 13, 14]                              Output
                      print (a)                                                 ['Ravi', 'Bhatt', 12, 13, 14]

                                                                                       Functions, String and List in Python  115

| 112 113 114 115 116 117 118 119 120 121 122 |