Q.Consider the statements given below and then choose the correct output from the given options:
D={'S01':95, 'S02':96 }
for I in D:
print(I,end='#')
(A) S01#S02#
(B) 95#96#
(C) S01,95#S02,96#
(D) S01#95#S02#96#
D={'S01':95, 'S02':96 }
for I in D:
print(I,end='#')
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →When a for loop iterates directly over a Python dictionary, it processes the dictionary's keys. The print() function with end='#' will output each key followed by a hash symbol, resulting in S01#S02#.
A Python dictionary stores data as key-value pairs. Understanding how to iterate over these structures is fundamental. When you use a for loop directly on a dictionary, Python has a specific default behavior: it iterates over the keys of the dictionary. The loop variable will sequentially take on each key.
Additionally, the print() function in Python, by default, adds a newline character (\n) after whatever it prints. However, you can modify this behavior using the end parameter. If you set end='#', then instead of a newline, a hash symbol (#) will be appended after each item printed.
Let's break down the execution step-by-step.
-
Dictionary Initialization:
The code begins by defining a dictionary
D:D = {'S01':95, 'S02':96}This dictionary contains two key-value pairs:
- The key
'S01'is associated with the value95. - The key
'S02'is associated with the value96.
- The key
-
Looping through the Dictionary:
Next, a
forloop is initiated:for I in D:.As discussed, when you iterate directly over a dictionary
D, the loop variableIwill take on the keys of the dictionary, one by one. -
First Iteration:
In the first pass of the loop,
Iis assigned the first key fromD, which is'S01'.The statement inside the loop is
print(I, end='#').This command prints the current value of
I(which is'S01') and then appends the character specified byend, which is'#'.The output generated at this point is
S01#. -
Second Iteration: …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.