2.1. for Loop

Sometime, you want to run through all items in a list, doing the same task with each item. When you want to do this, you can use the for loop.

In this example, we will use the list foods. Say we want to print each food in that list. Doing one-by-one will be inconvenient, especially if the length changed. Let's take a look at an example:

We begin with a list at ❶, as we did in previous section. At ❷, we define a for loop. This line tells to take the food from foods. Python then repeat line ❸ for every food inside the foods list.

We can read that for loop like this: "For each food in the list of foods, print the food's name."

> You can choose any name you want for the temporary variable that will be associated with the values in the list. It's a good practice to choose a meaningful name that represents a single item from the list. For example: for student in list_of_students, for number in client_numbers.

Let's try to use f-string in our loop. Say we want to order the food in the list.

You can write as many lines as you like in the for loop. Every indented line after for food in foods: is contained inside that loop. Let's add an extra line:

Any lines after the for loop that are not indented are executed only once. Let's write All payment completed at the very end.

Common Errors in for Loop

Forgetting to Indent

Sometimes people forget to put indent after the for statement. See the error message for the following code.

It will say the error occur on line 3 with the type of error IndentationError. To fix this error, we simply add indentation in the beginning of line 3.


Forgetting to Indent Extra Lines

Loop runs without any errors; but it's not what you want. If this happens, you might forget to indent some of its lines. The following codes will not produce error but see the output.

This kind of "not what I want" error is called logical error. Check the indentation first if you encounter this.

Indenting Unnecessarily

For example, we want to simply print the list of food without any loop.

It says the error occur on line 2 with the type of error IndentationError. Previously, this error occurred because of lack of indentation. Here, print() don't need extra indent because it's not inside a for. Deleting the indent will solve this error.

Forgetting the Colon ( : )

The colon at the end of a for is a fundamental part of for loop.

The example will return SyntaxError, since Python doesn't know what to do. Unlike indentation, Python will not say "you forget the :". Try to be aware of this kind of error.

Exercise 2.1

1. Japanese food
We are opening a Japanese food stall that sells taiyaki, takoyaki, and imagawayaki.
Save those food names in a list, and then use a for loop to print the following sentences
We sell taiyaki!
We sell takoyaki!
We sell imagawayaki!

2. How to Pay
Say you can pay by cash, credit card, food points, or voucher. Using for loop, write the following:
You can pay using
- cash
- credit card
- food points
- voucher

3. Fix it!
Uncomment no.3 block. Try to fix the following code so the result would be
Tasty for Japanese
Tasty for foreigners
Tasty for EVERYONE!

4. Fix it again!
Uncomment no.4 block. Then, try to fix the code so the result would be
Open hours:
Mon - Thu : 10:00 - 17:00
Fri : 10:00 - 21:00
Sat, Sun : 10:00 - 23:00
We are closed during holidays.