Page 81 - tp_Modula_v2.0
P. 81

Program 1: To create a list.

                     Program1.py
                  File  Edit  Format  Run   Options   Window    Help
                  new_list1=[5, 6, 7, 8]                  # Homogeneous data elements
                  print(new_list1)

                  new_list2=[5, "Orange", 15.5]           # Heterogeneous data elements
                  print(new_list2)

                  new_list3=[162, [7, "Orange", 15.5]]    # Nested list
                  print(new_list3)



                 On running the above program, you will get the following output:
                     Output

                  [5, 6, 7, 8]
                  [5, 'Orange', 15.5]
                  [162, [7, 'Orange', 15.5]]




                     TRAVERSING A LIST

                 Traversing means accessing or visiting the elements of a list. Various ways to traverse a list are
                 indexing, negative indexing and slicing.

                 Indexing

                 The index of elements of a list starts from 0; which means if a list contains 10 elements then its

                 index (it is always an integer number) is from 0 to 9.
                  Clickipedia



                   In case if the user tries to access an element from a list beyond the defined range of the
                   list, then it will give an IndexError.

                 To access the list elements, the indexing operator or subscript operator [ ] is used.
                 Program 2: To access a list of given elements.

                     Program2.py
                  File  Edit  Format  Run   Options   Window    Help

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









                                                                                                 List in Python   79
   76   77   78   79   80   81   82   83   84   85   86