ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
a = [1, 2, 3, 4, 5, 6, 7, 8]
print(a[::3], a[2::-1], a[-3::2])
  1. A. [1, 4, 7] [3, 2, 1] [6, 8]
  2. B. [1, 4, 7] [3, 2, 1] [6]
  3. C. [1, 4, 7] [2, 1] [6, 8]
  4. D. [1, 3, 5] [3, 2, 1] [6, 8]

Correct answer: A. [1, 4, 7] [3, 2, 1] [6, 8]

a[::3] takes indices 0, 3 and 6. a[2::-1] goes from index 2 back to index 0, which gives 3, 2, 1. a[-3::2] starts at index 5 and steps by two, giving 6 and 8.

More Python Programming questions

Exams that ask this topic