3.12. For Loops#
for loops can be used to repeat code.
The structure of a for loop is:
for variable in iterable:
# code you want to execute
Note
An iterable is some sequence of values you can go through. In general, our iterables will be lists, but they can also be strings!
Here is an example:
for number in [1, 5, 8, 2]:
print(number)
1
5
8
2
What’s happening here?
We can see our iterable is the list [1, 5, 8, 2], and number is our
variable name.
At the first iteration, number will store the first value in our
list i.e. number = 1. Then we will execute the code on line 2 and print
1.
At the second iteration, number will store the second value in our list
i.e. number = 5. Then we will execute the code on line 2 and print 5.
At the third iteration, number will store the third value in our list i.e.
number = 8. Then we will execute the code on line 2 and print 8.
At the fourth iteration, number will store the fourth value in our list
i.e. number = 2. Then we will execute the code on line 2 and print 2.
After the number 2 there are no more numbers left in the list so the loop terminates.
This is how we can represent this code diagrammatically.
Here is another example, this time with a larger code block:
for number in [1, 5, 8, 2]:
print(number * 2)
print('I have doubled that number for you!')
print('All done here!')
2
I have doubled that number for you!
10
I have doubled that number for you!
16
I have doubled that number for you!
4
I have doubled that number for you!
All done here!
Take note of the following:
foris a keywordinis a keyword:is placed at the end of theforloop definitionThe code you want repeated must be indented
Question 1
What do you think the output of the following code will be?
for value in [3, 1, 4, -2, 3]:
print(value + 1)
Question 2
What do you think the output of the following code will be?
for letter in ['A', 'B', 'C']:
print(letter)
print("Now I know my ABC's")
Solution
Solution is locked
Question 3
What is wrong with the following program?
For i in [1, 2, 3]:
print(i)
This will result in a NameError because
iis not defined.This will result in a SyntaxError because
forshould be spelt with a lower case ‘f’.This will result in an IndentationError because
print(i)should not be indented.This will result in an infinite loop because
iis not incremented at the end of the loop.
Solution
Solution is locked
Question 4
Write a program that sums the numbers 1 to 10 and prints the result.
Solution
Solution is locked
Question 5
Code challenge: Cubes
You have been provided with a list of numbers.
numbers = [4, 7, 9, 12, 30, 12, 86, 341, 19, 3, 8, 6529, 7, 0, 345]
Write a program that loops through the list and for each number \(n\), prints out \(n^3\) on a new line.
The output of your program should look like this:
64
343
729
...
343
0
41063625
Hint
The ** operator can be interpreted as ‘to the power of’. For example, 2**3 is \(2^3\).
Solution
Solution is locked
Code challenge: Multiply All
You have been provided with a list of numbers.
numbers = [4, 17, 9, 5, 7, 12, 3, 14, 3, 23, 45, 8, 103, 21, 6, 9, 3, 3, 13]
Write a program that loops through numbers and multiplies all the numbers together and prints out the final result.
Example: This example is with a smaller list of numbers.
numbers = [2, 3, 6]
The above list would produce the output:
36
Hint
It might be helpful to create the variable result = 1, and then multiply result by each number.
Solution
Solution is locked

