Note 28.4.1.
Remember that the slice always stops just before the second index.
[5:8]
only slices indexes 5-7.range(5, 8, 1)
to loop through them. As we access each one, we will append it to the summerMonths
list that starts empty.list[4]
. To ask for a slice of list
we would say something like: list[4:10]
which says “I want a copy of items number 4 through 9”. Notice that the second value, which is the stopping point, is not included in the slice.[5:8]
only slices indexes 5-7.[5:]
, it means “to the end of the list”. If you use a negative value, it is counted from the end of the list. [-3:-1]
means “the slice from the third to the last item up to but not including the last item”. [-2:]
would mean “the last two items”.[5:2]
, you will always get an empty list as a result. Same if you specify a starting index past the end of the list by doing something like months[15:]
.