1
00:00:04,400 --> 00:00:09,599
We've seen that you can use negative
start and stop values in a slice. You can

2
00:00:09,599 --> 00:00:13,770
also use a negative step. There's only
one thing to watch out for, and it's

3
00:00:13,770 --> 00:00:18,060
one of those things that's obvious, once
it's been pointed out. If I asked you to

4
00:00:18,060 --> 00:00:23,100
count backwards from 99, and stop when
you reach 100, how would you get on?

5
00:00:23,100 --> 00:00:27,380
You'd tell me it's impossible, of course, and you can't possibly get to 100 by

6
00:00:27,390 --> 00:00:32,940
counting backwards from 99. Well, neither can Python. Now that you know that, you

7
00:00:32,940 --> 00:00:37,020
won't make the most common error that
people make when using negative steps in

8
00:00:37,020 --> 00:00:40,980
a slice. Alright, so let's see some
examples. We want to create a new Python

9
00:00:40,980 --> 00:00:44,600
file called sliceback.

10
00:00:52,700 --> 00:01:06,360
So I'm going to start by typing letters
equals and the alphabet abcdefg etc.

11
00:01:06,360 --> 00:01:13,020
Then on line 3 I'm going to type
backwards equals letters square brackets

12
00:01:13,020 --> 00:01:19,990
25:0:-1 then the closing
right square bracket, and we're

13
00:01:19,990 --> 00:01:24,759
going to print backwards.
Alright so I've created a slice that

14
00:01:24,759 --> 00:01:30,970
starts at index 25, which is the z, and
I set the stop value to 0 and use the

15
00:01:30,970 --> 00:01:35,770
step of -1. So what do you expect
the output to be here? So pause the video

16
00:01:35,770 --> 00:01:41,250
and have a careful think, and come back
when you think you've got the answer.

17
00:01:42,600 --> 00:01:48,800
Alright, so let's run the program now
to see if you were right.

18
00:01:48,800 --> 00:01:54,300
So we get the letters of the alphabet in reverse order
from z down to b. Notice that the letter

19
00:01:54,310 --> 00:02:00,430
a isn't included. We used a stop value of
zero, and slices extend up to, but not

20
00:02:00,430 --> 00:02:05,020
including, the stop value. Before I ask
you another question, let's review what

21
00:02:05,020 --> 00:02:10,389
we've got here, on line 3. Because
we're using a negative step, the start

22
00:02:10,389 --> 00:02:14,620
value must be greater than the stop
value. Our slice starts at index position

23
00:02:14,620 --> 00:02:20,350
25, and it extends down to index position
0, but doesn't include the character

24
00:02:20,350 --> 00:02:25,150
at that position. We're stepping by -1,
 which produces all the letters from

25
00:02:25,150 --> 00:02:30,190
z down to b. So my second question is,
what stop value will let the slice

26
00:02:30,190 --> 00:02:34,660
include the letter a? There's two obvious
answers to that but only one of them

27
00:02:34,660 --> 00:02:39,310
works. So have a think and run your code,
to try your answer before starting the

28
00:02:39,310 --> 00:02:42,840
video again. So pause the video now.

29
00:02:43,520 --> 00:02:47,780
Alright so how did you get on? So one
obvious answer; if a slice doesn't

30
00:02:47,780 --> 00:02:52,900
include the position at the stop value,
is to use a stop value of -1.

31
00:02:52,900 --> 00:02:56,720
But unfortunately that's the one that
doesn't work, so if we try that, and use a

32
00:02:56,720 --> 00:02:59,660
stop value of -1,

33
00:02:59,660 --> 00:03:04,520
when we run it,
we don't get any output. So you may have

34
00:03:04,520 --> 00:03:08,750
worked out why. Remember that negative
stop values count backwards from the

35
00:03:08,750 --> 00:03:14,030
end of the sequence. Minus 1 means the
last character in the string, which means

36
00:03:14,030 --> 00:03:19,340
we've requested a slice that extends
from the z up to, but not including, the z,

37
00:03:19,340 --> 00:03:23,810
and therefore we get nothing. The
other obvious answer is to omit the

38
00:03:23,810 --> 00:03:28,190
stop value. Python will default to the
start or end of the sequence, if we don't

39
00:03:28,190 --> 00:03:32,750
specify start or stop values. So it's
clever enough to work out that it should

40
00:03:32,750 --> 00:03:37,280
reverse the defaults when we're using a
negative step. So if I change that now,

41
00:03:37,280 --> 00:03:44,710
get rid of the -1 for our stop. Run
this, and you can see that the code works.

42
00:03:44,710 --> 00:03:49,850
With a negative step, the start value
will default to the end of the string,

43
00:03:49,850 --> 00:03:55,310
and the stop value defaults to the start
of the string. That means we can ommit

44
00:03:55,310 --> 00:04:03,100
our start value as well. So we can go back
and do that, and if we run it now,

45
00:04:03,100 --> 00:04:10,220
once again we get the string reversed. A slice of
::-1 is a Python idiom.

46
00:04:10,220 --> 00:04:15,080
When you see that, you'll recognize it as
reversing the sequence. I'll cover a few

47
00:04:15,080 --> 00:04:18,769
more slicing idioms in the next
video. There's not much else that

48
00:04:18,769 --> 00:04:23,150
I can say about slicing backwards. Just
use a negative step and make sure the

49
00:04:23,150 --> 00:04:27,530
start value is greater than the stop
value. Experiment with different values

50
00:04:27,530 --> 00:04:31,550
and make sure you're comfortable with
what a negative step produces. I'll

51
00:04:31,550 --> 00:04:35,060
finish this video with a challenge, and
go over the solution to that challenge

52
00:04:35,060 --> 00:04:37,200
in the next video.

53
00:04:56,790 --> 00:05:00,090
That's your challenge. Have a go at that,
and as mentioned, I'll go through the

54
00:05:00,090 --> 00:05:03,770
solution to it in the next video.

