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

Solution and Explanation

Step 1: Name the operation:
We only need a yes or no answer about a key. A membership test does this.

Step 2: Use the membership operator:
The operator in, written between the key and the dictionary, tells whether the key exists.
'RNo' in D1

Step 3: Run it on sample data:
For D1 = {'Name': 'A', 'Age': 12}, the output of the expression is False.
The equivalent form 'RNo' in D1.keys() gives the same result.

Final Answer:
'RNo' in D1
Was this answer helpful?
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

Solution and Explanation

Step 1: Pick the right view of the dictionary:
A dictionary has three views: keys(), values() and items().
We want to match a value, so the values() view is the right one.

Step 2: Apply the membership test:
12 in D1.values()
The result is True when some key holds the value 12, and False otherwise.

Step 3: Run it on sample data:
For D1 = {'Name': 'A', 'Age': 12} the output is True.
For D1 = {'Name': 'A'} the output is False.

Final Answer:
12 in D1.values()
Was this answer helpful?
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

Solution and Explanation

Step 1: List what the statement must do:
Case 1: 'RNo' absent. Add 'RNo' : 12.
Case 2: 'RNo' present. Do not change anything and return the stored value.

Step 2: Match the behaviour with a method:
The method d[key] = value would overwrite an existing value, so it fails Case 2.
The method get() only reads; it never adds a pair, so it fails Case 1.
The method setdefault() adds when missing and only reads when present, so it satisfies both cases.

Step 3: Write and test the statement:
D1.setdefault('RNo', 12)
Test on D1 = {'Name': 'A', 'Age': 12}: the call returns 12 and D1 now has the pair 'RNo' : 12.
Repeating the call with 99 as the value still returns 12.

Final Answer:
D1.setdefault('RNo', 12)
Was this answer helpful?
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

Solution and Explanation

Step 1: Compare the options:
The statement del D1 removes the whole name D1, so D1 can no longer be used. That is not what we want.
The method pop() removes only one key at a time.
The method clear() empties the dictionary and keeps the name D1.

Step 2: Write and test the statement:
D1.clear()
On D1 = {'Name': 'A', 'Age': 12}, print(D1) after the call shows {}.

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