CBSE 2026 · Central · Set 4 · Q24 · 2 marks
Assuming that D1 is a dictionary in Python,(i)Write a Python expression to check if the key, 'RNo' is present in D1.Write a Python expression to check if any key in D1 has a value 12.(ii)Write a single statement using a BUILT_IN function to add the key : value pair 'RNo' : $\displaystyle 12$, if the key 'RNo'is not present in D1. However, if the key 'RNo' is present, the function should return its value.Write a single statement to delete all the elements from D1.
Assuming that D1 is a dictionary in Python,
(i)
Write a Python expression to check if the key, 'RNo' is present in D1.
Write a Python expression to check if any key in D1 has a value 12.
(ii)
Write a single statement using a BUILT_IN function to add the key : value pair 'RNo' : $\displaystyle 12$, if the key 'RNo'is not present in D1. However, if the key 'RNo' is present, the function should return its value.
Write a single statement to delete all the elements from D1.
Marking-scheme solution
(a)
'RNo' in D1
OR
'RNo' in D1.keys()(b)
12 in D1.values()(a)
D1.setdefault('RNo',12)
OR
if D1.get('RNo'):
print(D1['RNo']) # return D1['RNo']
else:
D1['RNo']=12
OR
Any other correct equivalent statement(b)
D1.clear()
OR
D1={}
OR
D1=dict()Revision of PythonDictionariesApplyvery_short_answer
More from Revision of Python
- State True or False: In Python, data type of 74 is same as the data type of 74.0.2026
- What will be the output of the following code snippet? t = tuple('tuple') t2 = t[2], t += t2 print(t)2026
- What will be the output of the following statement? print("PythonProgram"[-1:2:-2])2026
- Which of the following statements is true about dictionaries in Python?2026
- What is the output of the following code snippet? s='War and Peace by Leo Tolstoy' print(s.partition("by"))2026
- Write the output of the following code: def Exam2026(given): new=[] for ch in given[1:-1]: if ch.isupper():…2026
- Write a Python statement to perform the following tasks: (USE BUILT IN FUNCTIONS / METHODS ONLY) (i) To…2026
- Which of the following expressions in Python evaluates to True?2026
CBSE Class 12 Computer Science past-paper question from the 2026board exam, with the answer as CBSE’s own marking scheme gives it. Where our answers come from.