Tuple (Immutable)
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. Like list and string, elements of a tuple can be accessed using index values, starting from
0.
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1 (1, 2, 3, 4, 5)
#tuple2 is the tuple of mixed data types
>>> tuple2 =('Economics',87,'Accountancy',89.6)
>>> tuple2 ('Economics', 87, 'Accountancy', 89.6)
#tuple3 is the tuple with list as an element
>>> tuple3 = (10,20,30,[40,50])
>>> tuple3 (10, 20, 30, [40, 50])
#tuple4 is the tuple with tuple as an element
>>> tuple4 = (1,2,3,4,5,(10,20))
>>> tuple4 (1, 2, 3, 4, 5, (10, 20)
**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. It should be noted that a sequence
without parenthesis is treated as tuple by default.
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.
>>> tuple1 = (2,4,6,8,10,12)
>>> tuple1[0]
>>> tuple1[3]
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.
However an element of a tuple may be of mutable type, e.g., a list.
#4th element of the tuple2 is a list
>>> tuple2 = (1,2,3,[8,9])
#modify the list element of the tuple tuple2
>>> tuple2[3][1] = 10
#modification is reflected in tuple2
>>> tuple2 (1, 2, 3, [8, 10])
TUPLE OPERATIONS –
Concatenation-
Python allows us to join tuples using concatenation operator depicted by symbol +.
tuple1 = (1,3,5,7,9)
tuple2 = (2,4,6,8,10)
tuple1 + tuple2
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
Repetition-
Repetition operation is depicted by the symbol *.
tuple1 = ('Hello','World')
tuple1 * 3
('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.
tuple1 = ('Red','Green','Blue')
'Green' in tuple1
True
The not in operator returns True if the element is not present in the tuple, else it returns False.
tuple1 = ('Red','Green','Blue')
'Green' not in tuple1
False
Slicing-
tuple1 = (10,20,30,40,50,60,70,80)
tuple1[2:7]
(30, 40, 50, 60, 70)
tuple1[0:len(tuple1)]
(10, 20, 30, 40, 50, 60, 70, 80)
tuple1[:5]
(10, 20, 30, 40, 50)
tuple1[2:]
(30, 40, 50, 60, 70, 80)
tuple1[0:len(tuple1):2]
(10, 30, 50, 70)
tuple1[-6:-4]
(30, 40)
tuple1[::-1]
(80, 70, 60, 50, 40, 30, 20, 10)
TUPLE METHODS AND BUILT-IN FUNCTIONS
TUPLE ASSIGNMENT –
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. The number of variables on the left should be same as the
number of elements in the tuple.
#The first element 10 is assigned to num1
and
#the second element 20 is assigned to num2.
>>> (num1,num2) = (10,20)
>>> print(num1)
10
>>> print(num2)
20
NESTED TUPLES –
A tuple inside another tuple is called a nested tuple.