Tuples
A tuple is an ordered sequence of elements of different data types, such as integer, float,
string, list or even a tuple.
Elements of a tuple are enclosed in parenthesis (round brackets) and are separated by
commas.
Tuple of integers : tuple1 = (1,2,3,4,5)
Tuple of mixed data types : tuple2 =('Economics',87,'Accountancy',89.6)
Tuple with list as an element : tuple3 = (10,20,30,[40,50])
Tuple with tuple as an element : tuple4 = (1,2,3,4,5,(10,20))
Like list and string, elements of a tuple can be accessed using index values, starting from 0.
If there is only a single element in a tuple then the element should be followed by a comma.
If we assign the value without comma it is treated as integer.
Tup5 = (10) ; type(Tup5) int
Tup6 = (10,) ; type(Tup6) tuple
It should be noted that a sequence without parenthesis is treated as tuple by default.
S = 6,3,8,1; type(S) tuple
Elements of a tuple can be accessed in the same way as a list or string using indexing and
slicing.
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. However an element
of a tuple may be of mutable type.
Multi = (3,6,9,12,15)
Multi[2] = 10 Type Error: tuple object does not support item assignment
T = (4,5,6, [3,1,2], 10, 23)
T[3][2]=20 (4,5,6, [3,1,20], 10, 23)
A tuple inside another tuple is called a nested tuple (
Assignment of tuple is a useful feature in Python. It allows a tuple of variables on the left side
of the assignment operator to be assigned respective values from a tuple on the right side.
(N1,N2,N3) = (20,40,60)
Tuple can be unpacked to the ordinary variables.
Employee = ( "Raja",1225,"Sales")
(Name,ID,Dept) = Employee
Method Description Example
len( Returns the length or the number of Tup = (10,20,30,40,50)
elements of the tuple passed as the len(Tup)
argument 5
tuple() Creates an empty tuple if no argument is tuple1 = tuple()
passed. tuple1
()
Creates a tuple if a sequence is passed as tuple1 = tuple('Computer') #string
argument tuple1
('c', 'o', 'm', 'p', 'u', 't', 'e', 'r')
tuple2 = tuple([1,2,3]) #list
tuple2
(1, 2, 3)
tuple3 = tuple(range(5))
tuple3
(0, 1, 2, 3, 4)
count() Returns the number of times the given tuple1 = (10,20,30,10,40,10,50)
element appears in the tuple tuple1.count(10)
3
tuple1.count(90)
0
index() Returns the index of the first occurrence of tuple1 = (10,20,30,40,50)
the element in the given tuple. tuple1.index(30)
2
tuple1.index(90)
ValueError: tuple.index(x): x not
in tuple
sorted() Takes elements in the tuple and returns a tuple1 = ("Rama","Heena","Raj",
new sorted list. It should be noted that, "Mohsin","Aditya")
sorted() does not make any change to the sorted(tuple1)
original tuple ['Aditya', 'Heena', 'Mohsin', 'Raj',
'Rama']
min() Returns minimum or smallest element of Tup = (19,12,56,18,9,87,34)
the tuple min(Tup)
9
max() Returns maximum or largest element of the max(Tup)
tuple. 87
sum() Returns sum of the elements of the tuple sum(Tup)
235