Accessing Information in Python Lists: Understanding Indices and Slicing
Once you have created your own Python list, it's essential to know how to access information within the list. Python uses something called an index to achieve this. Let's take another look at the fan list again, which contains various elements such as strings and floats. The first element in the list has an index of 0, the second element has an index of 1, and so on. This indexing system allows you to retrieve specific elements from your list.
For instance, suppose you want to select the height of Emma, a float with value 1.6. It's located at index 4; therefore, you can access it by using the number three inside square brackets: `fan[3]`. Similarly, if you want to select the string "dad," which is the 7th element in the list, you need to put the index 6 inside square brackets: `fan[6]`. The fact that the index starts at 0 is called zero indexing. This means that if you want to access elements from the beginning of your list, you can use positive indices (i.e., 0, 1, 2, etc.). On the other hand, if you want to access elements towards the end of your list, you need to use negative indices.
Negative indexes are useful when you want to retrieve elements at the end of your list. For example, if you want to get your dad's height, which is located at index -1 (i.e., one position before the last element), you can simply use the number minus 1 inside square brackets: `fan[-1]`. The negative indexes for all list elements are as follows: `-n`, where `n` represents the index of the element.
In addition to using positive and negative indexes, Python also provides something called slicing. Slicing allows you to select multiple elements from a list and create a new list containing those selected elements. To achieve this, you can specify a range using a colon (:) inside square brackets. Let's take another look at the fan list and then try out some code to see what it will return. Can you guess what this piece of code will return: `fan[3:5]`? This code is supposed to return a list with the float 1.68, the string "mum", and the float 1.71 corresponding to the 4th, 5th, and 6th elements in the list.
However, if we run this piece of code, only the elements within an index range from 3 to 4 get returned. The element with index 5 is not included in general. This is because Python follows a specific syntax when using slicing: the index you specify before the colon (:) indicates where the slice starts and is inclusive, while the index you specify after the colon (:) indicates where the slice ends and is exclusive.
Remember that this means where the slice starts is included, while where the slice ends is not. With this in mind, can you tell me what this call will return: `fan[1:3]`? You probably guessed correctly that this call gives you a list with three elements corresponding to the elements with index 1, 2, and 3 of the fan list.
Now that we've explored the basics of indexing and slicing in Python lists, it's time to move on to the exercises. There, you'll continue to work on the list you've already created yourself, using different sub-setting methods to get exactly the pieces of information you need.
"WEBVTTKind: captionsLanguage: enafter you've created your very own Python list you'll need to know how to access information in the list Python uses something called an index to do this let's have a look at the fan list again the first element in the list has index 0 the second element has index 1 and so on suppose that you want to select the height of Emma the float one point six a it's the 4th element so it has index trait to select it you use three inside square brackets similarly to select the string dad from the list which is the 7th element in the list you'll need to put the index 6 inside square brackets now the fact that the index starts at 0 is called zero indexing you can also count backwards using negative indexes this is useful if you want to get some elements at the end of your list to get your dad's height for example you'll need the index minus one these are the negative indexes for all list elements this means that both these lines return the exact same result apart from indexing there's something called slicing which allows you to select multiple elements from a list thus creating a new list you can do this by specifying a range using a colon let's first have another look at the list and then try this piece of code can you guess what it will return a list with the float 1.68 the string mum and the float 1.71 corresponding to the 4th 5th and 6th elements in the list maybe let's see what the output is apparently only the elements within an X 3 & 4 get returned the element with index 5 is not included in general this is the syntax the index you specify before the colon so where the slice starts is included while the index you specify after the colon where the slice ends is not remember this with this in mind can you tell me what this call will return you probably guess correctly that this call gives you a list with three elements corresponding to the elements with index one two and three of the fam list you can also choose to just leave out the index before or after the colon if you leave out the index where the slash should begin you're telling Python to start the slice from index 0 like this example if you leave out the index where the slice should end you include all elements up to and including the last element of the list like here now it's time to head over to the exercises where you'll continue to work on the list you've already created yourself you'll use different sub setting methods to get exactly the pieces of information you need time to jump back into learning by doingafter you've created your very own Python list you'll need to know how to access information in the list Python uses something called an index to do this let's have a look at the fan list again the first element in the list has index 0 the second element has index 1 and so on suppose that you want to select the height of Emma the float one point six a it's the 4th element so it has index trait to select it you use three inside square brackets similarly to select the string dad from the list which is the 7th element in the list you'll need to put the index 6 inside square brackets now the fact that the index starts at 0 is called zero indexing you can also count backwards using negative indexes this is useful if you want to get some elements at the end of your list to get your dad's height for example you'll need the index minus one these are the negative indexes for all list elements this means that both these lines return the exact same result apart from indexing there's something called slicing which allows you to select multiple elements from a list thus creating a new list you can do this by specifying a range using a colon let's first have another look at the list and then try this piece of code can you guess what it will return a list with the float 1.68 the string mum and the float 1.71 corresponding to the 4th 5th and 6th elements in the list maybe let's see what the output is apparently only the elements within an X 3 & 4 get returned the element with index 5 is not included in general this is the syntax the index you specify before the colon so where the slice starts is included while the index you specify after the colon where the slice ends is not remember this with this in mind can you tell me what this call will return you probably guess correctly that this call gives you a list with three elements corresponding to the elements with index one two and three of the fam list you can also choose to just leave out the index before or after the colon if you leave out the index where the slash should begin you're telling Python to start the slice from index 0 like this example if you leave out the index where the slice should end you include all elements up to and including the last element of the list like here now it's time to head over to the exercises where you'll continue to work on the list you've already created yourself you'll use different sub setting methods to get exactly the pieces of information you need time to jump back into learning by doing\n"