Tuple also work as a data structure,A tuple is a sequence of immutable objects.Tuple cannot be changed,it store different type of data.
The objects are enclosed within parenthesis and separated by comma.
item1 = (1,2,3,4,5); item2 = ('a','b','c','d'); item3 = (1,'a',2.30,"abc"); item4 = 'a','b','c','d';
item = (); #empty tuple
Tuple example
item1 = (1,2,3,'a'); item2 = (1, 2, 3, 4, 5, 6, 7 ); print "item1[3]; print "item2[2:6];
output:
'a'
(3, 4, 5, 6, 7 )
Tuple does not support single element deleting.Only entire tuple can remove,by using the del statement.
item = (1,2,3,'a'); print item; del item; print "After deleting Item : "; print item;
output:
(1,2,3,'a')
After deleting Item : NameError: name 'item' is not defined
Due to the immutable object tuple element can not update.Only by using existing tuples to create new tuples.
item1 = (1,2,3,4); item2 = (1,2,3,4); item3 = item1+item2; print item3;
output:
(1,2,3,4,1,2,3,4)