Page 148 - 2620_Birla Open Mind C-8
P. 148

Program 3: To change the elements of a list

                   Program3.py

                File  Edit  Format  Run   Options  Window    Help
                list1=[13, 25, 41, 63, 82]
                list1[1]=50
                                                                                Output
                list1[3]=45
                print(list1[:])                                              [13, 50, 41, 45, 82]





                   TRAVERSING A LIST


              Traversing means accessing or visiting the elements of a list. Two ways to traverse a list are positive
              indexing and negative indexing.

              Positive Indexing


              In positive indexing, the index of elements of a list starts from from left to right which means if a list
              contains 10 elements then its index (it is always an integer number) is from 0 to 9.



                       Factbot



                If the user tries to access an element from a list beyond its defined range, then it will give an IndexError.




              To access the list elements, the indexing operator or subscript operator [ ] is used.

              Program 4: To find the elements in the list using the index value

                   Program4.py
                File  Edit  Format  Run   Options  Window    Help

                list1=[13, 25, 41, 63, 82]
                                                                                Output
                print(list1[0])                                              13
                print(list1[3])                                              63
                print(list1[4])                                              82





                                Factbot


                        If  a user tries to access a list element by  using  floating-point  indexing,  it will  result in
                        IndexError.






                  146  Premium Edition-VIII
   143   144   145   146   147   148   149   150   151   152   153