Tuples and Dictionaries in Python Class 11 Solutions

Teachers and Examiners (CBSESkillEduction) collaborated to create the Tuples and Dictionaries in Python Class 11 Solutions. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.

Tuples and Dictionaries in Python Class 11 Solutions

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([‘AndhraPradesh’, ‘Bihar’, ‘Maharashtra’, ‘Rajasthan’])

iii. print(stateCapital.values())
Answer – Dict_values([‘Hyderabad’, ‘Patna’, ‘Mumbai’, ‘Jaipur’])

iv. print(stateCapital.items())
Answer – dict_items([(‘AndhraPradesh’, ‘Hyderabad’), (‘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 – None

viii. del stateCapital[“AndhraPradesh”]
print(stateCapital)
Answer – {‘Bihar’: ‘Patna’, ‘Maharashtra’: ‘Mumbai’, ‘Rajasthan’: ‘Jaipur’}

3. “Lists and Tuples are ordered”. Explain.
Answer – In Lists and Tuples, the items are retained in the order in which they are inserted. The elements can always be accessed based on their position. The element at the position or ‘index’ 0 will always be at index 0. Therefore, the Lists and Tuples are said to be ordered collections.

4. With the help of an example show how can you return more than one value from a function.
Answer – Multiple values can be returned by Python functions. After creating the return statement, the values that must be returned must be comma separated. Return a, b, and c as an example
The calling function will receive a tuple containing all three values, which it can then access just like a tuple’s elements. Here is the programme sample. –
def myfunction():
subject = “Computer Science”
marks=99
return subject, marks

values = myfunction()
print(“The values returned are : “,values)

Output
The values returned are : (‘Computer Science’, 99)

5. What advantages do tuples have over lists?
Answer – The following are some benefits of tuples over lists: Lists take longer than triples. The code is protected from any unintentional changes thanks to tuples. It is preferable to store non-changing data in “tuples” rather than “lists” if it is required by a programme.

6. When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.
Answer – For example, if a programme requires the name of a month, that information can be saved in a tuple as names are typically either repeated for a loop or occasionally referred to while the programme is running. The data’s unordered list is represented by a dictionary.

7. Prove with the help of an example that the variable is rebuilt in case of immutable data types.
Answer –
mytuple = (1, 2, 3)
mytuple[0] = 0
print(mytuple)

We can see that a tuple is immutable once generated; the only method to change it is to rebuild or reassign the tuple. Rebuilding entails reassigning, which entails completely altering the value, not just a portion of it. You won’t see an error if you try the following statement: tuple1 = (1,2). This is because you are reconstructing it.

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, indicating that it has an integer type, and as a result, statement 2 returns a TypeError.
By adding a comma while assigning the value tuple1 =, the error can be fixed (5,) Now, rather than reading this as a number, the Python interpreter will read it as a tuple.

Programming Problems

1. Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email ids. Print all three tuples at the end of the program.
[Hint: You may use the function split()]

Answer –
n = int(input(“Enter number of students – “))
myList=[]
for i in range(n):
email=input(“Enter your email – “)
myList.append(email)
myTuple=tuple(myList)
names=[]
domains=[]
for i in myTuple:
name,domain = i.split(“@”)
names.append(name)
domains.append(domain)
names = tuple(names)
domains = tuple(domains)
print(“Person details”)
print(“Names = “,names)
print(“Domains Name = “,domains)
print(“Tuple = “,myTuple)

Output
Enter number of students – 2
Enter your email – amit@gmail.com
Enter your email – rajesh@gmail.com
Person details
Names = (‘amit’, ‘rajesh’)
Domains Name = (‘gmail.com’, ‘gmail.com’)
Tuple = (‘amit@gmail.com’, ‘rajesh@gmail.com’)

2. Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present in the tuple or not.
We can accomplish these by:
(a) writing a user defined function
(b) using the built-in function

Answer –
n = int(input(“Enter the number of students: “))
myList = []
for i in range(n):
name = input(“Enter Name : “)
myList.append(name)
myTuple = tuple(myList)
Search_Name = input(“Enter name to find: “)
def myfunction():
for item in myTuple:
if item==Search_Name:
print(“Name found”)
return
print(“Name not found”)

myfunction()

Output
Enter the number of students: 3
Enter Name : Rajesh
Enter Name : Ajit
Enter Name : Amit
Enter name to find: Rajesh
Name found

3. Write a Python program to find the highest 2 values in a dictionary.
Answer –
myDict ={“Computer Science”:99, “Information Technology”:98, “Web Application”:97, “Physical Education”:95}
myList = sorted(myDict.values())
s_max = myList[-2]
print(“Second largest element = “,s_max)

Output
Second largest element = 98

4. Write a Python program to create a dictionary from a string.
Note: Track the count of the letters from the string.
Sample string : ‘w3resource’
Expected output : {‘3’: 1, ‘s’: 1, ‘r’: 2, ‘u’: 1, ‘w’: 1, ‘c’: 1, ‘e’: 2, ‘o’: 1}
Answer –
str = input(“Enter a string: “)
myDict = {}
for char in str:
if char in myDict:
myDict[char] += 1
else:
myDict[char] =
for key in myDict:
print(key,’:’,myDict[key])

Output
Enter a string: Welcome to School
W : 1
e : 2
l : 2
c : 2
o : 4
m : 1
e : 2
t : 1
S : 1
h : 1

5. Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names

Answer –
n = int(input(“Enter the number of name : “))
names={}
for i in range(n):
name=input(“Enter name : “)
mobile=input(“Enter mobile number: “)
names[name]=mobile
print(names)
names[“Rakesh”]=”9785457545″
print(“Modified dictionary “,names)
del names[“Rajesh”]
for name in names:
names[name] = “8787457545”
break
print(“Rakesh” in names)
for i in sorted (names) :
print((i, names[i]), end =” “)

Output
Enter the number of name : 3
Enter name : Rohit
Enter mobile number: 854565577
Enter name : Ramesh
Enter mobile number: 554125465
Enter name : Pranav
Enter mobile number: 5623451675
{‘Rohit’: ‘854565577’, ‘Ramesh’: ‘554125465’, ‘Pranav’: ‘5623451675’}
Modified dictionary {‘Rohit’: ‘854565577’, ‘Ramesh’: ‘554125465’, ‘Pranav’: ‘5623451675’, ‘Rakesh’: ‘9785457545’}

6. Write a program to take in the roll number, name and percentage of marks for n students of Class X. Write
user defined functions to
• accept details of the n students (n is the number of students)
• search details of a particular student on the basis of roll number and display result
• display the result of all the students
• find the topper amongst them
• find the subject toppers amongst them
(Hint: use Dictionary, where the key can be roll number and the value is an immutable data type containing name and percentage)
Answer –


n=int(input(“Enter the number of students “))
if n!=0:
myDict={}
for i in range(n):
rollno=int(input(“Enter roll number: “))
name=input(“Enter name: “)
percentage=float(input(“Enter marks percentage: “))
myDict[rollno]=(name,percentage)

num=int(input(“Enter the roll number for search: “))
if num in myDict.keys( ):
print(“Details Found!”)
print(“Name:”,myDict[num][0])
print(“Marks Obtained: “,myDict[num][-1],”%”,sep=””)
else:
print(“Not present.”)

Output
Enter the number of students 2
Enter roll number: 1
Enter name: Amit
Enter marks percentage: 91
Enter roll number: 2
Enter name: Rajesh
Enter marks percentage: 96
Enter the roll number for search: 1
Details Found!
Name: Amit
Marks Obtained: 91.0%

Computer Science Class 11 Notes
Computer Science Class 11 MCQ
Computer Science Class 11 NCERT Solutions

Computer Science Class 11 Practical Questions and Answers

error: Content is protected !!