is used to know the data type of a python object. Note (ii) Creating Single element tuple While creating a tuple with a single element, add a comma at the end of the element. In the absence of a comma, Python will consider the element as an ordinary data type; not a tuple. Creating a Tuple with one element is called “Singleton” tuple.
>>> MyTup4 = ( ) >>> type(MyTup4) <class 'int'> >>> MyTup5 = ( ,) >>> type(MyTup5) <class 'tuple'> Example . . Accessing values in a Tuple Like list, each element of tuple has an index number starting from zero. The elements of a tuple can be easily accessed by using index number.
>>> Tup1 = ( , , , “Tamil”, “Telugu”, . , . ) # to access all the elements of a tuple >>> print(Tup1) ( , , , 'Tamil', 'Telugu', . , .
) #accessing selected elements using indices >>> print(Tup1[ : ]) ( , 'Tamil', 'Telugu') #accessing from the first element up to the specified index value >>> print(Tup1[: ]) ( , , , 'Tamil', 'Telugu') # accessing from the specified element up to the last element. >>> print(Tup1[ :]) ('Telugu', . , . ) # accessing from the first element to the last element >>> print(Tup1[:]) ( , , , 'Tamil', 'Telugu', .
, . ) Example 12th Computer Chapter - - Lists, Tuples, Sets and Dictionary . . Update and Delete Tuple As you know a tuple is immutable, the elements in a tuple cannot be changed.
Instead of altering values in a tuple, joining two tuples or deleting the entire tuple is possible. # Program to join two tuples Tup1 = ( , , , , ) Tup2 = ( , , , , ) Tup3 = Tup1 + Tup2 print(Tup3) Output ( , , , , , , , , , ) Example To delete an entire tuple, the del command can be used. Syntax: del Tup1 = ( , , , , ) print("The elements of Tup1 is ", Tup1) del Tup1 print (Tup1) Output: The elements of Tup1 is ( , , , , ) Traceback (most recent call last): File "D:/Python/Tuple Examp .py", line , in <module> print (Tup1) NameError: name 'Tup1' is not defined Example Note that, the print