Tuple Data Type


Tuple is also a type of compound data type which can contain ordered data of similar or different data types separated by a comma (,) and enclosed within parentheses ( ).

Tuple is an immutable data type means once we have assigned the size and values to a tuple, we cannot change/update them later.

Note: Both List and Tuple contains ordered collection of elements of different data types separated with comma. But elements of a list are enclosed within square brackets while elements of a tuple are enclosed in parentheses. Also, List is updatable while Tuple is non updatable.

Example:

        tuple1 = (‘Tutorials’, 123, 17.4, ‘Abhi’)
        tuple2 = (956, ‘Duniya’)
        print tuple1		        # Prints complete list
        print tuple1[3]		# Prints 3th element of the list
        print tuple1[1:2]	        # Prints elements starting from 1st to 2nd
        print tuple1[1: ]	        # Prints all elements starting from 1st element
        print tuple2 * 3	        # Prints list 2 times
        print tuple1 + tuple2	# Prints concatenation of list1 and list2
        tuple1[3] = ‘Abc’	        # Not Allowed as Tuple is non updatable        


Output:

        (‘Tutorials’, 123, 17.4, ‘Abhi’)
        (‘Abhi’)
        (123, 17.4)
        (123, 17.4, ‘Abhi’)
        (956, ‘Duniya’, 956, ‘Duniya’)
        (‘Tutorials’, 123, 17.4, ‘Abhi’, 956, ‘Duniya’)