Skip to 0 minutes and 3 secondsYou're going to use iteration and lists to create a program which will create and display a shopping list. You'll create this program in two parts, one displaying the shopping list and another creating the list. Now, your challenge will be to combine these two programs together. Now, the first step is to create a program which will loop through all the items in a list and print them to a screen.

Skip to 0 minutes and 28 secondsSo let's create a new program and save it as display shopping.

Skip to 0 minutes and 40 secondsNow we need to create a variable called shopping and add to it the items in our shopping list. So bread, cheese, apple, tomato, and biscuits.

Skip to 1 minute and 12 secondsNow, to loop through each of the items in this list, you will use a for loop.

Skip to 1 minute and 19 secondsFor loops allow code to be run for a set number of times. In our case, it runs for the number of things that we have in our shopping list. Now, the syntax for a for loop looks like this.

Skip to 1 minute and 35 secondsWe use for, and we say element in our list. Then we have our colon. Then we indent the code that we want to run as part of this loop in the same way that we did for a for loop. We say print, do this. Now, to use a for loop to print out each of the items in our shopping list, let's change this program. So we're going to say, for item in shopping, I'm going to say print the item. And if we run this program now, what we can see is the five items from our shopping list have been printed to the screen.

Skip to 2 minutes and 26 secondsNow let's look at how this works one line at a time by using the debugger.

Skip to 2 minutes and 35 secondsSo when we start the debugger, this is going to allow us to look at each one of these lines of code and see actually how it runs. So the first line of code, when we run it, it creates our shopping list. And actually, we can see that a variable is being created called shopping, which is a list of five items. And it starts our for loop. And when the first time it runs our for loop, we can see that a variable called item is created. And it's got the value bread, being the first item in our shopping list. We then run the line of code to print that item, and we see bread appear on the screen.

Skip to 3 minutes and 18 secondsAnd it then goes back to the for loop. And if we run our for loop again, we can see that the item is changed to cheese, the second item in our for loop. And again, if we step over that, it prints the item cheese to the screen. And this continues until there are no more items left in the shopping list, and the for loop exits.

Looping through a list

You are going to use iteration and lists to create a program which will create and display a shopping list for you. You will create this program in two parts: one to displaying the shopping list, and another to create it. Your challenge will be to combine these two programs together.

We’ll start with the code to display the shopping items. The first step is to create a program which will loop through all the items in a list and print them to the screen.

  • Create a new program and save it as display_shopping.

Note: If you are unsure how to do this, take a look at the “If it’s this, then do that” step from Week 2.

  • Create a list variable for your shopping and fill it with some items.
shopping = ["bread", "cheese", "apple", "tomato", "biscuits"]

To loop through each of the items in the list, you will use a for loop. for loops exist in most programming languages, and their purpose is to allow code to be run a set number of times – in our case, equal to the number of things in our shopping list.

The syntax of a for loop in Python looks like this:

for element in list:
    print("do this")
  • Use a for loop to print out each of the items in your shopping list.
for item in shopping:
    print(item)

Note: The print statement is indented under the for loop. Indentation in a for loop works exactly the same as in an if statement; whatever code is indented under the for is run as part of that loop.

  • Run your program.

Mu IDE, showing code in the editor. The first line is setting up the list variable as above, while the second and third lines are the 'for item in shopping:' loop above. The REPL shows the five items from the shopping list, each on a new line.

Let’s break this down and consider what steps the computer takes to carry out this program.

  • First it creates the list called shopping.
shopping = ["bread", "cheese", "apple", "tomato", "biscuits"]
  • The for loop is entered. The first element in the shopping list, "bread", is put into the variable item.
for item in shopping:
  • print is used to display what is in the variable item.
    print(item)
  • item holds the value “bread”, so “bread” is printed.
bread
  • The for then runs again and the second element in the shopping list, "cheese" is put into the variable item.
for item in shopping:
  • print is used to display what is in the the variable item.
    print(item)
  • This time, item holds the value “cheese”, so “cheese” is printed.
cheese
  • The for loop runs again and the third element, "apple", is put into the variable item.
for item in shopping:
  • This continues until there are no more items in the list. Each item has been printed in turn, and then the for loop stops.

Share this video:

This video is from the free online course:

Programming 101: An Introduction to Python for Educators

Raspberry Pi Foundation