Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Slices are Python terminology for parts of a container. So as an example, let's go ahead and set up a container here, call it list, and we'll use the mutable list type, and we'll go ahead and assign it a bunch of values here. Now the interesting thing to note about this is that the first element of the list is at index 0.
So if I say list sub 0, and you get the number 1. Of I say sub 1, we are going to get the number 2. So it's important to note that the subscripts of container objects in Python are 0-based. That means that the first item is number 0, the second item is number 1, the third item is number 2, and on like that. So we have 10 items here, and they are numbered 0 through 9. So if I look at list sub 9, we have a number 10.
Also it's interesting to note that if I say, if I want to get items 0 through 5 and I use this syntax here, which is called a slice, that is the slice of the first 5 items. And you'll notice that it gives me 0 through 4, because item number 4 is the one with a 5 in it. So when I said list 0 through 5, that actually means list 0 through 5. But it always gives me everything up to, and not including, the last item.
Now many people find this confusing. I find this confusing. The reasons for it are well explained in the Python documentation, and it's still hard to warp your head around. The thing to remember is that ranges in Python are non-inclusive. It's like if I use the range object, and I say I want the range of 0 through 10, and of course it gives it me in that form because it's an iterable. I have to say 4 i in range of 0 through 10, print i. And it's going to give me 0 through 9.
So ranges in Python are non inclusive. They never include the last item. And that second argument in the slice is a range. So if I say list 0 through 10, it's going to give me 0 through 9, which in our case ends with the number 10. Confusing? Yes. Understandable? Yes, it 'sunderstandable. So let's go ahead and make our list a whole lot bigger. I am going to use a shorthand here.
That's going to give us 100 items in our list. And there they are, 0 through 99. And so if I want to look at item 27, it's going to give me item 27, which in this case because we used a range for the entire thing actually has a 27 in it. And that makes it easy for us to deal with our nice, long list. Okay there is a third argument to this slice operator, which is also optional. The first one, of course, is the index. So if I say list sub 27, that's the first one, and that will give me a slice of just one element at index number 27.
If I say list sub 27:42, this will give me a slice that begins at index 27 and ends at index 42 and is non inclusive, so it actually won't give me that last one. So I get 27 through 41. The last one, if I say list sub 27: 42:3, that's going to give me every third element. So it starts at 27, and then it gives me 30 and 33 and 36 and 39.
And it does not give me 42, because our range is non-inclusive, so it didn't include 42. If I had said list sub 27:43:3, then it would have given me that 42 element. So the slice operator actually has three possible arguments. The first one is the index, where the slice begins. And the second one, which is optional, is the index where the slice ends. If it's omitted, then it simply gives me one element at the index that I specified in the first argument.
The third argument is the step. And that indicates how many elements to step over for each iteration, and what this returns, of course, is an iterator. So if I were to say 4 i in list sub 27 :43:3 : print i. We get the elements from our results. Finally, it's important to note that we can actually assign - and that's the reason that I used a mutable object here for our example - we can actually assign to the slice.
I can say list sub 27:43:3 =, and I can put something in there. Let's say 99, 99, 99, 99, 99, 99. And I can put all 99s in there. And now when I look at my list, where I used to have a 27, I now have a 99, and I have a 99 there, and I have a 99 there, and so all of these elements got replaced with 99s, because you can actually assign to a slice as well as just reading from it.
So slices are incredibly useful. More often than not, you are going to use just the first two forms of it. But occasionally, with that third form, you can do some very powerful things, especially for matrix calculations and any thing that has more than one or tow dimensions. So that's the slice operator. It has the three arguments: the start and the stop and the step. And keep in mind that the stop is non- inclusive, just like ranges in Python are non-inclusive.
So when you specify 43 there as the stop, you have to realize that you are not going to get a value from that stop compartment. You are going to get a value from everything up to it but not including it, because ranges are not inclusive in Python. And the step argument tells it how often to step, or how many to step over. So that's the slice operator.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97526 Viewers
61 Video lessons · 84763 Viewers
71 Video lessons · 68945 Viewers
56 Video lessons · 101390 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.