Slices4:29 with Ashley Boucher
Discover slicing in Python.
A slice is a portion of a sequence. 0:01 Much like a range, a slice is created by providing a start, stop, and step value. 0:03 The start and stop values refer to the index of elements in a sequence. 0:08 You're already familiar with the syntax for slicing. 0:12 It's very similar to the syntax for 0:15 accessing an individual element of a sequence. 0:17 You're just expanding on it by telling the interpreter to access several 0:20 elements of the sequence by telling you where to start and stop. 0:23 And if you want, adding a step value. 0:27 A start value of 1, for 0:29 instance, will create a slice that begins with the second element in the sequence. 0:31 And a stop value of 4, 0:35 means the sliced sequence will end at the fifth element in the sequence. 0:37 But just like with ranges, it won't include it. 0:41 This slice will include all of the elements in between. 0:44 All right, let's take a look. 0:47 I'm gonna work down in my command line tool, and I'm gonna copy and 0:49 paste in a Python list with six elements. 0:52 Now, let's say I wanna access the second element in this list. 0:57 I would use the following code, rainbow, then . 1:00 Remember I use the index 1 there instead of 2, 1:07 because python sequence indexing begins at 0. 1:10 So the first element in the list will be index 0 and 1:13 the second will be index 1 and so on. 1:16 So this code gives us access to the second element in the list, the string orange. 1:19 Now if I want the second, third, 1:24 and fourth elements in a list, I would use a slice. 1:26 To do that, I will use pretty similar syntax, 1:29 but I will add a colon after the index 1 and then I'll add the number 4. 1:31 The 1 is my starting index of the slice and the 4 is my ending index. 1:37 The slice will be created with all elements from the second element up to, 1:42 but not including the fifth element. 1:46 So that's basic slicing. 1:49 But that's not all you can do with them. 1:50 Stop values can be excluded if you want your slice to go all the way to the very 1:52 end of your original sequence, including its last element. 1:56 The syntax for that is the same, but 2:00 just leave the stock value blank after the colon. 2:01 So that returned a slice from the fourth element all the way to the end. 2:09 Just like ranges, slices can also accept a step value. 2:14 If we wanna slice that only includes every other element in the sequence, 2:17 we could make a slice with the step value of 2. 2:21 In this example, I'm going to leave the start value blank. 2:27 By doing this, Python assumes I mean for 2:30 the slide to start at the beginning of the sequence. 2:32 Then a lot of colon, then I'll also skip the stop value, 2:35 telling the interpreter that I want the slice to go all the way to the end. 2:38 And then I'll add another colon. 2:43 Then finally, I'll add step value of 2. 2:44 Let's see what it returns. 2:48 Okay, good, it skipped every other element of the list. 2:51 Did you know you can add a negative step value too? 2:54 Wanna handy way to quickly reverse a sequence? 2:57 In our above example, let's change the step value of 2 to -1. 2:59 Pretty neat, right? 3:10 It's important to note that slicing does not change a sequence in place, 3:12 it creates a new sequence. 3:15 The rainbow sequence is still in its initial order 3:17 even after running this code. 3:20 There are methods to reverse or change mutable sequences in place, but 3:23 slicing won't accomplish that. 3:26 Slicing works with all Python sequences, including strings. 3:28 Take a look at this example. 3:33 I can use a slice to grab just my nickname from this string, Ash. 3:40 So I've excluded a start value to indicate that I want to start at the very beginning 3:51 of the string. 3:55 And I've included a stop value to indicate that I want every element of the sequence 3:56 up to but not including the fourth element which has an index of 3. 4:00 Okay, so that's suggestive slicing. 4:05 Open the attached workspace, inside you'll find a file with a couple of 4:07 Python sequences and splices of those sequences. 4:11 For each one, I want you to manipulate the start, stop, and step values. 4:14 Use negative step values, leave values blank, play around until the cause and 4:18 effect of different permutations of start, stop, and step values. 4:23 When you feel you're ready, move on to the next step. 4:26
You need to sign up for Treehouse in order to download course files.Sign up