Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 138 - 2609_Logicode
P. 138

    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 9: To create a list

                          Program9.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 10: To create an empty list

                          Program10.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 11: To create a mixed data type list

                          Program11.py
                       File  Edit  Format   Run    Options   Window    Help
                       a=['Ravi', 'Bhatt', 12, 13, 14]                              Output
                       print (a)                                                  ['Ravi', 'Bhatt', 12, 13, 14]

                      136   Premium Edition-VIII

| 133 134 135 136 137 138 139 140 141 142 143 |