Now that we can appreciate what loops do for us, let’s start with your first type of loop, a
for loop, a type of definite iteration.
for loop, we will know in advance how many times the loop will need to iterate because we will be working on a collection with a predefined length. In our examples, we will be using Python lists as our collection of elements.
for loops, on each iteration, we will be able to perform an action on each element of the collection.
Before we work with any collection, let’s examine the general structure of a
for <temporary variable> in <collection>: <action>
Let’s break down each of these components:
forkeyword indicates the start of a
<temporary variable>that is used to represent the value of the element in the collection the loop is currently on.
inkeyword separates the temporary variable from the collection used for iteration.
<collection>to loop over. In our examples, we will be using a list.
<action>to do anything on each iteration of the loop.
Let’s link these concepts back to our
ingredients example. This
for loop prints each
ingredients = ["milk", "sugar", "vanilla extract", "dough", "chocolate"] for ingredient in ingredients: print(ingredient)
In this example:
<action>performed on every iteration using the temporary variable of
This code outputs:
milk sugar vanilla extract dough chocolate
Some things to note about
A temporary variable’s name is arbitrary and does not need to be defined beforehand. Both of the following code snippets do the exact same thing as our above example:for i in ingredients: print(i)for item in ingredients: print(item)
Programming best practices suggest we make our temporary variables as descriptive as possible. Since each iteration (step) of our loop is accessing an ingredient it makes more sense to call our temporary variable
Notice that in all of these examples the
forloop declaration is included in the loop body and is run on every iteration of the loop.for ingredient in ingredients: # Any code at this level of indentation # will run on each iteration of the loop print(ingredient)
If we ever forget to indent, we’ll get an
IndentationErroror unexpected behavior.
Python loves to help us write elegant code so it allows us to write simple
forloops in one-line. In order to see the below example as one line, you may need to expand your narrative window. Here is the previous example in a single line:for ingredient in ingredients: print(ingredient)
forloops are useful for simple programs. It is not recommended you write one-line loops for any loop that has to perform multiple complex actions on each iteration. Doing so will hurt the readability of your code and may ultimately lead to buggier code.
Let’s practice writing our own
Run the code.
We should get an
IndentationError because the
print(game) line is not indented.
Indent (2 spaces or tab) line 6 so that we don’t get an
IndentationError when you run the code.
Run the code again!
for loop that prints each sport in the list