Page 134 - Code_GPT_Class_8
P. 134

Topic Flashback                                                  21 st  Century
                                                                                            Skills  #Information Literacy

                     1.  Write the syntax to create a list.



                     2.  Write an example of nested list.








              MODIFYING THE LIST ELEMENT


              Lists are mutable, which means you can change the elements after the list is created without creating a
              new space for storage of the changed list. You can do this by using an assignment operator (=).


              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 0; which means if a list contains 10
              elements then its index 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.






                        CodeGPT (Ver. 4.0)-VIII
                132
   129   130   131   132   133   134   135   136   137   138   139