Slices take part of a list
Not met yet
items[start:stop] makes a new list from position start up to, but not including, stop. Leave out start to begin at the front, or stop to run to the end, so items[:3] is the first three and items[-2:] the last two. A slice that runs past the end doesn't crash; it gives you what is there. The original list is not changed.
Try it
main.pyOutput
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks items[1:3] includes the item at position 3.
- Thinks a slice that runs past the end of a list raises an IndexError.
- Thinks taking a slice cuts the original list down, rather than making a new list.
- Thinks slice positions count from 1, so items[1:] starts at the first item.