XI CS Tuple
XI CS Tuple
INTRODUCTION
A tuple is a collection which is ordered and immutable (We cannot change elements of a tuple in
place). Elements of a tuple are enclosed in parenthesis (round brackets) and are separated by
commas. Tuples can store a sequence of values belonging to any type. Tuples allows duplicate
members.
Ex:- T1 = ( ) # Empty Tuple
T2 = (4,5,6) # Tuple of integers
T3 = (1.5,4,9) # Tuple of numbers
T4 = (‘x’, ‘y’, ‘z’) # Tuple of characters
T5 = (‘a’,1.5,’KVS’,45) # Tuple of mixed values
T6 = (‘KVS’, ‘NVS’, ‘EMRS’) # Tuple of strings
>>>t2 = (10,) # Single element tuple
Note:- comma is necessary in single value tuples. Without a comma it will be a value, not a tuple.
>>>t3=(10,20,30,40,50) # Long tuple
>>>t4=(6,(4,5),9,7) # Nested Tuple
● tuple( ) function is used to create a tuple from other sequences.
● Indexing of tuple is similar to indexing of list.
● Difference between List and Tuple
S. No. List Tuple
01 Elements are enclosed in square brackets Elements are enclosed in parenthesis
i.e. [ ] i.e. ( )
02 It is mutable data type It is immutable data type
03 Iterating through a list is slower as Iterating through a tuple is faster as
compared to tuple compared to list
TRAVERSING A TUPLE
Traversing a tuple means accessing and processing each element of it. A tuple can be traversed using
a loop.
Ex:- >>> T1=(2,4,6,8,10) Output: 2
>>> for a in T1: 4
print(a) 6
8
10
ACCESSING TUPLES
Elements of a tuple can be accessed using Indexing and Slicing, same as in string and list.
if T1= (‘C’, ‘O’, ‘M’, ‘P’, ‘U’, ‘T’, ‘E’, ‘R’):
54
Elements C 0 M P U T E R
+ index Value 0 1 2 3 4 5 6 7
- Index Value -8 -7 -6 -5 -4 -3 -2 -1
OPERATORS IN TUPLE
Operation Name Description Example
Concatenation (+) Joining of two or more tuples is >>>t1 = (1, 2, 3)
called concatenation >>>t2 = (8, 9, 11)
Python allows us to join tuples >>>t1+t2
using concatenation operator (+) OUTPUT :
(1, 2, 3, 8, 9, 11)
(#concatenates two tuples)
>>>t1 = (1, 2, 3)
>>>t2 = (8, 9, 11)
>>>t3 = t1+t2
(#Created new tuple)
OUTPUT :
(1, 2, 3, 8, 9, 11)
Concatenation operator can also
be used for extending an
existing tuple.
>>>t1 = (1, 2, 3)
>>>t1 = t1 + (7,8,9)
>>>t1
It is used to repeat elements of a
>>>h1 = (‘H’ , ‘M’) >>>h1 * 3 (‘H’
Repetition (*) tuple. Repetition operation is
, ‘M’, ‘H’ , ‘M’, ‘H’ , ‘M’)
denoted by Symbol (*)
Membership The ‘in’ operator checks the >>>h1 = (‘H’ , ‘M’)
presence of element in tuple. If >>>’H’ in h1
the element is present it returns True
True, else it returns False. >>>’m’ not in h1
55
The not in operator returns True True
if the element is not present in
the tuple, else it returns False.
Slicing It is used to extract one or more >>>t1 = (1, 2, 3, 7, 8, 9)
elements from the tuple. Like >>>t1[2:4]
string and list, slicing can be (3, 7)
applied to tuples also. >>>t1 =(10, 20, 30, 40, 50,
60, 70, 80)
>>>t1[2 : 7]
(30, 40, 50, 60, 70)
>>>t1[ : 5]
(10, 20, 30, 40, 50)
>>>t1[: : -1]
(80, 70, 60, 50, 40, 30, 20, 10)
56
>>>t1 = tuple(range(7))
>>>t1
(0, 1, 2, 3, 4, 5, 6)
>>>t1=tuple(“tuples in python”)
This function returns the frequency
count( ) >>>t1.count(‘p’)
of an element in the tuple.
2
>>>t1=tuple(“tuples in python”)
>>>t1.index(‘n’)
This function returns the index of the 8
index( ) first occurrence of the element in the
given tuple. >>>t1=tuple(“tuples in python”)
>>>t1.index(‘f’)
ValueError: tuple.index(x): x not in tuple
>>>t1 = (‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
This element takes tuple as an >>>sorted(t1)
argument and returns a sorted list.
sorted( ) [‘e’, ‘l’, ‘p’, ‘s’, ‘t’, ‘u’]
This function does not make any
change in the original tuple. >>>t1
(‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
>>>t1 = (3, 8, 4, 10, 1)
>>>min(t1)
This function returns the minimum or 1
min( )
smallest element of the tuple. >>>t1 = (‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
>>>min(t1)
‘e’
>>>t1 = (3, 8, 4, 10, 1)
>>>max(t1)
This function returns the maximum 10
max( )
or largest element of the tuple. >>>t1 = (‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
>>>max(t1)
‘u’
>>>t1 = (3, 8, 4, 10, 1)
This function returns the sum of the
sum( ) >>>sum(t1)
elements of the tuple.
26
57
MIND MAP OF TUPLE
ANSWERS OF MCQ
(1) b, (2) c, (3) c, (4) c, (5) a, (6) d, (7) a, (8) d, (9) c, (10) a
Competency Based Question
Q.1 Discuss the utility and significance of Tuples, briefly.
Ans. It is a type of array. It plays a very important role in python. In python it is an immutable type
of container which stores any kind of data types. It is short in memory size in comparison to
lists.
Q.2 Lists and Tuples are ordered. Explain.
Ans. Lists and Tuples are ordered sequences as each element has a fixed position.
Q.3 Write the output of the following.
a=(23,34,65,20,5)
print(a[0]+a.index(5))
Ans. 27
Q.4 Create a tuple names as given here:
names = ('JAI', 'RAM', 'MAMTA', 'KAPIL', 'DINESH', 'ROHIT') Write proper code for getting:
('MAMTA', 'KAPIL', 'DINESH')
Ans. print(names [2 : 5 ])
Q.5 T1=(1,2) & T2=(“KV”,)
print(T2*2)
Ans. (‘KV’,’KV’)
Q.6 T1=(45,67,98)
T1=T1+(1,2,3)
print(T1)
Ans. (45,67,98,1,2,3)
Q.7 Consider the following tuple and write the code in python for the following statements:
T1=(12,3,45,’Hockey’,’Anil’,(‘a’,’b’))
(a) Display the first element of ‘T1’ (b) Display the last element of ‘T1’
(c) Display ‘T1’ in reverse order. (d) Display ‘Anil’ from tuple ‘T1’
(e) Display ‘b’ from tuple
59
Ans. (a) print(T1[0]) (b) print(T1[-1]) (c) print(T1[ : :-1]) (d) print(T1[4])
(e) print(T1[5][1])
Q.8 What is unpacking Tuple?
Ans. 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
the same as the number of elements in the tuple.
Q.9 What are nested tuples?
Ans A tuple containing another tuple in it as a member is called a nested tuple, e.g., the tuple
shown below is a nested tuple:
Employees = (4580,'Rahul', (82,67,75,89,90)) # nested tuple
Q.10 for i in tuple(“KVS”):
print(i+i)
Ans. KK
VV
SS
Very short Questions
Q.1 T1=(“Hockey”, “Cricket”, ‘Football’)
print(max(T1))
print(min(T1))
Ans. Hockey
Cricket
Q.2 What will be the output of the following Python Code?
tp = (5,)
tp1 = tp * 2
print(len(tp1))
Ans. 2
Q.3 Type Error occurs while statement 2 is running. Give reason. How can it be corrected?
>>> tuple1 = (5) #statement 1
>>> len(tuple1) #statement 2
Ans. Because tuple1 is an integer not a tuple. So, we cannot find the length of the integer. If you
want to make tuple then you should write ( 5, )
Q.4 How to create an empty tuple? Also create a single element tuple.
Ans. t=( ) or t=tuple( ) single element tuple: t=(10,)
Q.5 Write a program that inputs two tuples and creates a third, that contains all elements of the
first followed by all elements of the second.
60
Ans. tup1 = eval(input("Enter First tuple :-"))
tup2 = eval(input("Enter second tuple :-"))
tup3 = tup1 + tup2
print(tup3)
Q.6 T1=('A',['B','D'],'C')
T1[1][1]='C'
print(T1)
Ans. ('A', ['B', 'C'], 'C')
Q.7 What is the difference between a List and a Tuple?
Ans.
List Tuple
Elements are enclosed in square brackets i.e. [ ] Elements are enclosed in parenthesis i.e. ( )
It is mutable data type It is immutable data type
Iterating through a list is slower as compared Iterating through a tuple is faster as
to tuple compared to list
61
n = int(input("Enter a number :- "))
tup += (n,)
ch = input("To quit enter y/Y =")
if ch == "y" or ch=="Y":
print(tup)
print("Max :-",max( tup ))
print("Min :-",min( tup ))
break
Q.2 Write a program to count vowels in a tuple?
Ans. T = tuple(input("Enter Name :"))
print(T)
v=0
for ch in T:
if ch in ['a','e','i','o','u']:
v=v+1
print("No. of vowels : ",v)
Q.3 What will be stored in variables a, b, c, d, e, f, g, h after following statements?
62