Page 97 - tp_Modula_v2.0
P. 97

Program 7: Functions on a tuple.

                     Program7.py
                  File  Edit  Format  Run   Options   Window    Help

                  tup = (1,2,3,4,60,70,90)
                  print(len(tup))
                  print(max(tup))
                  print(min(tup))
                  print(sorted(tup))
                  print(sum(tup))
                  print(all(tup))
                  print(enumerate(tup))



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

                  7
                  90
                  1
                  [1, 2, 3, 4, 60, 70, 90]
                  230
                  True
                  <enumerate object at 0x000001DF6E296930>





                     OPERATIONS ON A TUPLE

                 Most common operation which you can perform with tuple is described in the table given below:

                      Operators             Operation                             Explanation
                           +           Joining tuples          ‘+’ operator is used to join two tuples

                            :          Slicing the tuples      Extracting the sub parts of the tuple

                            *          Tuple multiplication    Repeats the tuple by the given number of times

                  Clickipedia



                   You can join two or more tuples to form a new tuple. While using ‘+’ operator, both the operand
                   must be of same tuple type i.e. you cannot add a number or any other value to a tuple.


                 Program 8: Operations on a tuple.
                     Program8.py
                  File  Edit  Format  Run   Options   Window    Help
                  tup = (1,2,3,4,5,6)
                  tup1 = tup*2
                  print(tup+tup1)
                  print(tup1[3:-3])
                  print(tup1)



                                                                                               Tuple in Python    95
   92   93   94   95   96   97   98   99   100   101   102