ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from itertools import product
print(list(product('ab', repeat=2)), list(product([0, 1], 'x')))
  1. A. [('aa'), ('ab'), ('ba'), ('bb')] [(0, 'x'), (1, 'x')]
  2. B. [('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b')] [(0, 'x'), (1, 'x')]
  3. C. [('a', 'a'), ('b', 'a'), ('a', 'b'), ('b', 'b')] [(0, 'x'), (1, 'x')]
  4. D. [('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b')] [(0, 'x')]

Correct answer: B. [('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b')] [(0, 'x'), (1, 'x')]

product is the nested loop in order: the rightmost position changes fastest. Each argument gives its items in order, so repeat=2 over 'ab' gives aa, ab, ba, bb as tuples.

More Python Programming questions

Exams that ask this topic