Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 100 - 2612_Digimind
P. 100

    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  object  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]

                       98   Premium Edition-VIII

| 95 96 97 98 99 100 101 102 103 104 105 |