ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
print(list(range(10))[2:8:3], list(range(10))[::4])
- A. [2, 5, 8] [0, 4, 8]
- B. [2, 5] [0, 4, 8]
- C. [2, 5] [0, 4]
- D. [3, 6] [0, 4, 8]
Correct answer: B. [2, 5] [0, 4, 8]
The slice 2:8:3 takes indices 2 and 5, because index 8 is excluded. The slice ::4 takes indices 0, 4 and 8.
More Python Programming questions
- What is printed? python print(not [] is [], (not []) is [])
- What is printed? python print(sum(map(int, '1234')), '1234'[::-1] == '4321')
- What is printed? python import heapq pts = [(1, 3), (-2, 2), (2, -2), (0, 1)] print(heapq.nsmallest(2, pts, key=lambda p: p[0] 2 + p[1] 2))
- What is printed? python d = {} try: d[[1]] = 2 except TypeError as e: print(type(e).name) print(d)
- What is printed? python from functools import reduce print(reduce(lambda a, b: a 10 + b, [1, 2, 3]), reduce(lambda a, b: a - b, [10, 2…
- What is printed? python print(sum(x for x in range(1, 11) if x % 2 == 0) - sum(x for x in range(1, 11) if x % 2))