A tuple is a sequence. Python Tuples work exactly like a list except tuples are immutable,
means it cannot be changed in place.
The tuples are written inside parentheses. The tuple length is always fixed.
The difference between tuple and list is that tuple stores heterogeneous data while the list stores homogeneous data.
To create a single element in a tuple, write that element in parenthesis with a comma.
tu1 = (60,)
An empty tuple is created by opening and closing parenthesis.
tup = ()
To create a tuple containing more than one element -
tup1 = (22, 44, 55, 12 )
Concatenate Two Tuples
Addition sign (+) is used to concatenate two tuples, like -
>>> tup1 = (11, 54, 23) >>> tup2 = ('Car', 'Cycle', 'Bus') >>> tup3 = tup1 + tup2 >>> print tup3 (11, 54, 23, 'Car', 'Cycle', 'Bus')
Output of the above code
Update Tuple Element
To update the tuple element, simple re-assign tuple value using the index.
>>> tup1 = (11, 54, 23) >>> l1 = list(tup1) >>> l1 = 54 >>> tup1 = tuple(l1) >>> tup1 (11, 54, 54)
The del command is used to delete tuple -
>>> tup2 = (53, 23, 52, 50, 75, 37) >>> print tup2 (53, 23, 52, 50, 75, 37) >>> del tup2