Python also has a slicing operator, ·[·:·]
for extracting a slice (subsequence) of a
sequence. The number before the colon in the square brackets is the index at which the slice should begin, and
the number after the colon is the index at which the slice should stop. The first index is inclusive, but the
second is exclusive, so that, for example, (4, 5, 6, 7)[1:3]
is (5, 6)
. Negative indices
are also allowed; (4, 5, 6, 7)[-3:-1]
and (4, 5, 6, 7)[1:-1]
and
(4, 5, 6, 7)[-3:3]
all access the same (5, 6)
.
If a slice begins at the start of the sequence, the zero before the colon can be omitted;
(4, 5, 6, 7)[0:2]
would more usually be written (4, 5, 6, 7)[:2]
. Likewise, if a slice
ends at the end of the sequence, the length of the sequence after the colon can be elided;
(4, 5, 6, 7)[2:4]
would more usually be written (4, 5, 6, 7)[2:]
.
Fill in the placeholders so that the actual outputs match the expected outputs.