Login

Please fill in your details to login.





lesson 3.1.5 loops in python

Round and round again.


image

Imagine if you had to write "I will not talk in class" 100 times on a whiteboard. You’d get bored very quickly! Luckily, computers love doing boring, repetitive tasks. Today, you are going to learn the superpower of iteration. You will master the specific controls of the for loop to generate exact sequences of numbers, process text, and lists, before learning the while loop to build a game that can outsmart you! You are stepping into the shoes of a Software Developer or Automation Engineer where efficiency is everything.

Learning Outcomes
The Building Blocks (Factual Knowledge)
Define the terms "definite iteration" and "indefinite iteration" in the context of Python programming.
Recall the three parameters of the range() function: start, stop, and step.
State the syntax for iterating over strings and lists.

The Connections and Theories (Conceptual Knowledge)
Distinguish between the outputs of range(5), range(1, 5), and range(1, 10, 2).
Explain how a loop variable changes value during each iteration of a cycle.
Predict the output of a loop that processes a list of items.

The Skills and Methods (Procedural Outcomes)
Apply the range() function to generate precise sequences, including countdowns.
Construct for loops to iterate through strings and lists to process data.
Create a while loop to build a robust "Number Guessing Game".

Digital Skill Focus: C.1.1 Core Skills - Develop touch-typing skills to improve speed and accuracy of syntax entry within an IDE

Why repeat yourself?


In programming, we follow a rule called DRY: Don't Repeat Yourself. If you copy and paste the same line of code five times, you are wasting time. Instead, we use loopsI have no idea what this means (also called iterationI have no idea what this means). There are two types of iteration:

image


The
for
Loop


Use a
for
loop when you want to repeat a block of code a specific number of times. We often call this type of iteration definite because we know exactly how many times the loop will run. The main ways we can definitely iterate are:

Using the
range()
function to construct an iterable:
range(stop)
range(start,stop)
range(start,stop,step)
- step can be negative as well...
Using a string as the iterable.
Using a list as the iterable.

time limit
Task 1 PRIMM: The Range Rover and other iterables

As we've seen, there are a number of ways in which we can specify the iterable in a
for
loop. In this task, you are going to use the PRIMM methodology to learn the basics. You will use a read-only inline IDE and Thonny as well to give you some practice managing your workspace and coding in a bonefide IDE.

1
Get organised

Open up Thonny on your PC. If you haven't got Thonny available, ask your teacher to suggest a suitable IDE to use instead.
Organise your workspace!

2
The
range()
iterable

Arguably the most straightforward iterator is the
range()
function. It actually generates a list of values based on the parameters that you give it and then you use a
for
loop to iterate through them one at a time. The simplest option is to specify only the
stop
value instructing the loop to count from 0 up to (but not including) the number you give it. Let's get PRIMMing!

⭐ Easing you in:
range(stop)


🤔 Code Explainer
Line 1: Define the type of interaction (definite), declare the loop variable (
i
) and define the range of values assigned to it during the loop.
Line 2: Print a string "
This is loop number
" concatenated with a current value of the loop variable. The comma (
,
) automatically typecasts the integer in
i
to a string.

Predict the first number and the last number this code will print.
Run the script. Were you right?
Investigate the values printed on each line. What does stop value represent?

Here comes Thonny... Now copy the code and paste it into a new Thonny script. Make sure it's saved.

Modify the code so it prints the numbers 0 to 9. Change the message to say "Chapter " followed by each number.
Make a script containing a loop that prints the first 5 multiples of 3. Hint: calculate
i * 3
inside the print statement.

Challenge
Ask the user to input a number
n
, then loop
n
times printing "Python is cool" each time.

Additionally, we can specify the
stop
parameter as well so we can truely define the range of numbers that the loop should start counting at and end counting at. Let's get PRIMMing...

⭐⭐ Moving on:
range(start,stop)


🤔 Code Explainer
Line 1: Define the type of interaction (definite), declare the loop variable (
i
) and define the range of values assigned to it during the loop.
Line 2: Print a string "
i has the value: 
" concatenated with a current value of the loop variable. The comma (
,
) automatically typecasts the integer in
i
to a string.

Predict what will happen when you run the script. Will it print
9
?
Run the script - was your prediction correct?
Investigate the values printed. What stop value would you specify if you wanted the last number to be
24
?

Here comes Thonny... Now copy the code and paste it into a new Thonny script. Make sure it's saved.

Modify the loop to count from
10
to
20
.
Make a new script which prints the 6 times table but it MUST start with 1*6 = 6.

Challenge
Ask the user for a start number and a stop number, then run a loop between those two numbers.

The ultimate
range()
function has all three of it's parameters set -
start
,
stop
and
step
. Now it's getting interesting so let's get PRIMMing!

⭐⭐⭐ Ultimate control:
range(start,stop,step)


🤔 Code Explainer
Line 1: Define the type of interaction (definite), declare the loop variable (
i
) and define the range of values assigned to it during the loop.
Line 2: Print a string "
i has the value: 
" concatenated with a current value of the loop variable. The comma (
,
) automatically typecasts the integer in
i
to a string.

Predict what numbers will appear when you run the script.
4
,
6
,... what else?
Run the script - was your prediction correct?
Investigate the values printed. Can you see which number prints last and why?

Here comes Thonny... Now copy the code and paste it into a new Thonny script. Make sure it's saved.

Modify the step to
5
so the script counts up in 5s.
Make a new script which asks the user for the
start
,
stop
and
step
values and then prints out the correct numbers.

Challenge
Make a countdown timer by using a negative step value. Yes, that's possible! Try using this
range()
function:
range(10,0,-1)
in place of the one in your script and see what happens.

3
A string iterable

OK, so iterating through a list of numbers generated using the
range()
function is all well and good but there are some other cool things we can iterate through as well, including strings...

🤔 Code Explainer
Line 1: Declare the variable
word
and store the string "
Hello
" in it.
Line 2: Define the type of iteration (definite), declare the loop variable (
letter
) and assign it the values it will take during the loop, in this case the string contained in the variable
word
.
Line 3: Print out the current value of the loop variable,
letter
.

Predict how the word
Hello
will print when you run the script.
Run the script - was your prediction correct?
Investigate the output. Are you happy that you know what has happened?

Here comes Thonny... Now copy the code and paste it into a new Thonny script. Make sure it's saved.

Modify the script so that it prints a different word.
Make a new script which asks the user for their name using the
input()
function and then
print()
s out each letter in it.

Challenge
Can you make it print your name in UPPER CASE a letter at a time using the
.upper()
function? Hint:
print(letter.upper())

4
A list iterable

Lastly in the world of definite iteration we meet the list iterable. We'll learn more about lists in another module but for now, just recognise that they are a container for any values we want; integers, floats, characters, strings, Booleans or a combination thereof.

Firstly, a list of integers...

🤔 Code Explainer
Line 1: Define the type of loop, specify the loop variable (
item
) and the iterable.
Line 2: Print out the current value of the loop variable.

Predict what will happen when you run the script.
Run the script - was your prediction correct?
Investigate the values printed. Do they make sense?

Here comes Thonny... Now copy the code and paste it into a new Thonny script. Make sure it's saved.

Modify the script to add a list of the number of days in each month of the year (not a leap year).
Make a new script which prints out the double of each number in the list. Hint: You will have to include
item*2
inside the
print()
statement.

Challenge
Type the following code into a new Thonny script and run it...

for item in [77,97,103,105,99]:
    print(chr(item),end="")


Can you figure out what's going on here?

...and finally, a list of strings...

🤔 Code Explainer
Line 1: Define the type of loop, specify the loop variable (
name
) and the iterable.
Line 2: Print out the string
Hello
concatenated with the current value of the loop variable.

Predict what will happen when you run the script.
Run the script - was your prediction correct?
Investigate the values printed - do they make sense?

Here comes Thonny... Now copy the code and paste it into a new Thonny script. Make sure it's saved.

Modify the script by changing the list to one of your friends names.
Make a new script which contains a list of your favourite foods and prints out
I love [food]!
for each one.

Challenge
Write a script which takes this list...

words = ["Welcome","to","Python","for","beginners!"]


...and prints it out as a sentence like this

Welcome to Python for beginners!


Hint: You will have to use the the
end=" "
parameter in the
print()
statement.

Outcome: I have predicted, run and modified Python code in a
for
loop (definite iteration).

Checkpoint

The
while
loop


Sometimes we simply don't know how many times to loop! We want to keep going an indefinite number of times while a condition is
true
. Try this (very easy) guessing game out by pressing the ▶️ button.

🤔 Code Explainer
Line 1: Make sure the
answer
variable is empty.
Line 2: Define the loop as indefinite (
while
) and state the condition -
answer != "Paris"
. The loop will continue to run while this condition is
true
.
Line 3: Ask the user for their guess and store it in
answer
.
Line 4: Only if the condition is false will this line run and print
Correct!



time limit
Task 2 The Number Guessing Game

We will combine variablesI have no idea what this means, loopsI have no idea what this means (
while
), and selectionI have no idea what this means (
if
/
else
) to make a simple guessing game. Run the game by pressing ▶️ and try to win.

🤔 Code Explainer
Line 1: Import a special set of functions to create random numbers.
Line 2: Choose a random number between 1 and 20 and store it in the variable
secret
.
Line 3: Make sure
guess
is
0
.
Line 4: Print an introductory message.
Line 5: Define the loop as indefinite (
while
) and state the condition,
guess != secret
. The loop will continue to run if this condition stays
true
.
Line 6: Ask the user for their guess and convert it to an integer.
Line 7: If the guess is less than the secret number then run the next code block only.
Line 8: Print
Too low!

Line 9: Else if the guess is greater than the secret number, run the next code block only.
Line 10: Print
Too high!

Line 11: The guess must be correct - it's the only option left!
Line 12: Print
You got it!
. At this point, the loop will end because the condition is false.

Try these:
What is a MINIMUM number of guesses you would need to guess the number?
For the quiz that is written, what is the MAXIMUM number of guesses you would EVER need to guess the correct number?
Can you make the quiz harder by increasing the range of numbers that it choosen?

Outcome: I have combined iteration, selection, and variables to create a fully functioning interactive game.

Checkpoint

image
Today you have learnt how to control the flow of a program using loops. You can use for loops when you know the count, and while loops when you are waiting for a specific condition. You have applied this to create a game that responds intelligently to the user.

Out of Lesson Learning

Last modified: December 11th, 2025
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning