ProgrammingHardAccenture · Cognizant · Python
What does the following Python 3 code print? import itertools print(list(itertools.compress([1, 2, 3, 4, 5], [1, 0, 1, 0, 1])))
- A. [1, 0, 1, 0, 1]
- B. [2, 4]
- C. [1, 3, 5]
- D. [1, 2, 3]
Correct answer: C. [1, 3, 5]
compress keeps only the data elements whose matching selector is truthy: positions 0, 2, 4 -> 1, 3, 5.
More Python Programming questions
- 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)
- What does the following Python 3 code print? class A: count = 0 def init(self): A.count += 1 a1 = A(); a2 = A(); a3 = A() print(A.count)
- What does the following Python 3 code print? print(round(2.5), round(3.5), round(2.675, 2))