ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
s = list(range(10))
print(s[8:2:-3], s[::-4], s[-3:])
  1. A. [8, 5] [9, 5, 1] [8, 9]
  2. B. [8, 5] [9, 5, 1] [7, 8, 9]
  3. C. [8, 5, 2] [9, 5, 1] [7, 8, 9]
  4. D. [8, 5] [0, 4, 8] [7, 8, 9]

Correct answer: B. [8, 5] [9, 5, 1] [7, 8, 9]

s[8:2:-3] visits indices 8 and 5, and stops before index 2. s[::-4] starts at 9 and steps back by 4 (9, 5, 1). s[-3:] is the last three items.

More Python Programming questions

Exams that ask this topic