ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
print(list(range(10))[2:8:3], list(range(10))[::4])
  1. A. [2, 5, 8] [0, 4, 8]
  2. B. [2, 5] [0, 4, 8]
  3. C. [2, 5] [0, 4]
  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

Exams that ask this topic