Page 119 - trackpad v5.1 class 8 flipbook
P. 119

Program 11: To create a list

                     Program11.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 12: To create an empty list


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


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




                 Nested List

                 A list can contain other lists as elements, which is known as a nested list.

                 Program 14: To create a nested list
                     Program14.py

                  File  Edit  Format   Run   Options   Window     Help
                                                                               Output
                  a=['Lakshaya', 'Sameer', 10, 20, [1,2,3],7]
                                                                            ['Lakshaya', 'Sameer', 10, 20,
                  print (a)
                                                                            [1, 2, 3], 7]




                                                                                   Functions, String and List in Python  117
   114   115   116   117   118   119   120   121   122   123   124