An index selects one position. A slice selects an ordered part.
In Lists, you learned that every item has a position. Indexing lets you select one of those positions. Slicing lets you select a range of positions and gives you a new list containing the selected values. Think: index → one position → one value; slice → range of positions → a new list.
numbers = [10, 20, 30, 40, 50]
print(numbers[2])Python counts list positions from 0: numbers[0] is 10, numbers[1] is 20, and numbers[4] is 50. In numbers[2], the 2 is a position, not the value you want. Follow the path: numbers → index 2 → value 30.
30Positive indexes count from the beginning. Negative indexes count from the end. A slice uses start:stop:step: its start position is included, but its stop boundary is not included.
A useful map: values: 10 20 30 40 50
positive indexes: 0 1 2 3 4
negative indexes: -5 -4 -3 -2 -1
numbers = [10, 20, 30, 40, 50]
print(numbers[-1])
print(numbers[-2])-1 selects the last item, so it prints 50. -2 selects the item before the last, so it prints 40. Think: -1 → last item; -2 → one item before the last.
numbers = [10, 20, 30, 40, 50]
print(numbers[2])
print(numbers[1:4])numbers[2] selects one position and gives you one value: 30. numbers[1:4] selects an ordered range of positions and gives you a new list: [20, 30, 40]. Indexing is for one item; slicing is for a selected part of the list.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])Read numbers[1:4] as “start at index 1 and stop before index 4.” Python takes positions 1, 2, and 3, which hold 20, 30, and 40. It does not take the value at index 4. This makes the slice length predictable: 4 - 1 = 3 selected elements.
numbers = [10, 20, 30, 40, 50]
print(numbers[:3])With no start boundary, numbers[:3] begins at the first item and stops before index 3. The result is [10, 20, 30].
numbers = [10, 20, 30, 40, 50]
print(numbers[2:])With no stop boundary, numbers[2:] starts at index 2 and continues to the end. The result is [30, 40, 50]. With neither boundary, numbers[:] selects the whole list and produces a new list containing all its items.
numbers = [10, 20, 30, 40, 50]
print(numbers[::2])The three slice parts are start:stop:step. Here start and stop are omitted, so Python uses the whole list. A step of 2 takes every second item: [10, 30, 50]. Step 1 means every item; step 2 means every second item.
numbers = [10, 20, 30]
# numbers[10] raises IndexError
print(numbers[:10])Indexing asks for one exact position, so numbers[10] raises IndexError when that position does not exist. A slice can safely extend beyond the available positions: numbers[:10] simply takes the available items and produces [10, 20, 30].
scores = [12, 24, 36, 48, 60]
print(scores[2])
print(scores[-1])
print(scores[1:4])
print(scores[:2])Before running this code, predict four results: the value at index 2, the final value, the positions included by [1:4], and the result of [:2]. The outputs are 36, 60, [24, 36, 48], and [12, 24]. Trace positions and boundaries instead of guessing.
Common beginner mistakes
- Treating the index as the valueFor
numbers = [10, 20, 30],numbers[1]does not ask for the number1. The1is a position, and the selected value is20. - Including the stop position
numbers[1:4]does not include the item at index4. Keep the rule simple: start included; stop excluded. - Thinking -1 means the first item
-1means the last item. Negative indexes count backward from the end of the list. - Expecting an index and a slice to handle missing positions the same wayA missing index can raise
IndexError; a slice that extends beyond the list simply uses the items that exist.
