Python Programming 2 / Chapter 3: For Loops / For Loops

Introduction to For Loops

In the first Python course, we learned how to execute the same code many times using the while loop. In this chapter, we will study another type of loop called the for loop. The for loop is a convenient way to operate on elements of a collection. For example, let's say that we want to calculate the sum of the elements in the following list:

[ 1, 2, 3 ]

Our plan is to create a variable, say, total, which initially contains 0. Then, for each element in the list, we will add the element to total.

Here is our implementation using the for loop:

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Line 3 is the for statement, and line 4 is the for loop body.

Let's walk through the example program to see what it does.

  1. In line 1, Python assigns the list [ 1, 2, 3 ] to variable numbers.
  2. In line 2, Python assigns 0 to variable total.
  3. In line 3, Python copies the first element in numbers, 1, to value and enters the for loop body.
  4. In line 4, tab 1, Python adds value to total. total is now 1.
  5. The for loop body has ended. Return to the for statement in line 3.
  6. Python now copies the second element in numbers, 2, to value and enters the for loop body.
  7. In line 4, tab 1, Python adds value to total. total is now 3.
  8. Python returns to the for statement again and copies the third element in numbers, 3, to value.
  9. In line 4, tab 1, Python adds value to total. total is now 6.
  10. Python returns to the for statement again. Python copied all elements of the list, so Python exits the loop.

Whenever Python encounters the for statement, Python copies the next element in the numbers list to variable value. Initially, the next element is the first element in the list. When Python reaches the end of the list, Python exits the loop.

Here is the general format of the for statement:

for target in iterable:

  • target must be a variable such as x or name.
  • iterable must be an expression whose output is a collection of elements (such as a list). Here are some examples:
    • [ 1, 2, 3 ]
    • numbers

Example: Computing the length of a list

Python provides the len function to compute the length of a list, but we can also compute the length without using the len function. See if you can write the solution (hint: keep a counter as you loop through the list).

Quiz (1 point)

Given the following loop:

numbers = [1, 2, 3, 4]
sum = 0
for value in numbers:
    sum = sum + value 

How many times does the for loop body (line 4) execute?

Become a subscriber to save your progress, see the correct answer, and more!
Quiz (1 point)

Given the following loop:

numbers = [1, 2, 3, 4]
sum = 10
for value in numbers:
    sum = sum - value 

What is the value of sum at the end of the program?

Become a subscriber to save your progress, see the correct answer, and more!

Let's now go over the properties that collectively describe the for loop in more detail.

Previous Lesson Next Lesson

Comments

Please log in to add comments