Page 87 - tp_Modula_v2.0
P. 87

ADDING AN ELEMENT TO A LIST

                 An element is added to a list by using the append( ) method, it’s a built-in Python method.
                 In case if you want to add more than one elements to a list, then you can use extend( ) method.
                 Program 10: To add elements to a list.

                     Program10.py
                  File  Edit  Format  Run   Options   Window    Help

                  list=[13, 25, 41, 63, 82]
                  list.append(19) # to add one element
                  print(list)
                  list=[13, 25, 41, 63, 82]
                  list.extend([16, 2, 551) # to add more than one element
                  print(list)




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

                     Output
                  [13, 25, 41, 63, 82, 19]
                  [13, 25, 41, 63, 82, 16, 2, 551]





                     SOME MORE PROGRAMS

                 Program 11: Python built-in functions.
                     Program11.py
                  File  Edit  Format  Run   Options   Window    Help
                  list=[13, 25, 41, 63, 82]
                  list=[14, 26, 42, 64]
                  print(len(list))
                  print(max(list))
                  print(min(list))


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

                     Output

                  4
                  64
                  14




                 Program 12: To access a list of given elements beyond its range.

                     Program12.py
                  File  Edit  Format  Run   Options   Window    Help

                  list=[13, 25, 41, 63, 82]
                  print(list[5])




                                                                                                 List in Python   85
   82   83   84   85   86   87   88   89   90   91   92