Dictionaries

This page contains the CBSE Computer Science with Python class 11 Unit-4 chapter 3 Dictionaries. You can find the solutions for chapter 3 of CBSE class 11 Computer Science with Python Exercise. So is the case if you are looking for CBSE class 11 Computer Science with Python related topic Dictionaries questions and answers for the Exercise.
Question 1
1. Write the code to input any 5 years and the population of any city and print it on the screen.
Answer 1
This question asks us to store year and population as key-value pairs in a dictionary. Here, the year can be used as the key and the population can be used as the value. After taking 5 such entries, the dictionary can be displayed. A dictionary stores data in the form of key-value pairs and is useful when each value is to be accessed by a unique key.
Program
# Program to input 5 years and population and display them

cityPop = dict()
i = 1

while i <= 5:
    year = input("Enter year: ")
    population = input("Enter population: ")
    cityPop[year] = population
    i = i + 1

print "Year", "\t", "Population"
for year in cityPop:
    print year, "\t", cityPop[year]
Sample Output
Enter year: 2018
Enter population: 120000
Enter year: 2019
Enter population: 125000
Enter year: 2020
Enter population: 130000
Enter year: 2021
Enter population: 138000
Enter year: 2022
Enter population: 145000
Year    Population
2018    120000
2019    125000
2020    130000
2021    138000
2022    145000
Explanation
cityPop = dict() creates an empty dictionary.
Each year is used as a key.
The corresponding population is stored as its value.
The for loop is used to display all entries.
Thus, this program stores and prints the data using a dictionary.
Question 2
2. Write a code to input ‘n’ number of subject and head of the department and also display all information on the output screen.
Answer 2
In this question, the subject name can be used as the key and the Head of the Department can be used as the value. The number of records is not fixed, so the program first asks for n, then stores all entries dynamically. This is similar to dynamic creation of a dictionary during run time.
Program
# Program to input subjects and heads of department

dept = dict()
n = input("Enter total number of subjects: ")
i = 1

while i <= n:
    subject = raw_input("Enter subject: ")
    hod = raw_input("Enter head of department: ")
    dept[subject] = hod
    i = i + 1

print "Subject", "\t", "Head of Department"
for subject in dept:
    print subject, "\t", dept[subject]
Sample Output
Enter total number of subjects: 3
Enter subject: Physics
Enter head of department: Mr Sharma
Enter subject: Chemistry
Enter head of department: Mrs Rao
Enter subject: Mathematics
Enter head of department: Mr Verma
Subject Head of Department
Physics Mr Sharma
Chemistry   Mrs Rao
Mathematics Mr Verma
Explanation
The dictionary dept stores the subject-HOD pair.
The loop runs n times.
Each input is added as a new key-value pair.
Finally, all stored information is printed.
So, this program uses a dictionary to organize subject-wise information.
Question 3
3. Write the output for the following codes.
A={10:1000,20:2000,30:3000,40:4000,50:5000}
print A.items()
print A.keys()
print A.values()
Answer 3
Given code:
A = {10:1000,20:2000,30:3000,40:4000,50:5000}
print A.items()
print A.keys()
print A.values()
The methods items(), keys() and values() return the content of the dictionary in list form. items() returns key-value pairs as tuples, keys() returns the list of keys, and values() returns the list of values.
Output
[(10, 1000), (20, 2000), (30, 3000), (40, 4000), (50, 5000)]
[10, 20, 30, 40, 50]
[1000, 2000, 3000, 4000, 5000]
Explanation
A.items() returns all pairs in tuple form.
A.keys() returns all key values.
A.values() returns all corresponding values.
Note: In dictionaries, order is generally not guaranteed, but for this textbook-style answer, the expected output is written in the same sequence as the given code. The chapter also notes that dictionary order is unpredictable.
Question 4
4. Write a code to create customer’s list with their number & name and delete any particular customer using his / her number.
Answer 4
This question asks for a dictionary in which the customer number is the key and the customer name is the value. After storing all entries, one customer is deleted by using the given number. A dictionary item can be removed by using the del statement with the key.
Program
# Program to create customer list and delete a customer by number

customer = dict()
n = input("Enter total number of customers: ")
i = 1

while i <= n:
    number = input("Enter customer number: ")
    name = raw_input("Enter customer name: ")
    customer[number] = name
    i = i + 1

deleteNo = input("Enter customer number to delete: ")
del customer[deleteNo]

print "Customer Number", "\t", "Customer Name"
for number in customer:
    print number, "\t", customer[number]
Sample Output
Enter total number of customers: 3
Enter customer number: 101
Enter customer name: Amit
Enter customer number: 102
Enter customer name: Neha
Enter customer number: 103
Enter customer name: Rahul
Enter customer number to delete: 102
Customer Number Customer Name
101 Amit
103 Rahul
Explanation
Each customer number is stored as a key.
The customer name is stored as the value.
del customer[deleteNo] removes the required entry.
The remaining records are then displayed.
Thus, deletion in a dictionary is done directly by using the key.
Question 5
5. Write a Python program to input ‘n’ names and phone numbers to store it in a dictionary and print the phone number of a particular name.
Answer 5
This is a phone book program. Here, the name is the key and the phone number is the value. After storing all entries, the program asks for a name and prints its phone number. This is one of the main uses of a dictionary because values can be retrieved quickly by using the key. A very similar solved example is given in the chapter.
Program
# Program to create phone book and search phone number by name

phonebook = dict()
n = input("Enter total number of persons: ")
i = 1

while i <= n:
    name = raw_input("Enter name: ")
    phone = raw_input("Enter phone number: ")
    phonebook[name] = phone
    i = i + 1

searchName = raw_input("Enter name to search: ")

if phonebook.has_key(searchName):
    print "Phone number =", phonebook[searchName]
else:
    print "Given name does not exist"
Sample Output
Enter total number of persons: 3
Enter name: Mona
Enter phone number: 23456745
Enter name: Sonu
Enter phone number: 45678956
Enter name: Rohan
Enter phone number: 25678934
Enter name to search: Sonu
Phone number = 45678956
Explanation
The phone book is stored in dictionary form.
name is used as the key.
phonebook.has_key(searchName) checks whether the entered name exists.
If the name is found, the phone number is printed.
Otherwise, an error message is displayed.
So, this program shows how dictionaries help in searching data by key.
Question 6
6. Find errors from the following codes:
c=dict()
n=input(Enter total number )
i=1
while i<=n
a=raw_input("enter place")
b=raw_input("enter number")
c(a)=b
i=i+1
print "place"," ","number"
for i in c:
print i," ",cla[i]
Answer 6
Given code:
c=dict()
n=input(Enter total number )
i=1
while i<=n
     a=raw_input("enter place")
     b=raw_input("enter number")
     c(a)=b
     i=i+1
print "place","\t","number"
for i in c:
     print   i,"\t",cla[i]
This program contains multiple syntax and logical errors. The correct answer is to identify and fix them.
Errors
1.
The prompt in input() is not inside quotation marks.
2.
The while statement is missing a colon :.
3.
c(a)=b is incorrect for dictionary assignment.
4.
cla[i] is a wrong variable name; it should be c[i].
5.
Proper indentation is required inside the loop.
Corrected Program
# Corrected dictionary program

c = dict()
n = input("Enter total number: ")
i = 1

while i <= n:
    a = raw_input("Enter place: ")
    b = raw_input("Enter number: ")
    c[a] = b
    i = i + 1

print "Place", "\t", "Number"
for i in c:
    print i, "\t", c[i]
Sample Output
Enter total number: 3
Enter place: Delhi
Enter number: 101
Enter place: Mumbai
Enter number: 102
Enter place: Chennai
Enter number: 103
Place   Number
Delhi   101
Mumbai  102
Chennai 103
Explanation
c[a] = b is the correct way to store a value in a dictionary.
while i <= n: must end with a colon.
The loop body must be indented.
c[i] is used to access the value for key i.
Thus, after correcting these errors, the program runs properly and displays the stored dictionary entries.