We give an introduction to for and while loops.
Loops
Loops are useful whenever you have an algorithm that requires multiple uses of the same step. For example, say we wanted to compute the sum for some integer . We have seen in a previous section that this can be done using two variables, one that tracks the current value of the sum and another that tracks whether or not our task is finished (see the flowchart below).
The structure of the flowchart above is very suggestive of why we use the terminology ‘loop’. We will now go over two types of loops: for loops and while loops.
For Loops
The Python syntax for a for loop is as follows:
==============================
for variable in iterable:
code
==============================
In the above code, for indicates the start of a for loop, variable is just a variable name of your choice, in is a keyword, and iterable is a Python object that can be iterated over, like a list or a string. This Python code almost reads as an English statement. It is saying, for each item in iterable, whose value we assign to variable each time, perform the instructions given by code.
The next few examples illustrate how variable takes on the values of iterable.
Below is an example of how to compute using a for loop.
If we wanted to compute for some large or yet unknown value of , we can already see that the previous example won’t cut it. We need some general way of creating useful lists of numbers. We will use the range function to help us with this. The range function is commonly used in for loops. The inputs for the range function are similar to those used for list slicing. See the SageCells below for examples of how to use the range function.
The range function with 1 argument.
The range function with 2 arguments.
The range function with 3 arguments.
Using the range function, we can now write a for loop that computes .
We can modify, improve, and generalize the loop above. We will change out the notation s = s + i and use s += i instead. (The += operator increments whatever is on the left by whatever is on the right.) We will also create a function to compute for any integer .
While Loops
The Python syntax for a while loop is as follows:
==============================
while condition:
code
==============================
In the above code, while indicates the start of a while loop, condition is the comparison that is made to determine if the while loop is finished, and code is executed as long as condition holds true.
Below is an example of how to compute using a while loop.
Note that for a while loop, if condition is always true, then your algorithm will never finish running. Unlike a for loop, you must be sure to update the variables associated with the condition being checked at each iteration. In the example above, omitting the line i += 1 would create an infinite loop.
For vs. While
A common question when learning about these two types of loops is: Which one should I use? In many cases either will work just fine. For loops are preferred when the number of times the instructions are to be repeated is known. While loops are preferred when the number of times the instructions are to be repeated is unknown.
For example, we have already seen that computing for some integer can be done with a for loop using just a few lines of code. Consider the following related question: What is the smallest integer such that ?
In this case, a for loop may not be as helpful as a while loop since we do not know how many times to iterate. Using a while loop, we can track the value of the sum after each step. We can use the value of the sum as the condition that determines whether or not the while loop continues to iterate. See the code below.
Problems
The goal of these problems is to convert several of the flowchart solutions to questions in the Flowcharts With Operators to Python functions.
- Once you define a function, it can be used inside the definition of another.
- You can define a function inside of another. But beware of scope issues in this case.
Note that the second hint for this problem contains both solutions described above.