You Will Learn The Following Topics in This Tutorial: Chapter 16 - Tuples in Python
You Will Learn The Following Topics in This Tutorial: Chapter 16 - Tuples in Python
You Will Learn The Following Topics in This Tutorial: Chapter 16 - Tuples in Python
com
2 Creating Tuple
4 Accessing Tuple
5 Traversing a Tuple
6 Tuple Operations
9 5) Comparing Tuple
12 Deleting A Tuple
16 Nested Tuple
Example: Write a program to input n numbers from the user. Store these
numbers in a tuple. Print the maximum, minimum, sum and mean of number
17
from this tuple.
Page 1 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
INTRODUCTION
A tuple is a sequence of values, which can be of any type and they are
indexed by integer.
Tuples are just like list, but we can’t change values of tuples in place.
Thus tuples are immutable. The index value of tuple starts from 0.
Creating Tuple:
i) A tuple can be created by enclosing one or more value in round
brackets ( ).
For Example:
Page 2 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
For Example
# Here tuple5 is created as a copy of tuple1.
>>> tuple5 = tuple1 [:]
>>> print (tuple5)
(1, 3, 5, 7, 9, 11)
<class 'tuple'>
>>> print(seq) #seq is a tuple
(1, 2, 3)
Accessing Tuple:
The elements of a tuple are accessed in the same way as elements are
accessed in a list.
Let us understand with the help of an example:
#Access tuple with subscripts
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
Traversing a Tuple:
Traversing a tuple means accessing the elements of the tuple one after
the other by using the subscript of index value.
For Example:
Page 6 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
For Example:
TUPLE OPERATIONS:
1) Concatenate ( + )
2) Repetition / Replicating ( * )
3) Membership ( in and not in)
4) Tuple Slicing
5) Comparison Operators
Page 7 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
1) Concatenating Tuples:
Python allows us to join two or more tuples using concatenation operator
depicted by the symbol +.
Page 8 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
2) Replicating Tuples
Python allows us to repeat the given tuple using repetition operator(*).
>>> tuple1 = ('Save')
#elements of tuple1 repeated 4 times
>>> tuple1 * 4
['Hello', 'Hello', 'Hello', 'Hello']
in operator:
The operator displays True if the tuple contains the given element or the
sequence of elements.
>>> tuple1 = ['C.Sc.','IP','Web App']
>>> 'IP' in tuple1
True
Page 9 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
not in operator:
The not in operator returns True if the element is not present in the
tuple, else it returns False.
>>> tuple1 = ['C.Sc.','IP','Web App']
>>> 'IP' not in tuple1
False
4) Slicing
Like lists, the slicing operation can also be applied to tuples.
>>> tuple1 =('Maths','Phy','Che','Eng','IP','C.Sc.','PE')
>>> tuple1[2:6]
('Che','Eng','IP','C.Sc.')
('Maths','Phy','Che','Eng','IP'0
#slicing with a given step size, here step size is 2
>>> tuple1[0:6:2]
('Maths', 'Che', 'IP')
#negative indexes
#elements at index -6,-5,-4,-3 are sliced
>>> tuple1[-6:-2]
('Phy', 'Che', 'Eng', 'IP')
5) Comparing Tuple
Python allows us to use all the relational operators on tuple like
==, >=,<=,!=,>,<
Python will compare individual elements of each tuple.
>>> L1 = (9,11,13)
>>> L2 = (9,11,13)
>>> L3 = (1,5,3)
>>> L1 == L2
Page 11 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
True
>>> L1 == L3
False
>>> L1 < L2
False
>>> L1 < L3
False
>>> L4 = (9,11,15)
>>>L1 < L4
True
Tuples are immutable means that the contents of the tuple cannot be
changed after it is created.
Let us understand the concept of mutability with help of an example.
Example
>>> tuple1 =('Maths','Phy','Che','Eng','IP','C.Sc.','PE')
>>> tuple1[2] = 'Music'
TypeError: 'tuple' object does not support item assignment
Python does not allow the programmer to change an element in a tuple
and it gives a TypeError as shown in the above example.
Page 12 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
output is
(10, 15, 20, 25, 30)
2) We can also add new element to tuple by using list:-
Following example illustrates how to add new elements to tuple using a
list.
>>> T = tuple() # create empty tuple
>>> print (T)
()
>>> l = list(T) # convert tuple into list
>>> l.append(10) # Add new elements 10 to list
>>> l.append(20) # Add new elements 20 to list
>>> T=tuple(l) # convert list into tuple
>>> print (T) # Display tuple T
(10, 20)
DELETING A TUPLE:
The del statement is used to delete a tuple.
>>> T = 1,2,3,4,
>>> print (T)
(1,2,3,4)
>>> del T
>>> print (T)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
print (T)
Page 14 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
1) len():
It returns the length of the tuple passed as the argument
>>> tuple1 = (10,20,30,40,50)
>>> len(tuple1)
5
2) tuple():
3) count():
3
>>> tuple1.count(90)
0
4) index():
Returns index of the first occurrence of the element in the tuple. If the
element is not present, ValueError is generated
>>> tuple1 = (10,20,30,20,40,10)
>>> tuple1.index(20)
1
>>> tuple1.index(90)
ValueError: 90 is not in tuple
5) 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.
>>> tuple1 =('Maths','Phy','Che','Eng','IP','C.Sc.','PE')
>>> sorted(tuple1)
>>> tuple1
['C.Sc.', 'Che', 'Eng', 'IP', 'Maths', 'PE', 'Phy']
6) min():
Returns minimum or smallest element of the tuple
>>> tuple1 = (34,12,63,39,92,44)
Page 16 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
>>> min(tuple1)
12
7) max():
Returns maximum or largest element of the tuple
>>> tuple1 = (34,12,63,39,92,44)
>>> max(tuple1)
92
8) sum():
Nested Tuple:
When a tuple appears as an element of another tuple, it is called a nested
tuple.
>>> tuple1 = (1,2,'a','c',(6,7,8),4,9)
#fifth element of tuple is also a tuple
>>> tuple1[4]
[6, 7, 8]
Page 17 of 19
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
>>> tuple1[4][1]
7
#index i gives the fifth element of tuple1 which is a
tuple
# index j gives the second element in the nested tuple
Page 19 of 19