1
00:00:00,000 --> 00:00:04,990
Slices are Python terminology for parts of a container.

2
00:00:05,000 --> 00:00:11,990
So as an example, let's go ahead and set up a container here, call it list,

3
00:00:12,000 --> 00:00:24,990
and we'll use the mutable list type, and we'll go ahead and assign it a bunch of values here.

4
00:00:25,000 --> 00:00:28,990
Now the interesting thing to note about this is that the first element of

5
00:00:29,000 --> 00:00:31,990
the list is at index 0.

6
00:00:32,000 --> 00:00:34,990
So if I say list sub 0, and you get the number 1.

7
00:00:35,000 --> 00:00:39,990
Of I say sub 1, we are going to get the number 2.

8
00:00:40,000 --> 00:00:44,990
So it's important to note that the subscripts of container objects in

9
00:00:45,000 --> 00:00:47,990
Python are 0-based.

10
00:00:48,000 --> 00:00:50,990
That means that the first item is number 0, the second item is number 1, the

11
00:00:51,000 --> 00:00:53,990
third item is number 2, and on like that.

12
00:00:54,000 --> 00:00:56,990
So we have 10 items here, and they are numbered 0 through 9.

13
00:00:57,000 --> 00:01:01,990
So if I look at list sub 9, we have a number 10.

14
00:01:02,000 --> 00:01:07,990
Also it's interesting to note that if I say, if I want to get items 0 through

15
00:01:08,000 --> 00:01:15,990
5 and I use this syntax here, which is called a slice, that is the slice of the first 5 items.

16
00:01:16,000 --> 00:01:21,990
And you'll notice that it gives me 0 through 4, because item number 4 is the

17
00:01:22,000 --> 00:01:22,990
one with a 5 in it.

18
00:01:23,000 --> 00:01:28,990
So when I said list 0 through 5, that actually means list 0 through 5.

19
00:01:29,000 --> 00:01:33,990
But it always gives me everything up to, and not including, the last item.

20
00:01:34,000 --> 00:01:37,990
Now many people find this confusing. I find this confusing.

21
00:01:38,000 --> 00:01:42,990
The reasons for it are well explained in the Python documentation, and it's still

22
00:01:43,000 --> 00:01:44,990
hard to warp your head around.

23
00:01:45,000 --> 00:01:50,990
The thing to remember is that ranges in Python are non-inclusive.

24
00:01:51,000 --> 00:01:59,990
It's like if I use the range object, and I say I want the range of 0 through 10,

25
00:02:00,000 --> 00:02:03,990
and of course it gives it me in that form because it's an iterable.

26
00:02:04,000 --> 00:02:11,990
I have to say 4 i in range of 0 through 10, print i. And it's going to give me

27
00:02:12,000 --> 00:02:15,990
0 through 9.

28
00:02:16,000 --> 00:02:19,990
So ranges in Python are non inclusive.

29
00:02:20,000 --> 00:02:22,990
They never include the last item.

30
00:02:23,000 --> 00:02:27,990
And that second argument in the slice is a range.

31
00:02:28,000 --> 00:02:34,990
So if I say list 0 through 10, it's going to give me 0 through 9, which in our

32
00:02:35,000 --> 00:02:37,990
case ends with the number 10. Confusing?

33
00:02:38,000 --> 00:02:39,990
Yes. Understandable?

34
00:02:40,000 --> 00:02:41,990
Yes, it 'sunderstandable.

35
00:02:42,000 --> 00:02:43,990
So let's go ahead and make our list a whole lot bigger.

36
00:02:44,000 --> 00:02:49,990
I am going to use a shorthand here.

37
00:02:50,000 --> 00:02:51,990
That's going to give us 100 items in our list.

38
00:02:52,000 --> 00:02:55,990
And there they are, 0 through 99.

39
00:02:56,000 --> 00:03:02,990
And so if I want to look at item 27, it's going to give me item 27, which in this

40
00:03:03,000 --> 00:03:06,990
case because we used a range for the entire thing actually has a 27 in it.

41
00:03:07,000 --> 00:03:10,990
And that makes it easy for us to deal with our nice, long list.

42
00:03:11,000 --> 00:03:15,990
Okay there is a third argument to this slice operator, which is also optional.

43
00:03:16,000 --> 00:03:18,990
The first one, of course, is the index.

44
00:03:19,000 --> 00:03:23,990
So if I say list sub 27, that's the first one, and that will give me a slice of

45
00:03:24,000 --> 00:03:27,990
just one element at index number 27.

46
00:03:28,000 --> 00:03:36,990
If I say list sub 27:42, this will give me a slice that begins at index 27

47
00:03:37,000 --> 00:03:42,990
and ends at index 42 and is non inclusive, so it actually won't give me that last one.

48
00:03:43,000 --> 00:03:44,990
So I get 27 through 41.

49
00:03:45,000 --> 00:03:52,990
The last one, if I say list sub 27: 42:3, that's going to give me every

50
00:03:53,000 --> 00:03:55,990
third element.

51
00:03:56,000 --> 00:04:01,990
So it starts at 27, and then it gives me 30 and 33 and 36 and 39.

52
00:04:02,000 --> 00:04:08,990
And it does not give me 42, because our range is non-inclusive, so it didn't include 42.

53
00:04:09,000 --> 00:04:17,990
If I had said list sub 27:43:3, then it would have given me that 42 element.

54
00:04:18,000 --> 00:04:22,990
So the slice operator actually has three possible arguments.

55
00:04:23,000 --> 00:04:26,990
The first one is the index, where the slice begins.

56
00:04:27,000 --> 00:04:31,990
And the second one, which is optional, is the index where the slice ends.

57
00:04:32,000 --> 00:04:36,990
If it's omitted, then it simply gives me one element at the index that I

58
00:04:37,000 --> 00:04:38,990
specified in the first argument.

59
00:04:39,000 --> 00:04:40,990
The third argument is the step.

60
00:04:41,000 --> 00:04:45,990
And that indicates how many elements to step over for each iteration, and what

61
00:04:46,000 --> 00:04:48,990
this returns, of course, is an iterator.

62
00:04:49,000 --> 00:05:05,990
So if I were to say 4 i in list sub 27 :43:3 : print i. We get the elements from our results.

63
00:05:06,000 --> 00:05:10,990
Finally, it's important to note that we can actually assign - and that's the reason

64
00:05:11,000 --> 00:05:13,990
that I used a mutable object here for our example -

65
00:05:14,000 --> 00:05:16,990
we can actually assign to the slice.

66
00:05:17,000 --> 00:05:25,990
I can say list sub 27:43:3 =, and I can put something in there.

67
00:05:26,000 --> 00:05:31,990
Let's say 99, 99, 99, 99, 99, 99.

68
00:05:32,000 --> 00:05:35,990
And I can put all 99s in there.

69
00:05:36,000 --> 00:05:42,990
And now when I look at my list, where I used to have a 27, I now have a 99, and I

70
00:05:43,000 --> 00:05:48,990
have a 99 there, and I have a 99 there, and so all of these elements got

71
00:05:49,000 --> 00:05:56,990
replaced with 99s, because you can actually assign to a slice as well as just reading from it.

72
00:05:57,000 --> 00:05:59,990
So slices are incredibly useful.

73
00:06:00,000 --> 00:06:04,990
More often than not, you are going to use just the first two forms of it.

74
00:06:05,000 --> 00:06:09,990
But occasionally, with that third form, you can do some very powerful things,

75
00:06:10,000 --> 00:06:15,990
especially for matrix calculations and any thing that has more than one or tow dimensions.

76
00:06:16,000 --> 00:06:17,990
So that's the slice operator.

77
00:06:18,000 --> 00:06:22,990
It has the three arguments: the start and the stop and the step.

78
00:06:23,000 --> 00:06:29,990
And keep in mind that the stop is non- inclusive, just like ranges in Python are non-inclusive.

79
00:06:30,000 --> 00:06:32,990
So when you specify 43 there as the stop,

80
00:06:33,000 --> 00:06:36,990
you have to realize that you are not going to get a value from that stop compartment.

81
00:06:37,000 --> 00:06:40,990
You are going to get a value from everything up to it but not including it,

82
00:06:41,000 --> 00:06:43,990
because ranges are not inclusive in Python.

83
00:06:44,000 --> 00:06:49,990
And the step argument tells it how often to step, or how many to step over.

84
00:06:50,000 --> 00:07:00,000
So that's the slice operator.

