ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
nums = list(range(10)) print(nums[8:2:-2], nums[-3:], nums[2:8:-1])
- A. [8, 6, 4, 2] [7, 8, 9] []
- B. [8, 6, 4] [9, 8, 7] [7, 6, 5, 4, 3, 2]
- C. [7, 5, 3] [7, 8, 9] []
- D. [8, 6, 4] [7, 8, 9] []
Correct answer: D. [8, 6, 4] [7, 8, 9] []
With step -2 the slice goes from index 8 down towards 2, stopping before 2: 8, 6, 4. nums[-3:] is the last three in normal order. nums[2:8:-1] asks to walk backwards from 2 to 8, which is impossible, so the result is empty rather than an error.
More Python Programming questions
- What is printed? python print(min([3, -4, 2], key=lambda v: v v), max({"a": 3, "b": 1}), max("abc", "b", key=len))
- What is printed? python class Box: def len(self): return 0 b = Box() print(bool(b), "yes" if b else "no", len([b, b]))
- What is printed? python a = [3, 1, 4, 1, 5] a.insert(-1, 9) print(a, a.index(1), a.pop(), a.count(1))
- What is printed? python def g(): print("start", end=" ") yield 1 print("mid", end=" ") yield 2 it = g() print("made", end=" ")…
- What is printed? python x = 1 def f(flag): if flag: x = 2 return x print(f(True), end=" ") try: print(f(False)) except UnboundLocalError…
- What is printed? python class A: pass class B(A): pass try: class C(A, B): pass print("ok", end=" ") except TypeError: print("TypeError"…