1
00:00:04,900 --> 00:00:08,940
Python sequence types let you create a slice.

2
00:00:08,940 --> 00:00:12,660
The only sequence type we've looked at
so far is the string type, so let's

3
00:00:12,660 --> 00:00:16,440
continue with our strings2 program.
What I'll do is delete all the code

4
00:00:16,440 --> 00:00:23,520
except for the comment and the parrot
definition, so let's go ahead and do that.

5
00:00:23,520 --> 00:00:28,620
So we can produce a slice by providing
three numbers separated by colons.

6
00:00:28,620 --> 00:00:34,700
Now these numbers are the start, stop and step values. I'll introduce the step value

7
00:00:34,710 --> 00:00:39,649
soon, but first let's look at slicing without a step. So we're going to type print

8
00:00:39,649 --> 00:00:46,379
parentheses parrot left square bracket 0
colon 6 and right square bracket, then

9
00:00:46,379 --> 00:00:52,710
our right parentheses. I'm going to add
a comment as well, hash N o w, sorry

10
00:00:52,710 --> 00:00:59,159
N o r w e g. So here we're telling
Python to start at position 0 and slice

11
00:00:59,159 --> 00:01:04,199
up to, but not including, position 6,
and I've added a comment with a hash

12
00:01:04,199 --> 00:01:07,350
character there to indicate the result
that we should get. So if we run the

13
00:01:07,350 --> 00:01:15,320
program now, we get the output in the
output pane, Norweg.

14
00:01:15,320 --> 00:01:17,260
So when I said not including,

15
00:01:17,260 --> 00:01:20,940
that's important and something
that new Python programmers often forget.

16
00:01:20,940 --> 00:01:26,970
The last character you specify isn't
included in the slice. It turns out that

17
00:01:26,970 --> 00:01:31,740
not including that last character makes
slices much easier to use, but it'll

18
00:01:31,740 --> 00:01:35,520
probably catch you out at first. In fact,
not including that final value is

19
00:01:35,520 --> 00:01:39,500
something that happens in other places
in Python, such as ranges.

20
00:01:39,500 --> 00:01:43,380
Now don't do this if you're at work, in an open-plan
office, but if you're somewhere safe

21
00:01:43,380 --> 00:01:47,940
where you're not going to get funny
looks, say out loud 10 times, "up to but not

22
00:01:47,940 --> 00:01:53,790
including", "up to but not including", "up to
but not including". Alright, so getting

23
00:01:53,790 --> 00:02:00,090
back to our code, the slice on line 5
produces the characters from index 0 up

24
00:02:00,090 --> 00:02:06,120
to, but not including, index 6. So let's
see some more examples to understand how

25
00:02:06,120 --> 00:02:11,370
slices work. So let's say we wanted the
characters w e from the string, then we'd

26
00:02:11,370 --> 00:02:17,040
slice from position 3, the w, up to but
not including position 5,

27
00:02:17,040 --> 00:02:21,659
the g. So let's type in the code for
that. So on line 6 I'm going to type

28
00:02:21,659 --> 00:02:26,970
print left parenthesis parrot left square
bracket, then we're going to type 3

29
00:02:26,970 --> 00:02:30,620
colon 5, as I mentioned, then our
right square bracket, right parenthesis,

30
00:02:30,620 --> 00:02:36,510
and we'll run the program. We should see
w e outputted, and there's w e in the

31
00:02:36,510 --> 00:02:41,099
output pane. So what else can we do?
Well we can get the first word by

32
00:02:41,099 --> 00:02:46,019
getting the slice from position 0, up to
but not including position 9. So let's try that.

33
00:02:46,019 --> 00:02:54,420
So print left parenthesis parrot
left square bracket 0 colon 9, then our

34
00:02:54,420 --> 00:02:58,400
right square bracket and right
parenthesis, and we'll run that,

35
00:02:58,400 --> 00:03:04,139
and there's the entire word, Norwegian. When creating a slice, the first value is the

36
00:03:04,139 --> 00:03:07,680
position to start at, as you've seen.
That's separated from the position to

37
00:03:07,680 --> 00:03:11,879
stop at with a colon, which again, you've
seen. But if you don't provide a start

38
00:03:11,879 --> 00:03:16,470
value, you still need the colon. So we
could write that last slice, that we've

39
00:03:16,470 --> 00:03:22,440
got on line 7, differently, by typing
print, on line 8, a left parenthesis

40
00:03:22,440 --> 00:03:27,480
parrot left square bracket, then just
colon 9 this time, right square

41
00:03:27,480 --> 00:03:34,260
bracket right parenthesis, and we can run
that. And as you see we get exactly the

42
00:03:34,260 --> 00:03:37,739
same result, and that's this time, the
difference is, we didn't provide a start

43
00:03:37,739 --> 00:03:43,019
value. Basically, our lines 7 & 8 both do
the same thing because the start value

44
00:03:43,019 --> 00:03:47,459
defaults to the start of the sequence,
and obviously on line 7 we included the

45
00:03:47,460 --> 00:03:50,780
start value and line 8, we didn't include it.

46
00:03:50,780 --> 00:03:56,240
Alright so let's look at some slides to explain exactly what's going on here.

47
00:04:44,700 --> 00:04:46,650
So at this point you should be able to create a slice

48
00:04:46,650 --> 00:04:50,580
that returns the word blue, so pause the
video and come back when you've had a

49
00:04:50,580 --> 00:04:54,600
go at that.

50
00:04:54,600 --> 00:04:58,620
Alright, so welcome back. So to slice
the word blue from our string, we need to

51
00:04:58,620 --> 00:05:03,380
start at position 10, and extend up to
position 14. So let's do that.

52
00:05:03,380 --> 00:05:09,940
So I'm going to type print left parenthesis
parrot left square bracket, 10 colon 14

53
00:05:09,940 --> 00:05:16,160
right square bracket and right parenthesis, and if we run that, we should see the word Blue.

54
00:05:16,160 --> 00:05:18,820
And there's Blue on the screen. And again,

55
00:05:18,820 --> 00:05:22,840
there's no character at position 14, but
remember that slices extend up to, but

56
00:05:22,840 --> 00:05:28,180
not including, the stop value. We saw that
we can leave out the start value and it

57
00:05:28,180 --> 00:05:32,290
will default to the start of the
sequence. If we leave out the stop value

58
00:05:32,290 --> 00:05:35,830
then it defaults to the end of the
sequence. So that means that we can

59
00:05:35,830 --> 00:05:42,970
rewrite the last line, line 10, on line 11,
as print left parenthesis parrot left

60
00:05:42,970 --> 00:05:47,770
square bracket 10 colon, and this time
we're leaving off the other value. So

61
00:05:47,770 --> 00:05:53,500
we've got a right square bracket and
right parenthesis and if I run that, we

62
00:05:53,500 --> 00:05:58,210
get the same output, in this case, the
word Blue. It's important to include the

63
00:05:58,210 --> 00:06:02,860
colon that separates the start and stop
values. We haven't provided a stop value,

64
00:06:02,860 --> 00:06:06,640
but we still need the colon, so that Python knows that we want a slice.

65
00:06:06,640 --> 00:06:11,740
Without the colon, that would be in
index, and we'd get a single character at

66
00:06:11,740 --> 00:06:16,000
position 10. I mentioned that there are
four different things that square

67
00:06:16,000 --> 00:06:20,650
brackets are used for, and slicing is
the second thing. They're used for indexing

68
00:06:20,650 --> 00:06:25,240
and they used for slicing. If you use
square brackets for slicing, you must

69
00:06:25,240 --> 00:06:30,380
have at least one colon, otherwise it's in fact, an index and not a slice.

70
00:06:30,380 --> 00:06:35,320
Alright so I've got a question for you. Before I ask it, we'll have a quick recap.

71
00:06:35,320 --> 00:06:39,130
If the first number in a slice is
omitted, the slice will start from the

72
00:06:39,130 --> 00:06:43,450
beginning of the string, and if the
second number is omitted, it'll run to

73
00:06:43,450 --> 00:06:47,790
the end of the string. So it's a recap,

74
00:06:48,030 --> 00:06:54,670
print parentheses parrot left square
bracket colon 6 right square bracket

75
00:06:54,670 --> 00:07:00,280
right parenthesis, and print left
parenthesis parrot left square bracket

76
00:07:00,280 --> 00:07:06,430
6 colon, then a right square bracket and right parenthesis. So again, the first number in

77
00:07:06,430 --> 00:07:09,850
a slice is omitted, the slice starts
at the beginning of the string, and if

78
00:07:09,850 --> 00:07:13,980
the second number is omitted, it'll run
to the end of the string.

79
00:07:13,980 --> 00:07:21,040
The slice indices are defined such as string
square bracket colon n + string square

80
00:07:21,040 --> 00:07:26,050
bracket n colon, is the same as the
original string. So to show you what I

81
00:07:26,050 --> 00:07:30,460
mean I'm going to type some code on line
16 this time, print left parenthesis

82
00:07:30,460 --> 00:07:35,820
parrot left square bracket colon 6.
We've got a right square bracket,

83
00:07:35,820 --> 00:07:40,920
+ this time, I'm going to add a space there,
and a plus, another space then parrot

84
00:07:40,920 --> 00:07:45,430
left square bracket 6 colon, then we've
got our right square bracket and our

85
00:07:45,430 --> 00:07:48,360
right parenthesis again.

86
00:07:48,360 --> 00:07:52,740
So if we run this,

87
00:07:52,740 --> 00:07:55,880
you can see, in fact, we've got the

88
00:07:55,880 --> 00:07:59,710
entire Norwegian Blue outputted to the
screen. So at this point, my question to

89
00:07:59,710 --> 00:08:04,030
you is, what happens if we only have a
colon in the slice? In other words, what

90
00:08:04,030 --> 00:08:06,980
will this produce?

91
00:08:06,980 --> 00:08:12,490
print left parenthesis
parrot left square bracket colon and right

92
00:08:12,490 --> 00:08:16,330
square bracket parentheses. So have a
think about that and see if you can

93
00:08:16,330 --> 00:08:19,840
figure it out before running the program.
Pause the video now and come back when

94
00:08:19,840 --> 00:08:22,500
you've got an answer.

95
00:08:24,140 --> 00:08:26,800
Alright so welcome back. Did you work it out?

96
00:08:26,800 --> 00:08:29,060
Well if you don't provide a start value,

97
00:08:29,060 --> 00:08:32,219
the slice starts at the beginning
of the sequence. So there's no start

98
00:08:32,219 --> 00:08:36,929
value on line 18, so we start at the
beginning. Now if you don't provide a stop

99
00:08:36,929 --> 00:08:40,890
value, the slice extends up to the end of
the sequence, and we haven't got a stop

100
00:08:40,890 --> 00:08:45,029
value on line 18 either. So that means
the slice will start at the beginning

101
00:08:45,029 --> 00:08:51,020
and extend right up to the end. So if we
run the program now,

102
00:08:51,020 --> 00:08:53,760
we get the complete string printed out.

103
00:08:53,760 --> 00:08:57,260
So you might be wondering at this point, why we'd don't want to do that.

104
00:08:57,260 --> 00:08:59,840
When dealing with strings you probably wouldn't, and I'll

105
00:08:59,840 --> 00:09:03,200
come back to it when we look at
another sequence type, a list.

106
00:09:03,200 --> 00:09:07,740
When dealing with lists, a slice like this one, that
produces a copy of the entire original,

107
00:09:07,740 --> 00:09:12,240
can be very useful. I've covered it now
because we're looking at start and stop

108
00:09:12,240 --> 00:09:16,290
values in a slice, and we'll see some
practical applications of it, later in

109
00:09:16,290 --> 00:09:19,040
the course. Alright I'm going to stop the video here.

110
00:09:19,040 --> 00:09:20,620
Before watching the next video,

111
00:09:20,620 --> 00:09:24,500
experiment with slices with different strings. So a good one that you

112
00:09:24,500 --> 00:09:29,660
might want to experiment with, would be
this one which I'll add to the source code.

113
00:09:29,660 --> 00:09:41,260
So letters equals, and we're going to
add the letters of the alphabet,

114
00:09:41,260 --> 00:09:42,240
like so.

115
00:09:42,240 --> 00:09:46,520
So that can be a good string to actually
experiment with, but you can use any

116
00:09:46,520 --> 00:09:50,500
strings you want. Create some slices and
make sure you get the results you wanted.

117
00:09:50,500 --> 00:09:54,680
Once you've done, that I'll see you in the next video.

