TOPIC 1: COMPUTATIONAL THINKING
For loops and the range function
ACTIVITIES FOR Y10-03-CT14
Name ____________________________________________ Date __________________
Activity 1
Complete the table to show the output of the range() instruction with different parameters.
Code Output
range(5)
range(0, 5)
1, 2, 3, 4, 5, 6, 7, 8
range(0, 7)
0, 2, 4, 6, 8
range(11, 88, 11)
range(5, 0, -1)
range(50, 0, -10)
range(10, 0, -10)
range(10, 0)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
1
TOPIC 1: COMPUTATIONAL THINKING
For loops and the range function
Activity 2
Complete the table to show the code and output for combinations of a ‘for’ loop and ‘range’.
You can check them in your development environment.
Code Output
for num in range (3):
print (num)
for num in range (5):
print (num * num)
for num in range (11, 88, 11):
print (num / 11)
Hint: What data type does division return?
for num in range (50, 0, -10):
print (num * 10)
for num in range(0, 0, -10):
print(num)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
2
TOPIC 1: COMPUTATIONAL THINKING
For loops and the range function
Activity 3
Here is a list of fruit. A program is required that will display the first three letters of each fruit
name. When finished, the program should display ‘Fruit Salad!!’
fruits = ["apple", "banana", "grape", "lemon", "melon", "orange"]
These are the flowchart symbols for the algorithm to implement this program.
Put the symbols in the correct order. Use each symbol only once. Use as many arrows and
Yes/No labels as you required.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
3
TOPIC 1: COMPUTATIONAL THINKING
For loops and the range function
Activity 4
Here is the code for a program that finds and displays the average size in a list of sizes.
sizeList = [1, 36, 11, 38, 35, 40]
total = 0
for size in sizeList:
total = total + size
averageSize = total / len (sizeList)
print (averageSize)
Draw the corresponding flowchart.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
4
TOPIC 1: COMPUTATIONAL THINKING
For loops and the range function
Activity 5
Here is an existing program.
colours = ["red", "blue", "green", "yellow", "blue", "black",
"orange"]
for item in colours:
print(item)
1 Predict the output of this program.
2 Copy and paste or load the code into your development environment. Run the code.
Did it behave as you predicted? Why or why not?
……………………………………………………………………………………………….................
……………………………………………………………………………………………….................
……………………………………………………………………………………………….................
3 Add another ‘for’ loop to print the colours, but use range() to generate an index for each
item.
……………………………………………………………………………………………….................
……………………………………………………………………………………………….................
……………………………………………………………………………………………….................
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
5
TOPIC 1: COMPUTATIONAL THINKING
For loops and the range function
4 Add another ‘for’ loop to print the colours in reverse order, using range() to generate an
index for each item.
……………………………………………………………………………………………….................
……………………………………………………………………………………………….................
……………………………………………………………………………………………….................
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
6