Lists Workout
- 05:04
Practise using list functions in Python.
Downloads
No associated resources to download.
Glossary
list list function PythonTranscript
Let's practice all of that new knowledge about lists. In your Jupyter Notebook file, complete the following actions. You're going to create a list called List one containing the integers, 1, 2, 3, 4, and 5. Then you're going to create another list called list two containing the integer, 6, 7, 8, 9, and 10. Then I want you to print the sum of the fourth object in the first list and the first object in the second list. Then print the last two objects as a range from list one and the first three objects as a range in list two. Finally, you're going to create a new list called list three, and it's gonna contain the objects from step four in numerical order. And then you're gonna print the result. So that list three is going to be the last two objects of list one. It's gonna be the first three objects of list two, those five integer combine together and then print the result.
If you did this all in one cell, that's totally fine. I'm going to break it out into multiple cells by adding some of my own so that you can see step by step how everything is working. I'm gonna start by creating list one containing the integers 1 through 5, and that's gonna be contained in square brackets to tell Python that we're creating a list.
Then I'm gonna create list two containing 6 through 10 for a quick check, just for fun, I'm gonna print those lists just to make sure that they were defined correctly And it looks like they're working fine.
I'm gonna add a new cell right below that and now we are going to add the fourth object in list one with the first object in list two. So that's the number 4 from list one and the number 6 from list two. Now remember, python lists are zero indexed, so the number 4, which is in the fourth position of list one is going to be index three, and the number 6, which is the first object in list two, is gonna be index zero. So let's go ahead and add those together. List one in square brackets index three, which will gimme the number four plus list two in square brackets index zero. The number 6. Looks good.
Next, I'm going to print the last two numbers in list one as a range, and the first three in list two as a range.
So I'm gonna call list one, and then in square brackets the index of my first number in my range, so index three, and then a colon will tell Python that I'm calling a range, and since my range ends with the final object in the list, I can just leave that index blank and it will know to include the five.
Next, I'm gonna print list two, and then my starting object is the number 6. So that's my first object in my list, so I can leave that index blank. I'm gonna put a a colon to tell Python it's a range and it automatically knows that blank spot is the first object in the list. And then just the way that Python syntax works is that the second index is the first number that you want to exclude. So here we want to include 6, 7, and 8, and then we want to exclude 9. The index for 9 is three.
So that's saying that blank space is saying start with the first object in the list. The colon is giving me a range and exclude index three, the number 9.
So that gives us 4 and 5 and oh, it looks like I used list one. If we change that to list two, I'm gonna get 6, 7, and 8.
Finally, I'm gonna create a new list called list three, and I'm gonna make that equal to those two ranges that we just printed out, concatenated together or combined together. So list one, and I'm gonna type that range. So index three to the end, and then list two open square brackets. And I'm gonna start with my first object, so I don't need an index and then a colon. And then the first object I want to exclude.
And now lastly, I'm just gonna print that list.
There you go.