ProgrammingHardInfosys HackWithInfy · TCS CodeVita
What does the following Python 3 code print? import itertools print(list(itertools.product([1, 2], [3, 4])))
- A. [(1, 3), (1, 4), (2, 3), (2, 4)]
- B. [(1, 2), (3, 4)]
- C. [(1, 3), (2, 4)]
- D. [(1, 3), (2, 3), (1, 4), (2, 4)]
Correct answer: A. [(1, 3), (1, 4), (2, 3), (2, 4)]
product computes the Cartesian product; for each value from the first list it pairs with each value from the second, in order.
More Python Programming questions
- What does the following Python 3 code print? import itertools print(list(itertools.combinationswithreplacement([1, 2, 3], 2)))
- What does the following Python 3 code print? import itertools print(list(itertools.compress([1, 2, 3, 4, 5], [1, 0, 1, 0, 1])))
- What does the following Python 3 code print? import itertools print(list(itertools.dropwhile(lambda x: x < 3, [1, 2, 3, 4, 1, 2])))
- What does the following Python 3 code print? import itertools print(list(itertools.takewhile(lambda x: x < 3, [1, 2, 3, 4, 1, 2])))
- What does the following Python 3 code print? class Base: def greet(self): return "base" class Mid(Base): def greet(self): return "mid-" +…
- What does the following Python 3 code print? class A: x = 10 def init(self): self.x = 20 a = A() print(a.x, A.x) A.x = 99 print(a.x, A.x)