CBSE CS

cbse cs logo

Home

Tuples in Python

 

Chapter Tuples Syllabus 

 

Tuples: introduction, indexing, tuple operations (concatenation, repetition, membership
and slicing); built-in functions/methods – len(), tuple(), count(), index(), sorted(), min(),
max(), sum(); tuple assignment, nested tuple; suggested programs: finding the minimum,
maximum, mean of values stored in a tuple; linear search on a tuple of numbers, counting
the frequency of elements in a tuple.

MCQ AND QUESTIONS ON TUPLES

 

 

Exercise Questions: Tuple

 

 

1. What will be the output of the following code:
Employee=(‘rajesh’,100,23,[1,2,3])
len(Employee)

Answer

4

 

2. How tuple is different from list?

Answer

lists and tuples are both collections of items in Python, but they have some key differences. Lists are mutable, while tuples are immutable. Tuples are faster and consume less memory than lists, but have fewer built-in methods. Lists are more prone to unexpected changes and errors than tuples.

 

3. Which of the following creates a tuple?
(a)t1=(“a”,”b”) (b) t1[2]=(“a”,”b”)
(c) t1=(5)*2 (d) None of the above

Answer:

(a)t1=(“a”,”b”)

 

4. What is the length of the tuple shown below:
T = ( ( ( ( ‘a’, 1 ), ’b’ , ’c’ ), ’d’ , 2 ) , ’e’ , 3 )

Answer:

3

 

5. What is the difference between (30) and (30,)? 

Answer:
When we use type function then (30) is type of ‘int’ class where (30,) is a type of tuple which contain one element.

 

 

 

 

Question Answer  ( 2 Mark Questions )

1. 

Write a python program to create tuple of 10 integer type elements and find the largest element in tuple.

Answer: 

tuple=(6,3,1,8,4,9,2,20)
M=max(tuple)
print(“Largest Value in Tuple: “,M)

 

2. 

t1 = (3, 4)
t2 = (‘3’, ‘4’)
print(t1 + t2)

Answer: 
(3, 4, ‘3’, ‘4’)

 

3. 

t2 = (4, 5, 6)
t3 = (6, 7)
t4 = t3 + t2
t5 = t2 + t3
print(t4)
print(t5)

Answer: 

(6, 7, 4, 5, 6)
(4, 5, 6, 6, 7)

 

4. 

Discuss the utility and significance of Tuples, briefly.

Answer: 
It is a type of arrays . it play very important role in python . in python it is immutable type of container which store any kind of data types it is short in memory size in comparison of list .

 

5. Does the slice operator always produce a new tuple ?

Answer: 
No ,it will print a part of tuple .

 

6. Lists and Tuples are ordered. Explain.

Answer: 
Lists and Tuples are ordered sequences as each element has a fixed position.

 

 

 

 

 

3 Mark Questions

 

 

1. Find the output of the following Python Code:
t=(10,20,30,40,50,60,70,20,30,50)
>>> print (t)
>>> print (max(t))
>>> print (t.count(20))
>>> print (t[0]+5)
>>> print (t.index(40))
>>> print (min(t) + len(t))

Answer:
(10, 20, 30, 40, 50, 60, 70, 20, 30, 50)
70
2
15
3
20

 

 

2.  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.

Answer:

tup1 = eval(input(“Enter First tuple :-“))
tup2 = eval(input(“Enter second tuple :-“))
tup3 = tup1 + tup2
print(tup3)

 

 

 

3.  Create a tuple names as given here:
names = (‘jai’, ‘rahul’, ‘maya’, ‘kia’, ‘Dav’, ‘Lalit’)
Write proper code for getting :
(a) (‘maya’, ‘kia’, ‘Dav’)
(b) (‘jai’)
(c) (‘kia’, ‘Dav’, ‘Lalit’)

Answer:

a) names [2 : 5 ]
(b) names [ 0 ]
(c) names [3 : ]

 

 

 

4.  TypeError occurs while statement 2 is running. Give reason. How can it be corrected ?
>>> tuple1 = (5) #statement 1
>>> len(tuple1) #statement 2

Answer:

Because tuple1 is integer not a tuple. So, we cannot find the length of integer.
If you want to make tuple then you should write ( 5, ) 

 

 

 

 

error: Content is protected !!