Step-by-step Breakdown
nums = [100, 200, 300, 400]A list of 4 integers is defined.
range(len(nums)-1, -1, -2)This expands to:
len(nums) - 1 = 4 - 1 = 3 → Start at index 3 (last element)
-1 is the stop (not inclusive), so it goes until index 0
-2 is the step → go backwards by 2 steps each time
Iteration over range(3, -1, -2):
i = 3 → nums[3] = 400
i = 1 → nums[1] = 200
Loop stops before reaching -1.
Output
Summary
-
The code iterates in reverse, skipping every second element.
-
It accesses and prints nums[3], then nums[1].


0 Comments:
Post a Comment