Page 93 - tp_Modula_v2.0
P. 93

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

                     Output
                  (3, 4, 6, 7)
                  (3, 'Orange', 45.5)
                  (162, [7, 'Orange', 15.5], (2, 'Orange Education', 70.5))





                  Clickipedia



                   Items in a tuple can be repeated, i.e., tuple may contain duplicate items.




                 Syntax to create a tuple:
                 tup= (value1, value2, value3, ...)

                    To create an empty tuple: tup= tuple( )
                    To create a tuple with single element:  tup= (1,)

                     To create long tuple:  tup =  (0, 1, 2, 3, 4 , 5, 6, 7, 8, 9 10, 11, 12, 13,
                                                    14, 15)

                    To create nested tuple: tup = (1,2, (3,4))
                    To create tuple by using built-in tuple type object: tup = tuple(<sequence>)

                 Where, sequence is an object which includes strings, lists and tuples.
                 >>>tup= tuple('orange')
                 >>>tup

                 ('o', 'r', 'a', 'n', 'g','e')   #tuple tup is created from other sequence-
                                                             a string 'orange'.

                     ACCESSING A VALUE IN A TUPLE

                 To access values in a tuple, use the square brackets along with the index number for reference.

                 So, you can access the tuple element just like you access a list’s or string’s element.
                 Program 2: To access a value in tuple.

                     Program2.py
                  File  Edit  Format  Run   Options   Window    Help

                  tup1 = ('sst', 'chemistry', 2010, 2022);
                  tup2 = (1, 2, 3, 4, 5);
                  print(tup1 [1])
                  print(tup2 [3])








                                                                                               Tuple in Python    91
   88   89   90   91   92   93   94   95   96   97   98