Programming with Python · free preview
Repeating Actions with Loops
Explain what a `for` loop does. Correctly write `for` loops to repeat simple calculations. Trace changes to a loop variable as the loop runs. Trace changes to other variables as they are updated by a `for` loop.
1. Overview
Overview
2. What's in a name?
What's in a name?
3. From 1 to N
From 1 to N
4. Understanding the loops
Understanding the loops
5. Computing Powers With Loops
Computing Powers With Loops
6. Summing a list
Summing a list
7. Computing the Value of a Polynomial
Computing the Value of a Polynomial
8. Choosing while or for
Choosing while or for
9. Complete the accumulator
Complete the loop body so total accumulates the values: total = 0; for value in [2, 4, 6]: total = ____. Write the right-hand expression.
10. Tracing an accumulator loop over readings
Trace this complete Python program. Given readings = [3, 7, 4], state the value of total and value after each loop pass, then predict every printed line.
Example. Tracing an accumulator loop over readings
Problem. Trace this complete Python program. Given readings = [3, 7, 4], state the value of total and value after each loop pass, then predict every printed line.
Correct solution.
readings = [3, 7, 4]
total = 0
for value in readings:
total = total + value
print('pass:', value, 'running total:', total)
print('final total:', total)
print('count:', len(readings))
print('mean:', total / len(readings))Expected input, the list [3, 7, 4].
Expected output.``output pass: 3 running total: 3 pass: 7 running total: 10 pass: 4 running total: 14 final total: 14 count: 3 mean: 4.666666666666667 ``
Trace/reasoning,total starts at 0 because nothing has been added. The loop runs three times because the list has three elements. In pass one, value = 3 and 3 + 0 gives total = 3. In pass two, value = 7 and 3 + 7 gives total = 10. In pass three, value = 4 and 10 + 4 gives total = 14. After the last pass there is no next element, so the loop stops; value still exists and equals 4, while total keeps 14. The later len(readings) is 3, and 14 / 3 prints 4.666666666666667.
关键提醒。
This worked example separates the loop variable, the accumulated variable, and the values printed inside or after the loop. It also shows that the loop variable persists after the loop and that len supplies the element count independently of the manual accumulator.
11. Retrieval practice
Without looking back, write a complete Python loop that computes the product of the elements in numbers = [2, 4, 6] using repeated multiplication, and give the value of the product variable after each pass.
Write the loop in code. Then list the product after pass 1, pass 2, and pass 3, and state the final printed value.
12. Mastery check
Apply the same accumulator pattern to a new input, write a complete Python loop that computes total for numbers = [8, 3, 3, 1], prints total, and prints len(numbers).
Give the runnable loop. State the value of total after each of the four passes and both final printed lines.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free