Repaso
EspaƱol
PythonCorePY-48

What is nums[1:3] if nums = [10, 20, 30, 40, 50]?

Options

  1. [10, 20, 30]
  2. [20, 30] (Correct option)
  3. [20, 30, 40]
  4. [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.

Key terms

Practice this question in Repaso