Python : Tuples
Introduction to Tuples
A Python tuple is a collection of different data types, such as integers, floats, strings or lists. A tuple
is an immutable collection of elements. A tuple is a sequence of elements enclosed within
parentheses ().
Example 1 –
tuple1 = (1, 2, 3, 4, 5)
print(tuple1)
Output:
(1, 2, 3, 4, 5)
Example 2 –
tuple1 = ('Economics',87,'Accountancy',89.6)
print(tuple1)
Output:
('Economics', 87, 'Accountancy', 89.6)
Example 3 –
tuple1 = (10,20,30,[40,50])
print(tuple1)
Output:
(10, 20, 30, [40, 50])
Example 4 –
tuple1 = (1,2,3,4,5,(10,20))
print(tuple1)
Output:
(1, 2, 3, 4, 5, (10, 20))
Accessing Elements in a Tuple
Elements of a tuple can be accessed in the same way as a list or string using indexing and slicing.
Example 1 –
tuple1 = (2,4,6,8,10,12)
print(tuple1[0])
Output:
2
Example 2 –
tuple1 = (2,4,6,8,10,12)
print(tuple1[3])
Output:
8
Example 3 –
tuple1 = (2,4,6,8,10,12)
print(tuple1[15])
Output:
IndexError: tuple index out of range
Example 4 –
tuple1 = (2,4,6,8,10,12)
print(tuple1[1+4])
Output:
12
Example 5 –
tuple1 = (2,4,6,8,10,12)
print(tuple1[-1])
Output:
12
Tuple is Immutable
Tuple is an immutable data type. It means that the elements of a tuple cannot be changed after it
has been created. An attempt to do this would lead to an error.
Example –
tuple1 = (1,2,3,4,5)
tuple1[4] = 10
Output:
TypeError: 'tuple' object does not support item assignment
Tuple Operations
Concatenation
Python allows us to join tuples using concatenation operator depicted by symbol +.
Example –
tuple1 = (1,3,5,7,9)
tuple2 = (2,4,6,8,10)
print(tuple1 + tuple2)
Output:
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
Repetition
Repetition operation is depicted by the symbol *. It is used to repeat elements of a tuple.
Example –
tuple1 = ('Hello','World')
print(tuple1 * 3)
Output:
('Hello', 'World', 'Hello', 'World', 'Hello', 'World')
Membership
The in operator checks if the element is present in the tuple and returns True, else it returns False.
Example 1 –
tuple1 = ('Red','Green','Blue')
print('Green' in tuple1)
Output:
True
Example 2 –
tuple1 = ('Red','Green','Blue')
print('Green' not in tuple1)
Output:
False
Slicing
Like string and list, slicing can be applied to tuples also.
Example –
tuple1 = (10,20,30,40,50,60,70,80)
print(tuple1[2:7])
Output:
(30, 40, 50, 60, 70)
Tuples in Python Class 11 Notes
Tuple Methods and Built-in Functions
Python provides many functions to work on tuples.
len()
Returns the length or the number of elements of the tuple passed as the argument.
Example –
tuple1 = (10,20,30,40,50)
print(len(tuple1))
Output:
5
tuple()
Creates an empty tuple if no argument is passed, if a sequence is passed as argument.
Example 1 –
tuple1 = tuple()
print(tuple1)
Output:
()
Example 2 –
tuple1 = tuple('aeiou')
print(tuple1)
Output:
('a', 'e', 'i', 'o', 'u')
count()
Returns the number of times the given element appears in the tuple
Example –
tuple1 = (10,20,30,10,40,10,50)
print([Link](10))
Output:
3
index()
Returns the index of the first occurrence of the element in the given tuple.
Example –
tuple1 = (10,20,30,10,40,10,50)
print([Link](30))
Output:
2
sorted()
Takes elements in the tuple and returns a new sorted list. It should be noted that, sorted() does
not make any change to the original tuple.
Example –
tuple1 = ("Rama","Heena","Raj", "Mohsin","Aditya")
print(sorted(tuple1))
Output:
['Aditya', 'Heena', 'Mohsin', 'Raj', 'Rama']
min()
Returns minimum or smallest element of the tuple.
Example –
tuple1 = (19,12,56,18,9,87,34)
print(min(tuple1))
Output:
9
max()
Returns maximum or largest element of the tuple
Example –
tuple1 = (19,12,56,18,9,87,34)
print(max(tuple1))
Output:
87
sum()
Returns sum of the elements of the tuple.
Example –
tuple1 = (19,12,56,18,9,87,34)
print(sum(tuple1))
Output:
235
Tuple Assignment
Python has a useful feature called assignment of tuples. It enables the assignment of appropriate
values to a tuple of variables on the left side of the assignment operator from a tuple on the right
side. The left-hand side variables should have the same amount of components as the tuple.
Example 1 –
(num1,num2) = (10,20)
print(num1)
print(num2)
Output:
10
20
Example 2 –
record = ( "Pooja",40,"CS")
(name, rollNo, subject) = record
print(name)
print(rollNo)
print(subject)
Output:
Pooja
40
CS
Nested Tuples
A nested tuple is one that contains another tuple inside of it. Students’ names, roll numbers, and
grades (in percentage) are saved in a tuple. We can create a nested tuple to contain the
information of many of these students.
Example –
st=((101,"Aman",98),(102,"Geet",95),(103,"Sahil",87),(104,"Pawan",79))
print("S_No"," Roll_No"," Name"," Marks")
for i in range(0,len(st)):
print((i+1),'\t',st[i][0],'\t',st[i][1],'\t',st[i][2])
Output:
S_No Roll_No Name Marks
1 101 Aman 98
2 102 Geet 95
3 103 Sahil 87
4 104 Pawan 79