Page 105 - KEC Khaitan C8 Flipbook
P. 105

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]




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

                 Program 13: To create a nested list
                     Program13.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]



                 ACCESSING A LIST

                 We use an index number to access the list items just like strings. Use the index operator [ ] to
                 access the elements of a list.
                 Program 14: To access the different elements of a list

                     Program14.py
                  File  Edit  Format   Run    Options   Window    Help

                  List1=[1,2,3,11,12,13,'Computer',40, 'Science']
                  print (List1[2])
                  print (List1[6])
                  print (List1[-2])





                                                                                   Functions, String and List in Python  103
   100   101   102   103   104   105   106   107   108   109   110