Page 118 - Trackpad_V2.1_class8
P. 118

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 15: To access the different elements of a list


                      Program15.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])




                  You will get the following output:

                      Output

                   3
                   Computer
                   40




                  LIST FUNCTIONS

                  Let us study some of the functions we can use with lists.

                  The append() Function

                  The append() function inserts the element passed to it at the end of the list. Syntax of using
                  append() function is:

                      list_name.append(item)
                  Program 16: To add an element to a list using append() function

                      Program16.py

                   File  Edit  Format    Run   Options   Window    Help
                   a=[1,2,3,4,5]
                   a.append(6)
                   print (a)




                  You will get the following output:

                      Output

                   [1, 2, 3, 4, 5, 6]






                   116  Trackpad (V2.1)-VIII
   113   114   115   116   117   118   119   120   121   122   123