Python 6a - For Loops
Types of Loop
The third construct of programming (after Sequence and Selection) is Iteration. If you iterate something, then you repeat it.
​
There are two key loops to use in Python: for loops and while loops.
​
A for loop is count controlled – e.g. “For 10 seconds I will jump up and down”.
The loop will continue until the count (e.g. 10 seconds) has finished.
​
A while loop is condition controlled – e.g. “While I am not out of breath, I will jump up and down.”
The loop will continue as long as the condition remains true.
Simple For Loops (1 Range Value)
for i in range(5):
print("This is a loop!")
This is a loop!
This is a loop!
This is a loop!
This is a loop!
This is a loop!
for i in range(8):
print("Jaffa Cakes aren't biscuits")
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
Jaffa Cakes aren't biscuits
The i is a count variable, it is used to measure each iteration (turn) of the loop.
​
In the range brackets write the number of times to loop the indented code.
​
Don’t forget the colon at the end and remember that everything you want to repeat must be indented (press tab key once).
For Loops Task 1 (Repeat Your Name)
Example solutions (shortened):
Create a simple for loop that prints your name twenty times.
For Loops Task 2 (Are We There Yet?)
Christopher
Christopher
Christopher
...
Create a simple for loop that prints the sentence 'Are we there yet?' 150 times.
Are we there yet?
Are we there yet?
Are we there yet?
...
Counting Using i (2 Range Values)
For loops can be used to count by referring to the iteration inside the loop itself using i:
for i in range(5):
print("Loop number" , i)
Loop number 0
Loop number 1
Loop number 2
Loop number 3
Loop number 4
for i in range(1,6):
print("Loop number" , i)
Loop number 1
Loop number 2
Loop number 3
Loop number 4
Loop number 5
There are two important things to know about how Python counts using for loops.
​
-
Python will automatically start counting at 0 rather than 1.
-
The second value in the range is an exclusive limit - it will stop 1 before this value.
​
For example, if you wanted to count 1 to 10 you would need to write range(1,11).
For Loops Task 3 (100 to 150)
Create a for loop that prints all numbers from 100 to 150. You don't need to print any additional text, just the i variable.
Example solution (shortened):
100
101
102
...
...
148
149
150
Using a Step (3 Range Values)
A third value can be added to the range brackets of a for loop to define a step.
A step is the number to go up (or down) with each iteration.
for i in range(2,11,2):
print(i)
2
4
6
8
10
for i in range(18,0,-3):
print(i)
18
15
12
9
6
3
In most programs defining a step is not essential, Python will assume it is +1 if you don't include it.
For Loops Task 4 (Even Numbers 10 to 30)
Example solution for Task 4 (shortened):
Create a for loop that prints all even numbers from 10 to 30. Use a step.
For Loops Task 5 (Countdown)
Use a for loop with a negative step to print a countdown from 10 to 1.
10
12
14
...
...
26
28
30
Using Variables with For Loops
Variables can be used to make for loops suitable for a range of different purposes.
​loopnum = int(input("Times to repeat: "))
for i in range(loopnum):
print("Hello!")
Times to repeat: 4
Hello!
Hello!
Hello!
Hello!
The loop above uses a variable in the range brackets to repeat the loop the specific number of times that the user enters.
​loopnum = int(input("Times to repeat: "))
word = input("Word to repeat: ")
​
for i in range(loopnum):
print(word)
Times to repeat: 3
Word to repeat: velociraptor
velociraptor
velociraptor
velociraptor
The loop above uses two variables that are input by the user; one to define the range and another that is printed.
For Loops Task 6 (Many Happy Birthdays)
Example solution for Task 6 (shortened):
Ask the user to input their age then print 'Happy Birthday!' that many times.
For Loops Task 7 (House Number and Name)
Ask the user to enter their house number (e.g. 15 if they lived at 15 Cherry Road) and their name. Print their name as many times as their house number. For example, if Hannah lived at 103 Apple Lane then Hannah would be printed 103 times.
Enter your age: 5
Happy Birthday!
Happy Birthday!
Happy Birthday!
Happy Birthday!
Happy Birthday!