1
00:00:04,220 --> 00:00:08,420
Alright so how did you get on with the
challenge from the last video.

2
00:00:08,420 --> 00:00:13,380
The first part of the challenge was to slice the
string to produce the letters q, p and o.

3
00:00:13,389 --> 00:00:17,520
So let's have a go at doing that. So I'm
going to type, on line 7, print

4
00:00:17,520 --> 00:00:25,760
parentheses letters square brackets 16:13:-1

5
00:00:25,760 --> 00:00:27,400
So we're standing at position 16.

6
00:00:27,400 --> 00:00:32,320
We're extending backwards, up to, but not
including the character at position 13.

7
00:00:32,320 --> 00:00:39,220
So let's run that, and there's the
letters q, p and o. So next we want a

8
00:00:39,220 --> 00:00:44,379
slice to produce the letters edbca.
The easiest way to include the beginning

9
00:00:49,420 --> 00:00:58,900
is to type print parentheses letters[4::-1]. So if we

10
00:00:58,900 --> 00:01:02,840
run that,

11
00:01:02,840 --> 00:01:04,760
and there's the letters edcba.

12
00:01:04,760 --> 00:01:07,000
Finally, we wanted the last eight

13
00:01:07,000 --> 00:01:11,920
characters in reverse order. So we'll be
using a negative step value, which means

14
00:01:11,920 --> 00:01:17,109
the start will default to the end of the
string and can therefore be omitted.

15
00:01:17,109 --> 00:01:26,780
So to type that, I'm going to do a print
parentheses letters square brackets

16
00:01:26,780 --> 00:01:33,020
:-9:-1

17
00:01:33,020 --> 00:01:41,859
Alright so running that, and there's the last 8 letters, zyxwvuts.
Now there are other solutions you could have

18
00:01:41,859 --> 00:01:45,340
used, especially when returning items
from the beginning and end of the

19
00:01:45,340 --> 00:01:50,049
sequence. Using default values for the
start and stop is recommended, when

20
00:01:50,049 --> 00:01:53,409
trying to return a certain number of
characters from the beginning or end of

21
00:01:53,409 --> 00:01:58,780
a sequence. If you managed to produce the
correct strings with your slices, well done.

22
00:01:58,780 --> 00:02:02,580
Alright, so I'll finish talking
about slices for now, by looking at some

23
00:02:02,580 --> 00:02:06,860
common idioms that Python programmers use and recognize.

24
00:02:06,860 --> 00:02:13,299
In the last video,  I mentioned that a slice of ::-1  is a Python idiom. When you see

25
00:02:13,299 --> 00:02:17,110
that, you'll recognize it as reversing the sequence.

26
00:02:17,110 --> 00:02:21,640
Let's see some more idioms. The first is to return the last n items in a

27
00:02:21,640 --> 00:02:26,920
sequence. So let's say we wanted the last
four characters from letters. So I'm

28
00:02:26,920 --> 00:02:33,510
going to come down, and on line 15 I'm
going to type print parentheses letters

29
00:02:33,510 --> 00:02:40,510
[-4:]
If I run that you should get the last

30
00:02:40,510 --> 00:02:47,830
four letters, wxyz. Whenever you see a
slice with a negative start value and

31
00:02:47,830 --> 00:02:52,480
defaults for the stop and step, that just
returns the end of the sequence.

32
00:02:52,480 --> 00:02:57,720
Our common use of that is to get the last
item by specifying -1 for the start

33
00:02:57,730 --> 00:03:04,209
value. So let's have a go doing that. So I'm
going to type print parentheses then

34
00:03:04,209 --> 00:03:15,640
letters[-1:]
Run that, we get the last item, in this case, the z.

35
00:03:15,640 --> 00:03:21,070
Now you can also use a slice to get
the first character, very similar. We just

36
00:03:21,070 --> 00:03:28,460
type print(letters[1:]

37
00:03:28,460 --> 00:03:33,310
Run that, and I actually did that the wrong
way, because that obviously gave us

38
00:03:33,310 --> 00:03:38,120
everything but the first, starting at sequence position 1. It should have been [:1],

39
00:03:38,120 --> 00:03:43,480
which is what I meant to type. Running
that, we now get the letter a, which is

40
00:03:43,480 --> 00:03:48,040
the first item in the sequence, the first
character. Now that last one is useful

41
00:03:48,040 --> 00:03:52,239
because it doesn't give an error if the
sequence is empty. It may though, be more

42
00:03:52,240 --> 00:03:56,880
natural to get the item at index
position 0, and that works with a string.

43
00:03:56,880 --> 00:04:05,880
So we could also do it as print(letters[0]),

44
00:04:07,320 --> 00:04:11,950
and that's the same, gives us the same
result. That works, but what would happen

45
00:04:11,950 --> 00:04:16,149
if our string was empty? So I'm going to
go up and delete the letters, or the

46
00:04:16,149 --> 00:04:21,959
characters rather, on line 1.
So I'll make that an empty string.

47
00:04:21,959 --> 00:04:27,300
Run the program, and we now get an error, and the
error is: string index out of range.

48
00:04:27,310 --> 00:04:30,940
You can't access the first item of a sequence that doesn't

49
00:04:30,940 --> 00:04:37,210
any items. So looking at the code, the
slice on line 17 produces an empty

50
00:04:37,210 --> 00:04:43,280
string, instead of crashing with an index
error, and just to confirm, if I run it again,

51
00:04:43,280 --> 00:04:49,720
the error's actually talking, or is showing on line 18, as you can see there.

52
00:04:49,720 --> 00:04:55,990
So I had no problems with executing
line 17. So the idiom on line 17 is

53
00:04:55,990 --> 00:05:00,970
useful for getting the first item in a
sequence, without your code crashing.

54
00:05:00,970 --> 00:05:05,440
If the sequence is empty, you'll get an
empty sequence back, and that's often

55
00:05:05,440 --> 00:05:09,080
what you'd want to happen. Alright so
that's the end of slices, for now.

56
00:05:09,080 --> 00:05:14,940
In the next video, we'll look at string
operators. See you in the next video.

