ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
nums = [1, 2, 3, 4, 5]
print([nums[i] for i in range(len(nums) - 1, -1, -2)], nums[::-2][0])
  1. A. [5, 3, 1] 5
  2. B. [5, 3, 1] 1
  3. C. [4, 2] 5
  4. D. [5, 4, 3, 2, 1] 5

Correct answer: A. [5, 3, 1] 5

range from the last index stepping by -2 gives indices 4, 2 and 0, which are 5, 3 and 1. nums[::-2] starts from the last element, so its first item is 5.

More Python Programming questions

Exams that ask this topic