4.2. Tuple Data Type
4.2. Tuple Data Type
We can preserve insertion order and we can differentiate duplicate objects by using index.
Hence index will play very important role in Tuple also.
Tuple support both +ve and -ve index. +ve index means forward direction(from left to right) and
-ve index means backward direction(from right to left).
We can represent Tuple elements within Parenthesis and with comma separator. Parenthesis
are optional but recommended to use.
t=10,20,30,40
print(t) #(10, 20, 30, 40)
print(type(t)) #<class 'tuple'>
t=()
print(type(t)) #tuple
Note: We have to take special care about single valued tuple. Compulsory the value should ends with
comma, otherwise it is not treated as tuple.
Q. Which of the following are valid tuples?
t=(10)
1. t=()
print(t) #10
2. t=10,20,30,40
print(type(t)) #<class 'int'>
3. t=10
t=(10,) 4. t=10,
Tuple creation:
2). t=(10,) or t=10, #creation of single valued tuple ,parenthesis are optional, should ends with
comma
3). t=10,20,30 or t=(10,20,30) #creation of multi values tuples & parenthesis are optional
l=[10,20,30]
t=tuple(l)
print(t)
t=tuple(range(10,20,2))
print(t)
Accessing elements of tuple:
By using index:
t=(10,20,30,40,50,60)
print(t[0]) #10
print(t[-1]) #60
t=(10,20,30,40,50,60)
print(t[2:5]) # (30, 40, 50)
print(t[2:100]) # (30, 40, 50, 60)
print(t[::2]) # (10, 30, 50)
Tuple vs immutability:
Once we creates tuple, we cannot change its content. Hence tuple objects are immutable.
t=(10,20,30,40)
t1=(10,20,30)
t2=(40,50,60)
t3=t1+t2
print(t3) # (10,20,30,40,50,60)
t1=(10,20,30)
t2=t1*3
print(t2) #(10,20,30,10,20,30,10,20,30)
Important functions of Tuple:
len() : To return number of elements present in the tuple.
t=(10,20,30,40)
print(len(t)) #4
min() and max(): return min and max values according to default natural sorting order.
t=(40,10,30,20)
print(min(t)) #10
print(max(t)) #40
t=(10,20,10,10,20)
print(t.count(10)) #3
index(): returns index of first occurrence of the given element. If the specified element is not
available then we will get ValueError.
t=(10,20,10,10,20)
print(t.index(10)) #0
print(t.index(30)) #ValueError: tuple.index(x): x not in tuple
t1=sorted(t,reverse=True)
print(t1) #[40, 30, 20, 10]
cmp():
It compares the elements of both tuples. If both tuples are equal then returns 0
If the first tuple is less than second tuple then it returns -1
If the first tuple is greater than second tuple then it returns +1
t1=(10,20,30)
t2=(40,50,60)
t3=(10,20,30)
print(cmp(t1,t2)) # -1
print(cmp(t1,t3)) # 0
print(cmp(t2,t3)) # +1
Note: cmp() function is available only in Python2 but not in Python 3
Tuple Packing and Unpacking:
Tuple Packing
a=10
b=20
c=30
d=40
t=a,b,c,d
print(t) #(10, 20, 30, 40)
Here a,b,c,d are packed into a tuple t. This is nothing but tuple packing.
t=(10,20,30,40)
a,b,c,d=t
print("a=",a,"b=",b,"c=",c,"d=",d) #a= 10 b= 20 c= 30 d= 40
Note:At the time of tuple unpacking the number of variables and number of values should be same,
otherwise we will get ValueError.
t=(10,20,30,40)
a,b,c=t #ValueError: too many values to unpack (expected 3)
Tuple Comprehension:
Here we are not getting tuple object and we are getting generator object.
Output:
<class 'generator'>
1
4
9
16
25
Differences between List and Tuple:
List and Tuple are exactly same except small difference: List objects are mutable where as Tuple
objects are immutable.
In both cases insertion order is preserved, duplicate objects are allowed, heterogenous objects are
allowed, index and slicing are supported.
List Tuple
1) List is a Group of Comma separeated Values 1) Tuple is a Group of Comma separeated Values
within Square Brackets and Square Brackets are within Parenthesis and Parenthesis are optional.
mandatory. Eg: t = (10, 20, 30, 40)
Eg: lst = [10, 20, 30, 40] t = 10, 20, 30, 40
3) If the Content is not fixed and keep on changing 3) If the content is fixed and never changes then
then we should go for List. we should go for Tuple.
Q. Write a program to take a tuple of numbers from the keyboard and print its sum and average?
Output
Enter Tuple of Numbers:10,20,30
60 15.0