Slice notation is [start:stop:step]. This means "begin at start, then increase by step until you get to end." It's similar to this construct:
counter = 0
stop = 10
step = 1
while counter < stop:
print(counter)
counter += step
This will produce, as expected, 0 through 9.
Now imagine if we tried it with the values you're using:
counter = 2
stop = 5
step = -1
while counter < stop:
print(counter)
counter += step
If we actually executed this, it would print 2, then add -1 to that to get 1, then print 1 and add -1 to that to get 0, then it would be -1, -2, and so on, forever. You would have to manually halt execution to stop the infinite loop.
If you leave start or stop empty, as in [:4] or [::-1], this indicates the beginning or end of the sequence, as determined by the step. Python will go forwards with a positive step and backwards with a negative step (trying to use a step of 0 produces an error).
>>> l[2::]
[3, 4, 5, 6, 7, 8]
>>> l[2::-1]
[3, 2, 1]
>>> l[:2:]
[1, 2]
>>> l[:2:-1]
[8, 7, 6, 5, 4]
If you specify a start, end, and step that couldn't work (an empty step defaults to 1), Python will simply return an empty sequence.
l[2:5][::-1]Doing it in different square brackets, it works. - user5077528l[4:1:-1]using a negative step. -1 is not reversing it is stepping - Padraic Cunninghaml[2:5:2]does work I'm intrigued - EdChum2and stepping-1trying to get to5, it makes no sense to step from 2 to 5 going backwards - Padraic Cunningham