Page 83 - tp_Modula_v2.0
P. 83

OPERATIONS ON A LIST

                 There are several operations which can be performed on on the list. The following table describes
                 the various list operations.
                      Operator       Operations                              Explanation

                          +        Joining lists      To add/concatenate two list together by using ‘+’ operator.
                          *        Repeating lists    To multiplies a list by an integer ‘n’ and then creates a
                                                      new list which repeats the original list ‘n’ times by using’*’
                                                      operator.

                          :        Slicing lists      To access the list elements within a specific range.
                         = =       Comparing list     Check if the contents of the list are same or not by using
                                                      ‘==’ operator.

                 Program 5: Operations on a list.
                     Program5.py
                  File  Edit  Format  Run   Options   Window    Help
                  list=[13, 25, 41, 63, 82]
                  list=[13, 25, 41, 63, 82]

                  list1=[23, 35, 51, 73, 92]
                  print(list+list1)
                  print(list*4)
                  print(list[0:4])
                  print(list==list1])



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

                  [13, 25, 41, 63, 82, 23, 35, 51, 73, 92]
                  [13, 25, 41, 63, 82, 13, 25, 41, 63, 82, 13, 25, 41, 63, 82, 13, 25, 41,
                  63, 82]
                  [13, 25, 41, 63]
                  False




                     LIST METHODS

                 To make the use of list easier Python provides various built-in methods. In the above section we
                 have already discussed append( ) and extend( ) methods, let’s explore more methods.

                 The following table describes the various built-in methods.

                        Method                                     Explanation

                       append( )    Add element at the end of the existing list

                       extend( )    Add more than one elements to the end of the existing list

                       insert(x, a) Insert element ‘a’ at the ‘x’ location of the list



                                                                                                 List in Python   81
   78   79   80   81   82   83   84   85   86   87   88