Shopping List (Solution)
00:00 Here we are in IDLE. I’ve pasted just the tasks into this IDLE session just so I am going to remember what are the actual strings that I need to add and which methods am I supposed to use to do it.
And the first one was to create a list named
food with two elements,
"beans". I’ll start my
food list by opening up and closing the square brackets and then putting two string elements in there.
and then I can pass in an iterable here as it’ll nicely tells us here. So I will need another list, or it could be a tuple as well. But I’m going to use a list and I will add to that list
02:14 And then I want to print the first two items, which means I want to start at the beginning so I can omit the first index and just start with the colon that’s going to start slicing at the beginning.
And I’m going to go up to index two, which means that it’ll go up to, but not including,
"broccoli". So we’ll get
"broccoli", which is at index two, is not going to be included anymore.
And these are the first two items of the list. So I get a list returned from slicing that contains the first two elements. And again, if you wanted to actually print that out from a script, you just have to pass it as an argument to the
And then the last task here, print the last item in
food using index notation. So the last item, I can access it with negative indexing. So I can say
food, open up the square brackets, and then pass in the index
-1, which gives me the final element in the list.
Become a Member to join the conversation.