1
00:00:04,420 --> 00:00:09,380
So far we've only been providing two
values to our slices, the start and stop

2
00:00:09,380 --> 00:00:15,080
values. But slices can also take a step
value, which defaults to one. So let's see

3
00:00:15,080 --> 00:00:19,189
some examples before we discuss the
syntax more formally. So what I'm going

4
00:00:19,189 --> 00:00:23,400
to do is delete the code, other than the
definition of parrot on line 3,

5
00:00:23,400 --> 00:00:29,810
and I'm going to type on line 5, print
parenthesis parrot square bracket

6
00:00:29,810 --> 00:00:34,820
0:6:2, and close off the square
bracket and parentheses, and we'll put a

7
00:00:34,820 --> 00:00:40,310
comment to the effect of the output
being capital N r e. And on line 6 I'm

8
00:00:40,310 --> 00:00:45,770
going to type print parenthesis parrot left
square bracket 0:6:3 this

9
00:00:45,770 --> 00:00:50,600
time, and right square bracket and
parenthesis, and comment, the output

10
00:00:50,600 --> 00:00:56,570
should be Nw. So the slice on line 5 is
starting from index 0, which is the

11
00:00:56,570 --> 00:01:01,730
capital N, extracting all the characters
up to, but not including index 6, which is

12
00:01:01,730 --> 00:01:05,760
the i, in steps of 2.

13
00:01:33,720 --> 00:01:37,900
So let's confirm that that works by running it, and you

14
00:01:37,909 --> 00:01:41,270
can see we get the output that we
thought we should get, and you can see

15
00:01:41,270 --> 00:01:45,500
that we've got that output. Alright so
another example of a slice where the

16
00:01:45,500 --> 00:01:49,610
step is as follows; so I'm going to start
type some code on line 8. I'm going to start by

17
00:01:49,610 --> 00:01:58,729
typing number equals, and double quotes 9
comma 2 2 3 comma 3 7 2 comma 0 3 6

18
00:01:58,729 --> 00:02:05,920
comma 8 5 4 comma 7 7 5 comma and 8 0 7, and we've got a closing double quote there.

19
00:02:05,920 --> 00:02:12,829
Now on line 9 if I type print
parentheses number then square

20
00:02:12,829 --> 00:02:17,599
brackets again 1 colon colon 4, and
closing off the

21
00:02:17,599 --> 00:02:24,799
right bracket and right parenthesis. Now
if we run that, it doesn't appear to be very

22
00:02:24,799 --> 00:02:29,450
useful but it does illustrate using a
step in slices. So it starts at position 1,

23
00:02:29,450 --> 00:02:34,640
which is the first comma, and it's
slicing every fourth character. The list

24
00:02:34,640 --> 00:02:38,180
extends all the way to the end of the
string and that's because we've omitted

25
00:02:38,180 --> 00:02:42,859
the stop value, and that gives us all the
commas. So I said it didn't appear to be

26
00:02:42,859 --> 00:02:47,180
useful, however, it can be. In order to
show you how it's useful, we're gonna have

27
00:02:47,180 --> 00:02:50,780
to use some things that we'll be looking
at later. I don't like doing this

28
00:02:50,780 --> 00:02:54,680
generally, and I won't be doing it often,
but I think something like that slice

29
00:02:54,680 --> 00:02:58,340
will make more sense if you can see a
practical application of it. So what I'm

30
00:02:58,340 --> 00:03:02,480
going to do is start by changing some of
the separators. At the moment they're all

31
00:03:02,480 --> 00:03:06,889
commas, but it's possible we may have to
process a stream that contains a mix of

32
00:03:06,889 --> 00:03:11,540
separators. So what I'm going to do is 
use a mix of commas, semicolons, colons

33
00:03:11,540 --> 00:03:15,379
and spaces. These are all the things you
might find used as separators when

34
00:03:15,379 --> 00:03:20,810
processing data. So we've got 9 comma
2 2 3. Let's change the comma

35
00:03:20,810 --> 00:03:25,579
after the 2 2 3 to a semicolon,
and then moving on we've got, after

36
00:03:25,579 --> 00:03:30,829
3 7 2, we'll replace that comma
with a colon. The next one after the O 3 6,

37
00:03:30,829 --> 00:03:34,970
we'll put a space there
instead, and we'll leave the next one,

38
00:03:34,970 --> 00:03:37,970
8 5 4, leave that as a
comma, and the one down the end here

39
00:03:37,970 --> 00:03:43,220
before the 8 0 7, we'll add a semicolon
there as well. So if we run this program

40
00:03:43,220 --> 00:03:48,049
again now, we've changed those separators.
You can see with the output there, we've

41
00:03:48,049 --> 00:03:53,690
actually got comma, semi colon, colon
space, comma, semicolon. So why is that

42
00:03:53,690 --> 00:03:58,190
useful? Well if that number string
represents seven values that appear in a

43
00:03:58,190 --> 00:04:03,949
data file, we can use the separator
string to split out the seven values. Now

44
00:04:03,949 --> 00:04:06,799
this is the bit that I don't want you to
worry about. Think of it as a

45
00:04:06,800 --> 00:04:11,320
demonstration. It's all stuff that you'll
be learning about in later videos.

46
00:04:11,320 --> 00:04:15,199
So what I'm going to do is start by binding
a variable to the slice instead of

47
00:04:15,199 --> 00:04:20,089
printing it out. So I'm going to change line
9 there. Instead of print, we're going

48
00:04:20,089 --> 00:04:26,389
to type separators is equal to, and I'll
just remove the parenthesis on the end

49
00:04:26,389 --> 00:04:30,430
there, and then we're going to print
separators.

50
00:04:30,430 --> 00:04:33,980
So we've now going a string containing
all the separators that we used in

51
00:04:33,980 --> 00:04:38,090
numbers. Now it's not necessary to print
it, but on line 10 I'm doing that anyway,

52
00:04:38,090 --> 00:04:41,660
just to confirm that we've got the
separators, and obviously that should

53
00:04:41,660 --> 00:04:46,190
work because we've just only made a small
change there, by binding it to the

54
00:04:46,190 --> 00:04:51,680
separators variable. Now we can use the
separators to split out the individual

55
00:04:51,680 --> 00:04:55,640
values. This is the interesting part. So
what I'm going to do is add that. I'm

56
00:04:55,640 --> 00:05:01,340
going to close down the run window, and
on line 12 I'm going to type values is

57
00:05:00,840 --> 00:05:30,360
values = "".join(char if char not in separators else " " for char in number).split()

58
00:05:30,360 --> 00:05:33,780
Then on the next line

59
00:05:33,850 --> 00:05:49,420
print([int(val) for val in values])

60
00:05:49,420 --> 00:05:54,140
Now I don't expect that code on lines 12 and
13 to make sense. Again, I'm using it to

61
00:05:54,140 --> 00:05:58,820
demonstrate why extracting every third
character from a string might be useful.

62
00:05:58,820 --> 00:06:02,150
As you work through the course, you'll
learn what this code's doing and you'll

63
00:06:02,150 --> 00:06:05,960
be able to write code like that for yourself.
Now if you're typing the code in

64
00:06:05,960 --> 00:06:09,290
as you watch, you may want to copy and
paste those two lines from the

65
00:06:09,290 --> 00:06:13,700
transcript for this video. The source
code is also available in the resources

66
00:06:13,700 --> 00:06:16,940
for this video if you want to do that.
It's very important that you use the

67
00:06:16,940 --> 00:06:21,560
correct type of brackets. The second one
you see me use was a square bracket, and

68
00:06:21,560 --> 00:06:25,280
obviously, I'm using parentheses there,
which some people also call brackets,

69
00:06:25,280 --> 00:06:29,870
as well. So basically, make sure you've
typed it in exactly as you can see it

70
00:06:29,870 --> 00:06:36,110
here in order for it to work. Now if we
run this program, and you can see the output

71
00:06:36,110 --> 00:06:41,930
is now showing us the seven values that
we've extracted from the string, that

72
00:06:41,930 --> 00:06:46,489
we bound to the number variable on line 8.
I think that's pretty impressive when

73
00:06:46,489 --> 00:06:49,309
you consider it's only a few lines of
code there, and I'll just bring that back on

74
00:06:49,309 --> 00:06:53,839
the screen so you can see it. It's very
hard to come up with real-world examples,

75
00:06:53,839 --> 00:06:58,369
when all we've covered so far in the
course are variables, ints and strings, but

76
00:06:58,369 --> 00:07:02,899
hopefully, that demonstration has put the
slice into a useful context. So make sure

77
00:07:02,899 --> 00:07:08,269
though, you understand how that slice
on line 9 is working, and then I'll see

78
00:07:08,269 --> 00:07:11,599
you in the next video. And in that video
we're going to look at slicing backwards.

79
00:07:11,599 --> 00:07:15,129
See you in the next video.

