What is nums[1:3] if nums = [10, 20, 30, 40, 50]?
Options
- [10, 20, 30]
- [20, 30] (Correct option)
- [20, 30, 40]
- [10, 20]
Answer
[20, 30]
Why
A slice [start:stop] takes items from position start up to, but NOT including, stop.
Position 1 is 20, position 2 is 30, and position 3 (40) is excluded. Result: [20, 30].
Leaving a side empty means 'from the beginning' or 'to the end': nums[:2] is [10, 20].
Remember
Slices include start and exclude stop.