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)- A. 5 [0, 1] {'a': 'a', 'b': 'b'}
- B. outer [0, 1] {'a': 'a', 'b': 'b'}
- C. b [0, 1] {'a': 'a', 'b': 'b'}
- 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
- What is printed? python s = "programming" print(s[-3:-8:-2], s[100:], s[-100:3])
- What is printed? python a = list(range(6)) try: a[::2] = [9, 9] except ValueError as e: print("VE", end=" ") a[1:2] = [] a[:0] = [7, 7]…
- What is printed? python a = [0, 1, 2, 3, 4, 5, 6, 7] del a[::3] print(a, a[-1:0:-2])
- What is printed? python s = slice(1, None, 2) t = "abcdefg" print(t[s], s.indices(len(t)), t[::-1][s])
- What is printed? python a = [1, 2, 3, 4, 5] a[1:4] = "xy" b = a[:] b[::-1] = b print(a, b[0])
- What is printed? python nums = [10, 20, 30, 40, 50] print(nums[-2::-2], nums[1:-1][::-1], nums[::-1][1:-1])