CT03b
Explain that the 'range' function generates a sequence of numbers
Use the iteration 'for' to process every item in a one-dimensional data structure
CT03b
How might you go about printing all the numbers from 0 to 100?
You could use a while loop and a counter.
You could simply print them all one at a time in a sequence.
But there is a more efficient way to do it.
count = 1
while count <= 100:
print(count)
count = count + 1
CT03b
You can use the Python function range().
Range is most commonly used in for loops which you will look at shortly.
Range is a built-in Python function that generates a sequence of integers.
First you need to understand how to use range and what it returns.
CT03b
If you create a variable and assign a range to it, it looks like this:
To be able to see deeper inside, you need to convert the result to a list.
However, if you print ‘nums’, you get:
nums = range(10)
range(0, 10)
nums = list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Note: it starts at 0 and counts ten digits
CT03b
When using range there are three values that you can enter into the brackets.
Some of these are optional, which means you can leave them out. Python will then use default values.
These are called parameters.
range(start, stop, step)
start - The starting point or first digit (default: 0).
stop - The number of numbers to generate (final value + 1).
step - The number to add each time, e.g. 2 would be every other number (default: 1).
Complete the table to show the output of the range() instruction with different parameters.
Code | Output |
---|---|
range(5) | |
range(0, 5) | |
range(1, 9) | |
range(0, 7) | |
range(0, 10, 2) | |
range(11, 88, 11) | |
range(5, 0, -1) | |
range(50, 0, -10) | |
range(0, 10, -10) | |
range(10, 0) |
0, 1, 2, 3, 4
0, 1, 2, 3, 4
1, 2, 3, 4, 5, 6, 7, 8
0, 1, 2, 3, 4, 5, 6
0, 2, 4, 6, 8
11, 22, 33, 44, 55, 66, 77
5, 4, 3, 2, 1
50, 40, 30, 20, 10
nothing
nothing
CT03b
The most common use of range is in a for loop.
You saw that such a condition-controlled loop can be used to repeat blocks of code while a condition is true.
You learned previously about generating repetition using while loops.
You can use for and range together to produce a different type of loop that runs a precise number of times – a for loop.
This kind of loop is known as a count-controlled loop.
CT03b
You need a program that can count from 1 to 100 and output that value.
Using a for loop it would look like this:
for num in range(1, 101):
print(num)
Start at 1 and go up to 100. Remember that the stop is the number after the one you really want.
You may see the letters ‘i’, ‘j’, or ‘k’ used in a for loop. Experienced programmers tend to use one-letter identifiers for variables in situations where the variable will not be used again.
However, you should use an identifier that has meaning in the context of your code, as this will make your code more readable and your logic clear.
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.
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)
for num in range (50, 0, -10):
print (num * 10)
for num in range(0, 0, -10):
print(num)
0
1
4
9
16
1.0
2.0
3.0
4.0
5.0
6.0
7.0
500
400
300
200
100
nothing
0
1
2
CT03b
You can use a for loop to iterate over a list.
This means that you can look at or perform an operation on each item in that list one at a time.
This is incredibly useful and is a very commonly used technique in all programming languages.
Let’s see how it is done.
CT03b
You have a list of items. You want to print every item in the list, in sequence.
You can iterate over the list and print each item in turn like so:
Notice that ‘item’ has been used instead of ‘i’ for the iterator variable.
What will happen when this program is run?
colours = ["red", "blue", "green",
"yellow", "blue", "black", "orange"]
for item in colours:
print(item)
CT03b
Is this what you expected?
red
blue
green
yellow
blue
black
orange
CT03b
You saw before that there is no ‘while loop’ symbol in a flowchart.
What did you have to use instead?
You used a decision and a path that looped back.
There’s no ‘for loop’ symbol in flowcharts, either.
What do you think is used instead?
Let’s look at an example that shows the algorithm for printing colours stored in a list.
CT03b
This is the flowchart that performs the same action as the Python code you’ve just seen.
You can see the arrow looping back to the decision. This represents the iteration.
Once the end of the list is reached, the program ends.
Any number of instructions can be performed inside the loop.
The number of loops is exactly the same as the length of the input list.
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.
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.
Here is an existing program.
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
Here is an existing program.
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
red
blue
green
yellow
blue
black
orange
Here is an existing program.
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
Here is an existing program.
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
for index in range (0, len(colours)): #alternative non-pythonic method
print (colours[index])
Here is an existing program.
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
colours = ["red", "blue", "green", "yellow", "blue", "black", "orange"]
for item in colours:
print(item)
for index in range (len(colours) - 1, -1, -1):
print (colours[index])
CT03b