1 00:00:00,730 --> 00:00:04,790 Slices don't have to include everything from the first index to the second. 2 00:00:04,790 --> 00:00:06,190 Using what's called a step, 3 00:00:06,190 --> 00:00:09,300 you can make slices that skip every other item for example. 4 00:00:09,300 --> 00:00:11,630 Actually Python slices are a lot like pizza slices. 5 00:00:11,630 --> 00:00:14,936 Sometimes you want to slice with everything on, I mean in it. 6 00:00:14,936 --> 00:00:17,790 Sometimes you just wanna slice to just certain toppings. 7 00:00:17,790 --> 00:00:19,300 I think this analogy got away from me. 8 00:00:19,300 --> 00:00:22,460 So before I get any hungrier, let's jump back into work spaces and 9 00:00:22,460 --> 00:00:24,450 see how to do slices where we skip some items. 10 00:00:25,500 --> 00:00:30,080 It's easiest to see how slices work, especially slices with steps 11 00:00:30,080 --> 00:00:33,680 when you're working with something that you already know the order of. 12 00:00:33,680 --> 00:00:36,619 Since I want this to be understandable no matter where you are in the world, 13 00:00:36,619 --> 00:00:37,470 let's use numbers. 14 00:00:37,470 --> 00:00:39,958 We all know our numbers from zero to 20, right? 15 00:00:39,958 --> 00:00:41,482 Okay, so let's get a list. 16 00:00:41,482 --> 00:00:44,160 I'm gonna use the range function to make it. 17 00:00:44,160 --> 00:00:49,293 So numbers = list(range[20]). 18 00:00:49,293 --> 00:00:53,841 And if I look at numbers it's 0 through 19. 19 00:00:53,841 --> 00:00:56,531 Now if you're not familiar with the range function as a handy way to 20 00:00:56,531 --> 00:00:57,860 generate a bunch of numbers. 21 00:00:57,860 --> 00:01:01,650 It gives you back a special type of data though called a range object. 22 00:01:01,650 --> 00:01:06,310 Range objects can be sliced but they're not very transparent, they don't look and 23 00:01:06,310 --> 00:01:07,740 act exactly like lists. 24 00:01:07,740 --> 00:01:11,840 So I'm wrapping up the list in order to make it a list like we're all used to. 25 00:01:11,840 --> 00:01:15,180 So now, let's try to go through the list and get every even number. 26 00:01:16,465 --> 00:01:20,430 So we can do numbers, we want the entire list, and then before we can put in 27 00:01:20,430 --> 00:01:24,260 the step argument to put it another colon, and then what the step is. 28 00:01:24,260 --> 00:01:26,190 There we go that looks like even numbers. 29 00:01:26,190 --> 00:01:27,850 Since when the entire list from front to back, 30 00:01:27,850 --> 00:01:30,890 like I said we leave off the start and stop indexes. 31 00:01:30,890 --> 00:01:34,190 We have another colon and the final number which is known as the step. 32 00:01:34,190 --> 00:01:37,450 Three pieces to a slice, start, stop, step. 33 00:01:37,450 --> 00:01:39,960 The step tells Python how many steps to take 34 00:01:39,960 --> 00:01:42,710 through the list before it adds another item to the slice. 35 00:01:42,710 --> 00:01:44,530 If you didn't guess, the default step is one, 36 00:01:44,530 --> 00:01:47,270 which gives us every item in the slice. 37 00:01:47,270 --> 00:01:51,320 Slices with steps can still have whatever start and in steps you want to. 38 00:01:51,320 --> 00:01:53,140 For example, I don't have to include 0. 39 00:01:53,140 --> 00:02:00,350 I can do numbers 2 to start at the 0,1, 2 item. 40 00:02:00,350 --> 00:02:03,970 And then still go to the end with every other step, right? 41 00:02:03,970 --> 00:02:07,950 So that still gets me 2 and above all the even numbers. 42 00:02:07,950 --> 00:02:10,110 All of the slice stuff that's both with and 43 00:02:10,110 --> 00:02:12,540 without steps can be applied to any ordered iterable. 44 00:02:12,540 --> 00:02:14,780 That means right now, lists and strings. 45 00:02:14,780 --> 00:02:21,822 So for example if I have Oklahoma, And I slice this with [::2]. 46 00:02:21,822 --> 00:02:23,280 I get 'Olhm'. 47 00:02:23,280 --> 00:02:27,530 So far we've only used positive integers as start, stop and step. 48 00:02:27,530 --> 00:02:29,740 What happens if we use a negative number though? 49 00:02:29,740 --> 00:02:31,910 Well they're kind of like using negative indexes. 50 00:02:33,690 --> 00:02:39,630 So for instance numbers -2: gets 18 and 19, so the last two items, right? 51 00:02:39,630 --> 00:02:41,450 Cuz negative 1, negative 2, negative 3. 52 00:02:41,450 --> 00:02:42,550 All right? 53 00:02:42,550 --> 00:02:45,770 Since we started of index negative 2, which would be 2 as well as from 54 00:02:45,770 --> 00:02:47,930 the end then continue the end, we get the last two items. 55 00:02:47,930 --> 00:02:51,410 Now can I use this to go backwards through the list too? 56 00:02:51,410 --> 00:02:52,230 Let's try it. 57 00:02:52,230 --> 00:02:57,320 Numbers -2 and then -5 which is further back in the list, right? 58 00:02:57,320 --> 00:02:59,620 And I get an empty list, but that makes sense right, 59 00:02:59,620 --> 00:03:02,010 because the default step is a positive one. 60 00:03:02,010 --> 00:03:05,290 Which means a Python moves through the list from left to right. 61 00:03:05,290 --> 00:03:11,110 There's no way to move from left to right, and go from index -2 to index -5. 62 00:03:11,110 --> 00:03:13,910 Well what if I used a negative step? 63 00:03:13,910 --> 00:03:17,120 So, call a -1, it worked. 64 00:03:17,120 --> 00:03:20,050 So, a negative step let's move backwards through the iterable to 65 00:03:20,050 --> 00:03:21,530 generate the slice. 66 00:03:21,530 --> 00:03:25,964 I bet, that that means I can reverse the entire list then with a -1 step. 67 00:03:25,964 --> 00:03:32,514 Some numbers, colon cuz I want the entire list and then my step is -1. 68 00:03:32,514 --> 00:03:33,968 And there's all the numbers backwards. 69 00:03:33,968 --> 00:03:34,611 So that's awesome. 70 00:03:34,611 --> 00:03:37,912 Steps give us so much more control over how we get bits and 71 00:03:37,912 --> 00:03:40,805 pieces from a list or another ordered iterable. 72 00:03:40,805 --> 00:03:44,610 Don't underestimate how handy those negative indexes and steps are. 73 00:03:44,610 --> 00:03:47,730 Being able to move backwards there are a string or a list, especially without 74 00:03:47,730 --> 00:03:51,620 having to know the length of the string of the list is a very useful tool, and 75 00:03:51,620 --> 00:03:54,870 generating a reversed copy of a list by using that negative one step 76 00:03:54,870 --> 00:03:55,880 is amazingly useful.