Tuple is an ordered collection of arbitrary objects. They work exactly like lists, except that tuples can’t be changed in place because they are immutable and parentheses are used rather than square brackets. If parenthesis is not specified then it by default treated as tuple. Tuples share most of their properties with lists. They are:
- Accessed by offset
- Of the category immutable sequence
- Fixed length, heterogeneous, arbitrarily nestable
- Arrays of object references
T1 =(1, 2, 3,"intellipaat") print T1
(1, 2, 3,’intellipaat’)
Tuples are accesses same like as list.
Operations on List
On list following operation are performed:
- Concatenate Tuple
+ Operator is used to concatenate two tuples.
T1 = (1, 2, 3, 4) T2 = (5, 6, 7, 8) print T1+T2
(1, 2, 3, 4, 5, 6, 7, 8)
- Replicate Tuples
Operator * is used to repeat a tuple by specific number of time.
T1=(1,2) print T1*2
(1,2, 1, 2)
- Sub Tuple
It is used to obtain a sub Tuple which is done by specifying the index.
T1 = (1, 2, 3, 4) print T1[0:3]
(1, 2, 3)
- Update List
Updation is not possible in tuple because tuples are immutable objects.
In tuple you cannot delete individual value in tuple. Using del you can delete entire tuple at a time.
T1 = (1, 2, 3, 4) del T1 print L1
Traceback (most recent call last):
File “C:/Python27/t.py”, line 3, in
NameError: name ‘T1’ is not defined
Functions in Tuple
|min(tuple)||Returns the minimum value from a tuple|
|max(tuple)||Returns the maximum value from the tuple|
|len(tuple)||Gives the length of a tuple|
|tuple(sequence)||Converts the sequence into tuple|
|cmp(tuple1,tuple2)||Compares the two tuple|