chapter 10. Tuples and Dictionary
1. Consider the following tuples, tuple1 and tuple2:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
Find the output of the following statements:
i. print(tuple1.index(45))
Answer: 2
ii. print(tuple1.count(45))
Answer: 3
iii. print(tuple1 + tuple2)
Answer: (23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
iv. print(len(tuple2))
Answer: 2
v. print(max(tuple1))
Answer: 67
vi print(min(tuple1))
Answer: 1
vii. print(sum(tuple2))
Answer: 300
viii. print(sorted(tuple1))
print(tuple1)
Answer: [1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)
2. Consider the following dictionary stateCapital:
stateCapital = {“AndhraPradesh”:”Hyderabad”, “Bihar”:”Patna”, “Maharashtra”:”Mumbai”, “Rajasthan”:”Jaipur”}
Find the output of the following statements:
i. print(stateCapital.get(“Bihar”))
Answer: Patna
ii. print(stateCapital.keys())
Answer: dict_keys([‘Assam’, ‘Bihar’, ‘Maharashtra’, ‘Rajasthan’])
iii. print(stateCapital.values())
Answer: dict_values([‘Guwahati’, ‘Patna’, ‘Mumbai’, ‘Jaipur’])
iv. print(stateCapital.items())
Answer: dict_items([(‘Assam’, ‘Guwahati’), (‘Bihar’, ‘Patna’), (‘Maharashtra’, ‘Mumbai’), (‘Rajasthan’, ‘Jaipur’)])
v. print(len(stateCapital))
Answer: 4
vi. print(“Maharashtra” in stateCapital)
Answer: True
vii. print(stateCapital.get(“Assam”))
Answer: Guwahati
viii. del stateCapital[“Rajasthan”]
print(stateCapital)
Answer: {‘Bihar’: ‘Patna’, ‘Maharashtra’: ‘Mumbai’, ‘Rajasthan’: ‘Jaipur’}
3. “Lists and Tuples are ordered”. Explain.
Answer: Lists and Tuples are ordered, it means that both the lists’ and tuples’ elements are ordered, because each element in lists and tuples has a designated index position. The index position (either forward or backward), maintain the order and these elements can also accessed individually with the help of their index position.
For example :
List1 = [10, 3, ‘mycstutorial.in’, False]
Tuple1 = [25, 12, ‘mycstutorial.in’, True]
print(List1[2])
print(Tuple[3])
Output:
myctutorial.in
True
4. With the help of an example show how can you return more than one value from a function.
Answer: A function can return multiple values in the form of tuples.
For example :
#Example - 1 def square(n1, n2, n3): sq = n1**2, n2**2, n3**2 rerurn sq #Example - 2 def cube(n1, n2, n3): return n1**3, n2**3, n3**3 #__main__ print("Calling Method Square") result1 = square(5,10,15) print(result1) print(type(result1)) print("Calling Method Cube") result2 = cube(2,3,4) print(result2) print(type(result2))
Output:
Calling Method Square
(25, 100, 225 )
<class ‘tuple’>
Calling Method Cube
(8, 27, 64)
<class ‘tuple’>
5. What advantages do tuples have over lists?
Answer: The advantages of tuples over lists is that tuples ensure that the data stored in tuple will not change.
If you want to store data as a sequence of any type of values and does not want to make any changes accidently, then you can use Tuple instead of List.
6. When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.
Answer: Tuples are used to store the data which is not intended to change during the course of execution of the program. For example, if the name of months is needed in a program, then the same can be stored in the tuple as generally, the names will either be repeated for a loop or referenced sometimes during the execution of the program.
Dictionary is used to represent the unordered list of the data. The key and value are separated by colon. Dictionary is used to store associative data like student’s roll no. and the student’s name. Here, the roll no. will act as a key to find the corresponding student’s name. The position of the data doesn’t matter as the data can easily be searched by using the corresponding key.
For example :
#Tuple vs Dictionary month = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') stud = {1: ('Amrit', 'IV','DPS Mgarh'), 2: ('Tanmay', 'III', 'DPS Mgarh')} print("Name of month 6th : ", month[5]) print("Details of Roll Number 1: ") print(stud[1])
Output:
Name of month 6th : Jun
Details of Roll Number 1:
(‘Amrit’, ‘IV’, ‘DPS Mgarh’)
7. Prove with the help of an example that the variable is rebuilt in case of immutable data types.
Answer: A tuple once created cannot be changed Even if you try to make changes in the tuple and give it the same name, Python Internally will create a new tuple and store it at a different memory location. For example
>>> tup1 = (1,2,'mycstutorial.in') >>> id(tup1) 61944840 >>> print(tup1) (1, 2, 'mycstutorial.in') >>> tup1 = tup1 + ('ncert solution','xi','computer','science') >>> tup1 (1, 2, 'mycstutorial.in', 'ncert solution', 'xi', 'computer', 'science') >>> id(tup1) 59301336 >>> print(tup1) (1, 2, 'mycstutorial.in', 'ncert solution', 'xi', 'computer', 'science') >>>
Check the id printed in two different statement. As you can observe in both cases variable name is the same but it is printed different id’s, It means that tuple is immutable data type, and if you try to change it, it will not make the change in-place, it create a new variable.
8. TypeError occurs while statement 2 is running. Give reason. How can it be corrected?
tuple1 = (5) #statement 1
len(tuple1) #statement 2
Answer: tuple1 = (5) statement does not create a tuple type variable, it creates integer type variable. len() function works with collection/sequence type variable such tuple, list, dictionary, string.
len() function need sequence value or variable as argument. That’s why len(tuple1) generate an error TypeError.
Corrected Way :
tuple1 = (5,) #statement 1
len(tuple1) #statement 2
Output:
1
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.