ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
i = "outer"
r = [i for i in range(2)]
s = {i: i for i in "ab"}
for i in range(5, 6):
    pass
print(i, r, s)
  1. A. 5 [0, 1] {'a': 'a', 'b': 'b'}
  2. B. outer [0, 1] {'a': 'a', 'b': 'b'}
  3. C. b [0, 1] {'a': 'a', 'b': 'b'}
  4. D. 1 [0, 1] {'a': 'a', 'b': 'b'}

Correct answer: A. 5 [0, 1] {'a': 'a', 'b': 'b'}

Comprehension variables are private to the comprehension in Python 3, so they never touch i. The ordinary for loop, however, does rebind i and leaves it at its last value, 5.

More Python Programming questions

Exams that ask this topic