Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

00:13 And the first one was to create a list named food with two elements, "rice" and "beans". I’ll start my food list by opening up and closing the square brackets and then putting two string elements in there.

00:25 The first one is "rice" and the second one is "beans". I’ll separate them with a comma and like I said before, surrounded with square brackets.

00:34 And then I’ve got my food list looking good. type(food) is list. That doesn’t sound too tasty. Anyway, let’s keep working on this shopping list.

00:46 I should append the string "broccoli" to the food list using the .append() method. So I will say food.append() and then pass in an object.

00:55 And this is going to be the object to add and that should be the string "broccoli".

01:03 Enter, and then the food, wait, got to not add a plus here. The food list now contains "rice", "beans", and "broccoli".

01:13 Next step is to add the strings "bread" and "pizza" using the .extend() method. So we’re at this task right now. So I will say food.extend()

01:25 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 "bread" and "pizza".

01:42 So I create a new list element that contains two strings, "bread" and "pizza". And I pass it as an argument to the .extend() method that I’m calling on the food object, pressing enter.

01:54 And looking at food gives me the extended food list. Now it contains "rice", "beans", "broccoli", "bread", and "pizza".

02:02 Next step is to print the first two items in the food list using slice notation. I can do that by using the variable name food, then opening up square brackets.

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.

02:24 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 "rice" and "beans", but "broccoli", which is at index two, is not going to be included anymore.

02:39 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 print() function.

02:54 I’ll probably keep not using the print() function because in here in the interactive IDLE shell, I can just inspect the output like this.

03:03 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.

03:22 And that is "pizza". All right, that’s the shopping list. Sounds like a relatively healthy meal. I would say just make sure you get a good pizza.

Become a Member to join the conversation.