Question:
\(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]))
\)
\(निम्नलिखित पायथन कोड का अपेक्षित आउटपुट क्या है?
print("\nDictionary Iteration")
d = dict()
d['lmn'] = 123
d['abc'] = 345
for i in d :
print("%s %d" %(i, d[i]))
\)
Options:
- Dictionary Iteration
lmn 123
abc 345 - Dictionary Iteration’
123 lmn
abc 345 - Dictionary Iteration’
123 lmn
345 abc - Dictionary Iteration’
lmn 345
abc 123
- Dictionary Iteration
lmn 123
abc 345 - Dictionary Iteration’
123 lmn
abc 345 - Dictionary Iteration’
123 lmn
345 abc - Dictionary Iteration’
lmn 345
abc 123
Correct Answer: a)
Explanation:
\(Step-by-step execution of the Python script:
1. `print(“\nDictionary Iteration”)` prints a newline followed by `Dictionary Iteration`.
2. An empty dictionary `d` is initialized.
3. Key-value pairs are added: first `’lmn’: 123`, then `’abc’: 345`. In Python 3.7+, dictionary insertion order is strictly preserved.
4. The loop `for i in d:` iterates through the dictionary keys in insertion order:
– 1st iteration: `i = ‘lmn’`, `d[i] = 123` → prints `lmn 123`
– 2nd iteration: `i = ‘abc’`, `d[i] = 345` → prints `abc 345`
Thus, the complete printed output matches Option 1.\)
\(पायथन कोड का चरणबद्ध निष्पादन:
1. `print(“\nDictionary Iteration”)` एक नई लाइन के साथ `Dictionary Iteration` प्रदर्शित करता है।
2. डिक्शनरी `d` में पहले `’lmn’: 123` और फिर `’abc’: 345` जोड़ा गया। (पायथन 3.7+ में डिक्शनरी इन्सर्शन क्रम सुरक्षित रखती है)।
3. लूप `for i in d:` कुंजियों (keys) पर क्रमबद्ध चलता है:
– पहली बार: `i = ‘lmn’`, `d[i] = 123` → आउटपुट: `lmn 123`
– दूसरी बार: `i = ‘abc’`, `d[i] = 345` → आउटपुट: `abc 345`
अतः सही आउटपुट विकल्प 1 है।\)