Check your understanding of the previous reading: the parts of a for loop, the iteration variable, and how a loop runs.
Identify each of the components of the for loop:
![Two lines of code under `grades = [..., 50, 80]`: `for item in grades:` and an indented `print(item)`, with six numbered labels: 1 points at `for`, 2 at `item`, 3 at `in`, 4 at the indented line, 5 at the end of `grades:`, and 6 at `item` inside `print(item)`.](media/imgur-zQDwPr7.png)
| Label | Points at | Component |
|---|---|---|
| 1 | for | |
| 2 | item | |
| 3 | in | |
| 4 | print(item) | |
| 5 | grades: | |
| 6 | item inside print(item) |
Read the header for item in grades: from left to right: a keyword, the new variable, a keyword, then the list. Label 4 marks the indented line under it. One component gets two labels: label 6 points at a name inside the body that also appears in the header.
1 For keyword, 2 Iteration Variable, 3 In keyword, 4 Body, 5 List Variable, 6 Iteration Variable (the same item, used inside the body).
Type the character used at the end of the first line of a for loop.
The first line of a for loop ends with a colon, as in for item in grades:. The answer is :.
What is the type of the Iteration Variable in this code?
groceries = ["Apples", "Cookies", "Steaks"]
for grocery in groceries:
print(grocery)
Select one answer, then check.
groceries is the list; the iteration variable grocery holds one element of it at a time.groceries is a string, and the iteration variable takes on one element at a time, so grocery is a string."Apples".Mark each of the following statements about for loops that are true:
Select every answer that applies, then check.
for loop repeats its body once for each element of a list.for line for the next element.Trace the values of this code. If a variable is not yet defined, put an "X" (without quotes).
grades = [10, 20]
total = 0
for grade in grades:
adjusted = grade + 10
total = total + adjusted
Each row is one step: the line that just ran, and the value of every variable after it.
| step | line | grades | grade | adjusted | total |
|---|---|---|---|---|---|
| 1 | 1 | [10, 20] | X | X | X |
| 2 | 2 | [10, 20] | X | ||
| 3 | 3 | [10, 20] | X | ||
| 4 | 4 | [10, 20] | 20 | ||
| 5 | [10, 20] | ||||
| 6 | |||||
| 7 | [10, 20] | ||||
| 8 | [10, 20] | 50 |
Steps 1 and 2 run lines 1 and 2: grades becomes [10, 20] and total becomes 0; grade and adjusted don't exist yet, so they stay X.
Step 3 is line 3, the first pass of the loop: grade becomes the first element, 10.
Step 4 is line 4: adjusted is 10 + 10 = 20. Step 5 is line 5: total is 0 + 20 = 20.
Step 6 goes back up to line 3 for the second pass: grade becomes 20, while adjusted and total keep their values.
Step 7 is line 4: adjusted is 20 + 10 = 30. Step 8 is line 5: total is 20 + 30 = 50. The list has no more elements, so the loop ends.
After line 5, execution goes back up to line 3, which gives grade the next element of the list. A variable keeps its value until a line assigns it again.