1 00:00:01,060 --> 00:00:03,200 A slice is a portion of a sequence. 2 00:00:03,200 --> 00:00:08,370 Much like a range, a slice is created by providing a start, stop, and step value. 3 00:00:08,370 --> 00:00:11,900 The start and stop values refer to the index of elements in a sequence. 4 00:00:12,990 --> 00:00:15,525 You're already familiar with the syntax for slicing. 5 00:00:15,525 --> 00:00:17,165 It's very similar to the syntax for 6 00:00:17,165 --> 00:00:20,425 accessing an individual element of a sequence. 7 00:00:20,425 --> 00:00:23,675 You're just expanding on it by telling the interpreter to access several 8 00:00:23,675 --> 00:00:27,290 elements of the sequence by telling you where to start and stop. 9 00:00:27,290 --> 00:00:28,870 And if you want, adding a step value. 10 00:00:29,870 --> 00:00:31,030 A start value of 1, for 11 00:00:31,030 --> 00:00:35,600 instance, will create a slice that begins with the second element in the sequence. 12 00:00:35,600 --> 00:00:37,500 And a stop value of 4, 13 00:00:37,500 --> 00:00:41,620 means the sliced sequence will end at the fifth element in the sequence. 14 00:00:41,620 --> 00:00:44,320 But just like with ranges, it won't include it. 15 00:00:44,320 --> 00:00:47,794 This slice will include all of the elements in between. 16 00:00:47,794 --> 00:00:49,085 All right, let's take a look. 17 00:00:49,085 --> 00:00:52,287 I'm gonna work down in my command line tool, and I'm gonna copy and 18 00:00:52,287 --> 00:00:54,262 paste in a Python list with six elements. 19 00:00:57,236 --> 00:01:00,699 Now, let's say I wanna access the second element in this list. 20 00:01:00,699 --> 00:01:07,045 I would use the following code, rainbow, then [1]. 21 00:01:07,045 --> 00:01:10,210 Remember I use the index 1 there instead of 2, 22 00:01:10,210 --> 00:01:13,490 because python sequence indexing begins at 0. 23 00:01:13,490 --> 00:01:16,120 So the first element in the list will be index 0 and 24 00:01:16,120 --> 00:01:18,330 the second will be index 1 and so on. 25 00:01:19,500 --> 00:01:24,220 So this code gives us access to the second element in the list, the string orange. 26 00:01:24,220 --> 00:01:26,550 Now if I want the second, third, 27 00:01:26,550 --> 00:01:29,710 and fourth elements in a list, I would use a slice. 28 00:01:29,710 --> 00:01:31,570 To do that, I will use pretty similar syntax, 29 00:01:31,570 --> 00:01:36,354 but I will add a colon after the index 1 and then I'll add the number 4. 30 00:01:37,800 --> 00:01:42,050 The 1 is my starting index of the slice and the 4 is my ending index. 31 00:01:42,050 --> 00:01:46,642 The slice will be created with all elements from the second element up to, 32 00:01:46,642 --> 00:01:49,022 but not including the fifth element. 33 00:01:49,022 --> 00:01:50,660 So that's basic slicing. 34 00:01:50,660 --> 00:01:52,880 But that's not all you can do with them. 35 00:01:52,880 --> 00:01:56,700 Stop values can be excluded if you want your slice to go all the way to the very 36 00:01:56,700 --> 00:02:00,170 end of your original sequence, including its last element. 37 00:02:00,170 --> 00:02:01,988 The syntax for that is the same, but 38 00:02:01,988 --> 00:02:04,540 just leave the stock value blank after the colon. 39 00:02:09,760 --> 00:02:14,060 So that returned a slice from the fourth element all the way to the end. 40 00:02:14,060 --> 00:02:17,558 Just like ranges, slices can also accept a step value. 41 00:02:17,558 --> 00:02:21,313 If we wanna slice that only includes every other element in the sequence, 42 00:02:21,313 --> 00:02:23,683 we could make a slice with the step value of 2. 43 00:02:27,644 --> 00:02:30,518 In this example, I'm going to leave the start value blank. 44 00:02:30,518 --> 00:02:32,323 By doing this, Python assumes I mean for 45 00:02:32,323 --> 00:02:35,550 the slide to start at the beginning of the sequence. 46 00:02:35,550 --> 00:02:38,827 Then a lot of colon, then I'll also skip the stop value, 47 00:02:38,827 --> 00:02:43,012 telling the interpreter that I want the slice to go all the way to the end. 48 00:02:43,012 --> 00:02:44,950 And then I'll add another colon. 49 00:02:44,950 --> 00:02:47,045 Then finally, I'll add step value of 2. 50 00:02:48,680 --> 00:02:49,560 Let's see what it returns. 51 00:02:51,180 --> 00:02:54,750 Okay, good, it skipped every other element of the list. 52 00:02:54,750 --> 00:02:57,610 Did you know you can add a negative step value too? 53 00:02:57,610 --> 00:02:59,860 Wanna handy way to quickly reverse a sequence? 54 00:02:59,860 --> 00:03:03,719 In our above example, let's change the step value of 2 to -1. 55 00:03:10,347 --> 00:03:12,200 Pretty neat, right? 56 00:03:12,200 --> 00:03:15,410 It's important to note that slicing does not change a sequence in place, 57 00:03:15,410 --> 00:03:17,380 it creates a new sequence. 58 00:03:17,380 --> 00:03:20,020 The rainbow sequence is still in its initial order 59 00:03:20,020 --> 00:03:21,160 even after running this code. 60 00:03:23,410 --> 00:03:26,470 There are methods to reverse or change mutable sequences in place, but 61 00:03:26,470 --> 00:03:27,800 slicing won't accomplish that. 62 00:03:28,950 --> 00:03:31,930 Slicing works with all Python sequences, including strings. 63 00:03:33,670 --> 00:03:34,487 Take a look at this example. 64 00:03:40,886 --> 00:03:45,267 I can use a slice to grab just my nickname from this string, Ash. 65 00:03:51,185 --> 00:03:55,291 So I've excluded a start value to indicate that I want to start at the very beginning 66 00:03:55,291 --> 00:03:56,495 of the string. 67 00:03:56,495 --> 00:04:00,655 And I've included a stop value to indicate that I want every element of the sequence 68 00:04:00,655 --> 00:04:03,990 up to but not including the fourth element which has an index of 3. 69 00:04:05,165 --> 00:04:07,375 Okay, so that's suggestive slicing. 70 00:04:07,375 --> 00:04:11,285 Open the attached workspace, inside you'll find a file with a couple of 71 00:04:11,285 --> 00:04:14,055 Python sequences and splices of those sequences. 72 00:04:14,055 --> 00:04:18,740 For each one, I want you to manipulate the start, stop, and step values. 73 00:04:18,740 --> 00:04:23,060 Use negative step values, leave values blank, play around until the cause and 74 00:04:23,060 --> 00:04:26,860 effect of different permutations of start, stop, and step values. 75 00:04:26,860 --> 00:04:28,670 When you feel you're ready, move on to the next step.