Page 221 - Robotics and AI class 10
P. 221

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 Tuples
            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.














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















            Deleting a Tuple
            Elements of a tuple cannot deleted or removed, but we can delete the entire tuple by using the del keyword.
            Program 10: To delete a tuple.
















                                                         Introduction to Data and Programming with Python  219
   216   217   218   219   220   221   222   223   224   225   226