You are on page 1of 4

DELHI PUBLIC SCHOOL, AZAAD NAGAR, KANPUR

CLASS: XI
SUB: Computer Science CODE: 083
Chapter – Dictionaries Worksheet Solution

Q.1 Can you modify the keys in a dictionary?


Ans: Keys cannot be modified in dictionary while values are mutable in dictionary.

Q.2 Can you modify the value in a dictionary?


Ans: Yes, Values can be modified in dictionaries.

Q.3 Is dictionary Mutable?why are dictionary keys immutable


Ans: Dictionary is mutable type because we can change its values.
In Python a dictionary is used to store a mapping from one set of objects to another. An entry in
a dictionary consists of a key and its associated value. ... Values can be any type of object, but keys must
be immutable. This means keys could be integers, strings, or tuples, but not lists, because lists are mutable.
Q4.How are dictionaries different from Lists?
Ans: The dictionary is similar to lists in the sense that it is also a collection of data-items but it is different from lists
in the sense that lists are sequential collections(ordered) and dictionaries are non-sequential
collections(unordered).
Elements in lists or tuples can be accessed by using indexes. But in dictionaries the values can be obtained using
keys. By changing the sequence of key we can shuffle the order of elements of dictionary while this thing is not
possible in list.
I) II)
d1={5:[6,7,8],’a’ : (1,2,3)} dict = {'Subject': 'Computer Science', 'Sec':'A',
print(d1.keys()) 'Class': 11}
print(d1.values()) dict1 ={'Eng':15,'Hindi':20}
dict_keys([5, 'a']) dict2 ={"Inf.":dict,'Marks':dict1}
dict_values([[6, 7, 8], (1, 2, 3)]) print(dict2)
{'Inf.': {'Subject': 'Computer Science', 'Sec': 'A', 'Class':
11}, 'Marks': {'Eng': 15, 'Hindi': 20}}
III) IV)
dict = {'Subject': 'Computer Science', 'Sec':'A', 'Class': 11} dict1 ={'Eng':15,'Hindi':20}
k='rno' t=0
m=18 for i in dict1.values():
dict[k]=m t=t+i
print(dict) print(t)
k='Sec'
m='B' 35
dict[k]=m
print(dict)
{'Subject': 'Computer Science', 'Sec': 'A', 'Class': 11, 'rno':
18}
{'Subject': 'Computer Science', 'Sec': 'B', 'Class': 11, 'rno':
18}
V) VI)
setdefault() method returns the value of the item with max() – returns key having maximum value
the specified key. If the key does not exist, insert the key, min()- returns key having minimum value
with the specified value. dict = {'Subject':'Comp.Sci.','Sec':'A','Class':'XI'}
x = dict(name = "Aman", country = "India") print(min(dict))
x.setdefault('age',39) print(max(dict))
x.setdefault(30) print(min(dict.values()))
print(x) print(max(dict.values()))
o/p Class
{'name': 'Aman', 'country': 'India', 'age': 39, 30: None} Subject
A
XI
VII) VIII)
employees = {'Aman':{'salary':'10000'},'Mayur': d1={2:99,4:9}
{'salary':'51000'}} employee1 = employees['Aman'] d={(1,2):'ab',3:(4,5),4:[7,8],5:d1,12.3:{'CS':11}}
print(employee1) for i in d:
{'salary': '10000'} print(d[i])
ab
(4, 5)
[7, 8]
{2: 99, 4: 9}
{'CS': 11}
Q6.WAP to print a highest value from the given dictionary
dict1 ={'Eng':15,'Hindi':25,'Maths':23}
Ans:
t=0
for i in dict1:
if t<dict1[i]:
t=dict1[i]
s=i
print("HIGHEST MARKS",t,"in",s)

Q7.WAP that repeatedly asks the user to enter product names and prices. Store all of them in a dictionary whose keys are
product names and values are prices. And also write a code to search an item from the dictionary.

Ans:
Q8.WAP to create a dictionary named year whose keys are month names and values are their corresponding number of
days.
Ans:

Q9.WAP to create a dictionary with the rno., name & marks of n students .Also print the record of those who have
scored>75.
d={}
for i in range(2):
rno=int(input("Enter roll no."))
name=input("Enter name")
marks=int(input("Enter marks"))
d['rno']=rno
d['name']=name
d['marks']=marks
for i in d:
if i=='marks' and 'marks'>'75':
print(d)
Q10. Explain shallow copy and deep copy.
refer pg no.468 from book

You might also like