Test Index
CBSE Class 12 Computer Science 2026 Paper Set 4
© examsnet.com
Question : 24 of 37
Marks:
+1,
-0
Assuming that
D1 is a dictionary in Python,(i) (a) Write a Python expression to check if the key, 'RNo' is present in D1.OR(b) Write a Python expression to check if any key in D1 has a value 12.(ii) (a) Write a single statement using a BUILT_IN function to add the key : value pair 'RNo' : 12, if the key 'RNo' is not present in D1. However, if the key 'RNo' is present, the function should return its value.OR(b) Write a single statement to delete all the elements from D1.
Solution:
(i) (a)
'RNo' in D1The
in operator on a dictionary checks its keys; it gives True if 'RNo' is a key of D1 (same as 'RNo' in D1.keys()).OR (i) (b)12 in D1.values()
D1.values() gives all the values of the dictionary; in checks whether 12 is one of them.(ii) (a)D1.setdefault('RNo', 12)setdefault(key, value) returns the value of the key if it is present; if it is not present, it inserts key : value into the dictionary and returns value.D1 = {'Name': 'Asha'}print(D1.setdefault('RNo', 12)) # 12 (added)print(D1) # {'Name': 'Asha', 'RNo': 12}print(D1.setdefault('RNo', 99)) # 12 (already present, value returned, not changed)OR (ii) (b)D1.clear()
clear() removes all the key : value pairs, leaving an empty dictionary {}.© examsnet.com
Go to Question: