title: "For Loops"
teaching: 10
exercises: 15
questions:
- "How can I make a program do many things?"
objectives:
- "Explain what for loops are normally used for."
- "Trace the execution of a simple (unnested) loop and correctly state the values of variables in each iteration."
- "Write for loops that use the Accumulator pattern to aggregate values."
keypoints:
- "A for loop executes commands once for each value in a collection."
- "A for
loop is made up of a collection, a loop variable, and a body."
- "The first line of the for
loop must end with a colon, and the body must be indented."
- "Indentation is always meaningful in Python."
- "Loop variables can be called anything (but it is strongly advised to have a meaningful name to the looping variable)."
- "The body of a loop can contain many statements."
- "Use range
to iterate over a sequence of numbers."
- "The Accumulator pattern turns many values into one."
pressure_001
, pressure_002
, etc.for number in [2, 3, 5]:
print(number)
{: .language-python}
for
loop is equivalent to:print(2)
print(3)
print(5)
{: .language-python}
for
loop's output is:2
3
5
{: .output}
for
loop is made up of a collection, a loop variable, and a body.for number in [2, 3, 5]:
print(number)
{: .language-python}
[2, 3, 5]
, is what the loop is being run on.print(number)
, specifies what to do for each value in the collection.number
, is what changes for each iteration of the loop.for
loop must end with a colon, and the body must be indented.{}
or begin
/end
to show nesting.for number in [2, 3, 5]:
print(number)
{: .language-python}
IndentationError: expected an indented block
{: .error}
firstName = "Jon"
lastName = "Smith"
{: .language-python}
File "<ipython-input-7-f65f2962bf9c>", line 2
lastName = "Smith"
^
IndentationError: unexpected indent
{: .error}
for kitten in [2, 3, 5]:
print(kitten)
{: .language-python}
primes = [2, 3, 5]
for p in primes:
squared = p ** 2
cubed = p ** 3
print(p, squared, cubed)
{: .language-python}
2 4 8
3 9 27
5 25 125
{: .output}
range
to iterate over a sequence of numbers.range
produces a sequence of numbers.range(N)
is the numbers 0..N-1print('a range is not a list: range(0, 3)')
for number in range(0, 3):
print(number)
{: .language-python}
a range is not a list: range(0, 3)
0
1
2
{: .output}
# Sum the first 10 integers.
total = 0
for number in range(10):
total = total + (number + 1)
print(total)
{: .language-python}
55
{: .output}
total = total + (number + 1)
as:number
.total
.total
, replacing the current value.number + 1
because range
produces 0..9, not 1..10.Classifying Errors
Is an indentation error a syntax error or a runtime error?
Solution
An IndentationError is a syntax error. Programs with syntax errors cannot be started. A program with a runtime error will start but an error will be thrown under certain conditions. {: .solution} {: .challenge}
Tracing Execution
Create a table showing the numbers of the lines that are executed when this program runs, and the values of the variables after each line is executed.
~~~ total = 0 for char in "tin": total = total + 1 ~~~ {: .language-python}
Solution
| Line no | Variables | |---------|----------------------| | 1 | total = 0 | | 2 | total = 0 char = 't' | | 3 | total = 1 char = 't' | | 2 | total = 1 char = 'i' | | 3 | total = 2 char = 'i' | | 2 | total = 2 char = 'n' | | 3 | total = 3 char = 'n' | {: .solution} {: .challenge}
Reversing a String
Fill in the blanks in the program below so that it prints "nit" (the reverse of the original character string "tin").
~~~ original = "tin" result = _ for char in original: result = _ print(result) ~~~ {: .language-python}
Solution
~~~ original = "tin" result = "" for char in original: result = char + result print(result) ~~~ {: .language-python} {: .solution} {: .challenge}
Practice Accumulating
Fill in the blanks in each of the programs below to produce the indicated result.
~~~
Total length of the strings in the list: ["red", "green", "blue"] => 12
total = 0 for word in ["red", "green", "blue"]: _ = _ + len(word) print(total) ~~~ {: .language-python}
Solution
~~~ total = 0 for word in ["red", "green", "blue"]: total = total + len(word) print(total) ~~~ {: .language-python} {: .solution}
~~~
List of word lengths: ["red", "green", "blue"] => [3, 5, 4]
lengths = _ for word in ["red", "green", "blue"]: lengths._(____) print(lengths) ~~~ {: .language-python}
Solution
~~~ lengths = [] for word in ["red", "green", "blue"]: lengths.append(len(word)) print(lengths) ~~~ {: .language-python} {: .solution}
~~~
Concatenate all words: ["red", "green", "blue"] => "redgreenblue"
words = ["red", "green", "blue"] result = _ for in _: print(result) ~~~ {: .language-python}
Solution
~~~ words = ["red", "green", "blue"] result = "" for word in words: result = result + word print(result) ~~~ {: .language-python} {: .solution}
~~~
Create acronym: ["red", "green", "blue"] => "RGB"
write the whole thing
~~~ {: .language-python}
Solution
~~~ acronym = "" for word in ["red", "green", "blue"]: acronym = acronym + word[0].upper() print(acronym) ~~~ {: .language-python} {: .solution}
ZNK: this is a test of text after an end block
ZNK: test two {: .challenge}
Cumulative Sum
Reorder and properly indent the lines of code below so that they print a list with the cumulative sum of data. The result should be
[1, 3, 5, 10]
.~~~ cumulative.append(sum) for number in data: cumulative = [] sum += number sum = 0 print(cumulative) data = [1,2,2,5] ~~~ {: .language-python}
Solution
~~~ sum = 0 data = [1,2,2,5] cumulative = [] for number in data: sum += number cumulative.append(sum) print(cumulative) ~~~ {: .language-python} {: .solution} {: .challenge}
Identifying Variable Name Errors
- Read the code below and try to identify what the errors are without running it.
- Run the code and read the error message. What type of
NameError
do you think this is? Is it a string with no quotes, a misspelled variable, or a variable that should have been defined but was not?- Fix the error.
- Repeat steps 2 and 3, until you have fixed all the errors.
~~~ for number in range(10): # use a if the number is a multiple of 3, otherwise use b if (Number % 3) == 0: message = message + a else: message = message + "b" print(message) ~~~ {: .language-python}
Solution
The variable
message
needs to be initialized and Python variable names are case sensitive:number
andNumber
refer to different variables. ~~~ message = "" for number in range(10): # use a if the number is a multiple of 3, otherwise use b if (number % 3) == 0: message = message + "a" else: message = message + "b" print(message) ~~~ {: .language-python} {: .solution} {: .challenge}Identifying Item Errors
- Read the code below and try to identify what the errors are without running it.
- Run the code, and read the error message. What type of error is it?
- Fix the error.
~~~ seasons = ['Spring', 'Summer', 'Fall', 'Winter'] print('My favorite season is ', seasons[4]) ~~~ {: .language-python}
Solution
This list has 4 elements and the index to access the last element in the list is
3
. ~~~ seasons = ['Spring', 'Summer', 'Fall', 'Winter'] print('My favorite season is ', seasons[3]) ~~~ {: .language-python} {: .solution} {: .challenge}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.