What is the expected output of the following Python code? print("\nDictionary…
2023
What is the expected output of the following Python code? print("\nDictionary Iteration") d = dict() d['lmn'] = 123 d['abc'] = 345 for i in d : print("%s %d" % (i, d[i]))
- A.
Dictionary Iteration
lmn 123
abc 345
- B.
Dictionary Iteration
123 lmn
abc 345
- C.
Dictionary Iteration
123 lmn
345 abc
- D.
Dictionary Iteration
lmn 345
abc 123
Attempted by 826 students.
Show answer & explanation
Correct answer: A
Step 1: The code starts by printing "\nDictionary Iteration", which adds a newline and the text.
Step 2: A dictionary d is created, and two key-value pairs are added: d['lmn'] = 123 and d['abc'] = 345.
Step 3: The for loop iterates over the dictionary keys in the order they were inserted, which is 'lmn' then 'abc'.
Step 4: Inside the loop, the print statement uses the format string "%s %d", which prints the key (string) followed by the value (integer).
Step 5: The output will be: Dictionary Iteration followed by lmn 123 and abc 345 on separate lines.