Page 94 - tp_Modula_v2.0
P. 94

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

                      Output

                   chemistry
                   4





                      UNPACKING TUPLE

                  Assigning individual elements of a tuple to multiple variables is called unpacking of tuples.

                  Program 3: To unpack a tuple.

                      Program3.py
                   File  Edit  Format   Run   Options  Window    Help

                   tuple3=(162, [7, "Orange", 15.5], [2, "Orange Education", 70.5])
                   print(tuple3)
                   x, y, z=tuple3
                   print(x)
                   print(y)
                   print(z)




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

                   (162, [7, 'Orange', 15.5], [2, 'Orange Education', 70.5])
                   162
                   [7, 'Orange', 15.5]
                   [2, 'Orange Education', 70.5]






                      TRAVERSING ELEMENTS IN A TUPLE

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

                  Indexing


                  Indexing means searching for elements by using the index (position) of the elements for the
                  quick retrieval of information. The index of a tuple starts from 0. The index or subscript operator
                  [] is used to access the elements of a tuple. The index value must be an integer as it will result
                  into TypeError.

                  For example: If a tuple contains 5 elements, then the index value of tuple will varies from 0 to 4.








                   92     Touchpad MODULAR (Version 2.0)
   89   90   91   92   93   94   95   96   97   98   99