ProgrammingHardProduct Company SDE · Python internals
What does the following Python 3 code print? a, *b, c = [1, 2, 3, 4, 5] print(a, b, c)
- A. 1 2 5
- B. 1 [2, 3, 4] 5
- C. 1 [2, 3] 4
- D. [1] [2, 3, 4] [5]
Correct answer: B. 1 [2, 3, 4] 5
Starred assignment makes b grab every element not claimed by a (first) and c (last): a=1, b=[2,3,4], c=5.
More Python Programming questions
- What does the following Python 3 code print? def power(base, exp=2): return base exp print(power(3), power(3, 3), power(exp=3, base=2))
- What does the following Python 3 code print? class Stack: def init(self): self.items = [] def len(self): return len(self.items) def…
- What does the following Python 3 code print? class Range2: def init(self, n): self.n = n def getitem(self, i): if i >= self.n: raise…
- What does the following Python 3 code print? import itertools print(list(itertools.chain.fromiterable([[1, 2], [3], [4, 5, 6]])))
- What does the following Python 3 code print? import itertools print(list(itertools.product([1, 2], [3, 4])))
- What does the following Python 3 code print? import itertools print(list(itertools.combinationswithreplacement([1, 2, 3], 2)))