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)
  1. A. [(1, 0), (2, 0), (2, 1)]
  2. B. [(1, 0), (2, 0)]
  3. C. [(0, 0), (1, 0), (2, 0)]
  4. 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

Exams that ask this topic