Python: Slicing Sequences
First of all lets create a sequence that we can slice:
SeqOne = [0, 1, 2, 3, 4, 5, 6]
Lets say we want to get a range of numbers from this sequence (called slicing the sequence); well if we wanted the numbers 1-4, we would run this:
SeqOne[1:5]
When slicing, the first number is included in the slice, but the last number is excluded.
So if we wanted 2-6, how would we do it?
Well it's quite simple really, we just have to slice it from 2 to 7 (although 7 isn't in the sequence):
SeqOne[2:7]
But what about counting from the back of the array?
Well we can just use negative numbers! For example to output 3 and 4 we could run:
SeqOne[-4:-2]
But how would we ouput 3-6?
Well we can't run [-4:-1] as that would output 3, 4, and 5.
Instead we simply have to leave the last number as nothing:
SeqOne[-4:]
This would output 3 all the way up to 6.
We can also leave out the first number to do the start of the sequence, up to a number - for example:
SeqOne[:5]
The above would output 4.
Back to Python

