Page 96 - tp_Modula_v2.0
P. 96

Tuple Slicing

                  To access the certain range of tuple elements the slicing operator [:] is used.

                  Program 6: To slice a tuple.
                      Program6.py
                   File  Edit  Format   Run   Options  Window    Help
                   tuple=('O', 'R', 'A', 'N', 'G', 'E')
                   print(tuple[1:2])
                   print(tuple[-1:])
                   print(tuple[:3])



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

                      Output

                   ('R',)
                   ('E',)
                   ('O', 'R', 'A')




                      PYTHON TUPLE METHODS

                  As you know tuples are immutable. So, add and remove methods are not provided with tuples.
                  There are only two methods for tuples, which are described in the table given below:


                                 Method                                     Explanation
                        count(a)                     It returns number of times ‘a’ occurs in the tuple

                        index(a)                     It returns index of first occurrence of the element in tuple


                      PYTHON FUNCTIONS

                  Python also provides various built-in Python tuple functions. The table given below describes the
                  various built-in functions:

                                Function                                   Explanation

                        len( )                     Returns the total length of the tuple
                        max( )                     Returns the largest element of the tuple

                        min( )                     Returns the smallest element of the tuple

                        sum( )                     Returns the sum of all the elements of the tuple
                        sorted()                   Returns the sorted list of elements
                        enumerate( )               Returns the enumerate object

                        all( )                     Returns true if all the elements of the tuple are true






                   94     Touchpad MODULAR (Version 2.0)
   91   92   93   94   95   96   97   98   99   100   101