Comprehension

Assuming that D1 is a dictionary in Python,

Question: 1

Write a Python expression to check if the key, 'RNo' is present in D1.

Show Hint

The in operator checks the keys of a dictionary.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understand how a dictionary stores data:
A dictionary holds key : value pairs.
The in operator on a dictionary tests the KEYS, not the values.

Step 2: Write the expression:
'RNo' in D1
It gives True if the key 'RNo' is in D1, and False if it is not.

Step 3: Check with a sample:
Take D1 = {'Name': 'A', 'Age': 12}. Then 'RNo' in D1 gives False.
After D1['RNo'] = 5, the same expression gives True.

Final Answer:
'RNo' in D1
Was this answer helpful?
0
0
Question: 2

Write a Python expression to check if any key in D1 has a value 12.

Show Hint

Use the values() method of the dictionary together with the in operator.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understand the problem:
Here we must search the VALUES, not the keys.
The plain in operator only checks keys, so D1.values() is needed.

Step 2: Write the expression:
12 in D1.values()
D1.values() gives all the values of D1, and in checks if 12 is one of them.

Step 3: Check with a sample:
For D1 = {'Name': 'A', 'Age': 12}, the expression gives True.
The plain test 12 in D1 would give False here, because 12 is not a key.

Final Answer:
12 in D1.values()
Was this answer helpful?
0
0
Question: 3

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.

Show Hint

The setdefault() method adds a missing key with a value, and returns the existing value when the key is already there.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Find the built-in function:
The dictionary method setdefault(key, value) does both jobs in one call.
If the key is missing, it adds key : value and returns the value.
If the key is present, it leaves the dictionary as it is and returns the existing value.

Step 2: Write the statement:
D1.setdefault('RNo', 12)

Step 3: Check with a sample:
Take D1 = {'Name': 'A', 'Age': 12}. The first call returns 12 and D1 becomes {'Name': 'A', 'Age': 12, 'RNo': 12}.
A second call D1.setdefault('RNo', 99) returns 12 and D1 does not change. The value 99 is ignored.

Final Answer:
D1.setdefault('RNo', 12)
Was this answer helpful?
0
0
Question: 4

Write a single statement to delete all the elements from D1.

Show Hint

The clear() method empties a dictionary but keeps the dictionary itself.
Updated On: Oct 20, 2026
Show Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Find the method:
The dictionary method clear() removes every key : value pair.
The dictionary object D1 stays, but it becomes empty.

Step 2: Write the statement:
D1.clear()

Step 3: Check with a sample:
Take D1 = {'Name': 'A', 'Age': 12}. After D1.clear(), printing D1 gives {}.
The call itself returns None.

Final Answer:
D1.clear()
Was this answer helpful?
0
0