6.12. Group Work - Loops (For, Range, While)

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.

Note

If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.

A loop allows you to execute the same statements multiple times. Python has two kinds of loop structures: for loops, which iterate over the items of a sequence, and while loops, which continue to execute as long as a condition is true.

Content Learning Objectives

After completing this activity, students should be able to:

Process Skill Goals:

During the activity, students should make progress toward:

6.12.1. for Statements

A for loop executes the same block of code “for each item in a sequence.”

Run this code to see what it prints.

In general, the length of the list determines the number of times that the loop repeats. The value of the variable x is selected from the list. Each time the loop runs, the next value from the list is assigned to x.

Before your for statement, you can assign your list to a variable and your program will run the same way:

Run this code to see what it prints.

In addition, for loops can be used with strings:

Run this code to see what it prints.

With strings, a for statement iterates over each character in the string. The length of the string determines how many times the body of the loop will run.

for loops can also handle many other data types, like tuples and dictionaries. Experiment on your own to see this in action!

6.12.2. The range Function

The Python range function will generate a list of numbers. The range function can take up to three numbers as arguments.

Run this code to see what it prints.

The first line of output describes the range as a function, whereas the second line shows the actual range of values as a list by using the list function.

If the argument of the range function specifies a single number, like range(x), the first number listed will be 0, the last number listed will be x - 1, and there will be x numbers in the list.

If the argument of the range function specifies two numbers, like range(x, y), the first number listed will be x, the last number listed will be y - 1, and there will be y - x numbers in the list.

If the argument of the range function specifies three numbers, like range(x, y, z), the first number listed will still be x, just like the two parameter version. The third argument represents how much to increment the number by each time. To calculate how many numbers will be in the list, take the result of (y - x) / z and round it up to the nearest whole number.

The arguments to range must be integers, so range does not work with strings. However, if you wanted to print the letters A to Z in a loop, you could do something like this:

You can use the built-in function chr to convert integers to their corresponding Unicode characters.

6.12.3. while Statements

A more general looping structure is the while statement.

Run this code to observe the behavior of a basic while loop and answer the questions below.

In the above code, the variable i is incremented by 1 each time the loop body is executed. Because the value of i steadily grows, the “loop condition” (the Boolean expression after the while) eventually becomes false when i = 3, which causes the loop body to stop executing.

A while loop has three parts that control the number of times it executes. The first part initializes the variable or condition, the second part tests whether the end has been reached, and the third part updates the variable or condition.

When writing a while loop, it’s helpful to answer a few questions before you start:

What needs to be initialized before the loop?

What condition must be true for the loop to repeat?

What will change so that the loop eventually ends?

For example, consider the code below. The add(n) function prompts the user for n numbers and returns the sum of these values. For example, when add(5) is called, the user is asked to input five numbers. If the user inputs 3, 1, 5, 2, and 4, the function would return the value 15.

Observe the behavior of this code to see how it answers the the three bullet points above.

Before the loop begins, the i variable, which counts how many times the loop runs, must be initialized. However, the total variable must also be initialized outside of the while loop, or else it would reset to 0 each time the loop ran.

The loop repeats n times, so the Boolean expression that must be true for the loop to continue is i < n.

Finally, for the loop to eventually end, i must be incremented, so we include the statement i = i + 1.

Making sure you answer these questions helps you write better (and less buggy) while loops.

If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.

The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.

You have attempted of activities on this page