![]() Since we did not specify a start or stop value, it assumes that we want to start at the beginning of the sequence and go through the entire list. Or perhaps we only want the elements at even indices. Let’s say that we want every other value in our list, starting from index 0. But what if we don’t want all the elements between those two points? What if we want every other element? That’s where the step value comes in. So far we’ve only specified start and/or stop values, where we start at the start value, and end right before the stop value (since it is exclusive). In other words, because the step value is positive, we are increasing the index by 1 (moving to the right) as we are slicing our list. Thus, we start with index 2, then take 1 step to index 3, then 1 step to index 4, and so on. Since we did not provide a step value, the default step value is 1. If we imagine the indices as between the elements (as seen above), then that is further illustrated, since the index 7 is before the number 35. Thus, we start with index 2, which is the number 10, and go all the way to but not including the index 7, which would be the number 30 at index 6. Notice how the start value is inclusive, but the stop value is exclusive. Variable returns the portion of the variable that starts with position start, and up to but not including position stop.įor example, if we want to obtain all the elements from index 2 up to and including index 6, we can do so as follows: num_list = num_list # ![]() ![]() In other words, if we want all the elements between two specific points in our list, we can use the following format: One way we can slice a sequence, such as a list, is by specifying the start and stop values. Even though there are three possible values that we can provide within the brackets (the start value, stop value, and step/stride value), we don’t actually have to provide all three unless we need to, as we will see below. In other words, the colon (:) in subscript notation make slice notation. In order to slice a sequence, we need to use a colon within square brackets. The syntax for slicing a sequence is as follows: Although this is only useful when the step value is positive, meaning when we are slicing from left to right. Sometimes to understand slicing it can be useful to imagine that the indices are pointing between the elements, rather than to the elements themselves. ![]() Slicing, on the other hand, allows us to obtain a portion from our sequence, such as of a list or string. We just saw how indexing can be used to retrieve individual elements from a list. Now that we’ve reviewed how to index a sequence using positive and negative indexing, let’s look at slicing. If we use an index value that’s not present in our list or out of range, we will receive an Inde圎rror: num_list #Inde圎rror: list index out of range num_list #Inde圎rror: list index out of range Or we can just use the len() function as follows: num_list #40 If we want to obtain the last number in our list, which is 40, we can do so using the index 8 or -1: num_list #40 or num_list #40 In other words, if we want to retrieve the number 10 (or third element) from num_list, we can either use its positive index of 2, or negative index of -7: num_list #10 or num_list #10 ![]() As we may recall, we can either index our sequence starting from the beginning (from the left) with positive indexing starting with index 0, or starting from the end of the sequence (from the right) with negative indexing, starting with index -1. In the example above, we have a list of numbers, num_list, and the numbers below that list represent the indices of the corresponding elements. The index of an element in a sequence is its location in that sequence. Remember that we can access individual elements in a list by using their index within square brackets. Before we start with slicing, let’s briefly review how to index elements in a sequence (specifically a list). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |