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:
Answer:
tuple1 = (23,1,45,67,45,9,55,45)
print(tuple1.index(45))
Output:
2
tuple1 = (23,1,45,67,45,9,55,45)
print(tuple1.count(45))
Output:
3
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
print(tuple1 + tuple2)
Output:
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
tuple2 = (100,200)
print(len(tuple2))
Output:
2
tuple1 = (23,1,45,67,45,9,55,45)
print(max(tuple1))
Output:
67
tuple1 = (23,1,45,67,45,9,55,45)
print(min(tuple1))
Output:
1
tuple2 = (100,200)
print(sum(tuple2))
Output:
300
tuple1 = (23,1,45,67,45,9,55,45)
print(sorted(tuple1))
Output:
(1, 9, 23, 45, 45, 45, 55, 67)
2. Consider the following dictionary stateCapital:
stateCapital = {“AndhraPradesh”:”Hyderabad”,”Bihar”:”Patna”,”Maharashtra”:”Mumbai”,”Rajasthan”:”Jaipur”}
Find the output of the following statements:
Answer:
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print(stateCapital.get("Bihar"))
Output:
Patna
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print(stateCapital.keys())
Output:
dict_keys(['AndhraPradesh', 'Bihar', 'Maharashtra', 'Rajasthan'])
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print(stateCapital.values())
Output:
Dict_values(['Hyderabad', 'Patna', 'Mumbai', 'Jaipur'])
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print(stateCapital.items())
Output:
dict_items([('AndhraPradesh', 'Hyderabad'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')])
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print(len(stateCapital))
Output:
4
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print("Maharashtra" in stateCapital)
Output:
True
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
print(stateCapital.get("Assam"))
Output:
None
stateCapital = {
"AndhraPradesh": "Hyderabad",
"Bihar": "Patna",
"Maharashtra": "Mumbai",
"Rajasthan": "Jaipur"
}
del stateCapital["AndhraPradesh"]
Output:
'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. for example, return subject, marks. The calling function will receive a tuple containing all two 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 advantages of tuples are
- Tuples are immutable, meaning once created can’t be modified.
- Tuples required less memory and were faster than lists.
- Tuples can be used as keys in dictionaries.
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
Output:
TypeError: 'tuple' object does not support item assignment
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: mytuple = (0,2,3). 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 – The Python generate TypeError occurs due to the wrong statement; tuple1 = (5) does not create a tuple because a tuple can store a minimum of two values; the example shows that the variable tuple1 is just simply assigning the value 5. If you want to create a single-element tuple, then you must include a trailing comma: tuple1 = (5,).
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: "))
emails = tuple(input("Enter email: ") for _ in range(n))
usernames = tuple(email.split("@")[0] for email in emails)
domains = tuple(email.split("@")[1] for email in emails)
print("Emails:", emails)
print("Usernames:", usernames)
print("Domains:", domains)
Ouput:
Enter number of students: 2
Enter email: amit@gmail.com
Enter email: rajesh@yahoo.com
Emails: ('amit@gmail.com', 'rajesh@yahoo.com')
Usernames: ('amit', 'rajesh')
Domains: ('gmail.com', 'yahoo.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 –
def find_student(name, students):
if name in students:
print("Name found")
else:
print("Name not found")
n = int(input("Enter the number of students: "))
students = tuple(input("Enter Name: ") for _ in range(n))
search_name = input("Enter name to find: ")
find_student(search_name, students)
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
}
sorted_values = sorted(myDict.values(), reverse=True)
highest = sorted_values[0]
second_highest = sorted_values[1]
print("Highest value =", highest)
print("Second highest value =", second_highest)
Output:
Highest value = 99
Second highest value = 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 –
input_str = input("Enter a string: ")
char_count = {}
for char in input_str:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1 # Assign 1 when character appears for the first time
print("Character Count Dictionary:", char_count)
Output:
Enter a string: Welcome to School
Character Count Dictionary: {'W': 1, 'e': 2, 'l': 2, 'c': 2, 'o': 4, 'm': 1, '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 friends: "))
names = {}
for i in range(n):
name = input("Enter name: ")
mobile = input("Enter mobile number: ")
names[name] = mobile
print("\nOriginal Dictionary:", names)
names["Rakesh"] = "9785457545"
print("\nModified Dictionary (After Adding Rakesh):", names)
if "Rajesh" in names:
del names["Rajesh"]
print("\nDictionary (After Deleting Rajesh):", names)
else:
print("\nRajesh not found in dictionary.")
if "Ramesh" in names:
names["Ramesh"] = "8787457545"
print("\nUpdated Phone Number of Ramesh:", names)
else:
print("\nRamesh not found in dictionary.")
print("\nIs Rakesh present in dictionary?", "Rakesh" in names)
print("\nSorted Dictionary:")
for i in sorted(names):
print(i, ":", names[i])
Output:
Enter the number of friends: 3
Enter name: Rohit
Enter mobile number: 854565577
Enter name: Ramesh
Enter mobile number: 554125465
Enter name: Pranav
Enter mobile number: 5623451675
Original Dictionary: {'Rohit': '854565577', 'Ramesh': '554125465', 'Pranav': '5623451675'}
Modified Dictionary (After Adding Rakesh): {'Rohit': '854565577', 'Ramesh': '554125465', 'Pranav': '5623451675', 'Rakesh': '9785457545'}
Rajesh not found in dictionary.
Updated Phone Number of Ramesh: {'Rohit': '854565577', 'Ramesh': '8787457545', 'Pranav': '5623451675', 'Rakesh': '9785457545'}
Is Rakesh present in dictionary? True
Sorted Dictionary:
Pranav : 5623451675
Rakesh : 9785457545
Ramesh : 8787457545
Rohit : 854565577
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 –
def accept_details(n):
students = {}
for _ in range(n):
roll_no = int(input("Enter roll number: "))
name = input("Enter name: ")
percentage = float(input("Enter marks percentage: "))
students[roll_no] = (name, percentage)
return students
def search_student(students, roll_no):
if roll_no in students:
print("\nDetails Found!")
print("Name:", students[roll_no][0])
print("Marks Obtained:", students[roll_no][1], "%")
else:
print("\nNot present.")
def display_all(students):
print("\nAll Students' Results:")
for roll_no, (name, percentage) in students.items():
print(f"Roll No: {roll_no}, Name: {name}, Marks: {percentage}%")
def find_topper(students):
topper = max(students.items(), key=lambda x: x[1][1])
print("\nTopper:")
print(f"Roll No: {topper[0]}, Name: {topper[1][0]}, Marks: {topper[1][1]}%")
n = int(input("Enter the number of students: "))
students = accept_details(n)
roll_no_to_search = int(input("\nEnter the roll number for search: "))
search_student(students, roll_no_to_search)
display_all(students)
find_topper(students)
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%
All Students' Results:
Roll No: 1, Name: Amit, Marks: 91.0%
Roll No: 2, Name: Rajesh, Marks: 96.0%
Topper:
Roll No: 2, Name: Rajesh, Marks: 96.0%
Computer Science Class 11 Questions and Answers
- Computer Systems and Organisation
- Introduction to problem solving
- Getting Started with Python
- Flow of Control statements in Python
- String in Python
- Lists in Python
- Tuples and Dictionary in Python
- Society, Law and Ethics
Disclaimer: We have taken an effort to provide you with the accurate handout of “Tuples and Dictionaries in Python Class 11 Solutions“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 11 NCERT Textbook and CBSE Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination.
Images and content shown above are the property of individual organizations and are used here for reference purposes only.
For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in