ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
pairs = [(i, j) for i in range(3) if i for j in range(i) if j != 1] print(pairs)
- A. [(1, 0), (2, 0), (2, 1)]
- B. [(1, 0), (2, 0)]
- C. [(0, 0), (1, 0), (2, 0)]
- D. [(2, 0)]
Correct answer: B. [(1, 0), (2, 0)]
Clauses read left to right like nested loops: i = 0 is skipped by `if i`; i = 1 gives j = 0; i = 2 gives j = 0 and j = 1, but j = 1 is filtered out.
More Python Programming questions
- What is printed? python 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)
- 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])