ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: B. [2, 4, 6] [6, 4, 2] [5, 4, 3]

a[1::2] takes indices 1, 3, 5. a[::-2] starts at the last index and steps back two places. a[4:1:-1] goes from index 4 down to index 2, which includes 5, 4 and 3.

More Python Programming questions

Exams that ask this topic