Loading...
Hello, my name is Mrs. Holborow and welcome to Computing.
I'm so pleased you've decided to join me for the lesson today.
We're going to be exploring how we can use count-controlled iteration to iterate through a list and inspect each item.
Welcome to today's lesson from the unit Python Programming with Sequences of Data.
This lesson is called Using For Loops to Iterate Data Structures, and by the end of today's lesson you'll be able to use a for loop to inspect every element of a list.
It would be useful if you had access to a device that you can create and amend code on for this lesson.
Shall we make a start? We will be exploring these keywords throughout today's lesson.
Iteration Iteration, the process of repeating a sequence of instructions within a program loop.
For loop.
For loop, a count-controlled loop used to repeat a specific block of code a known number of times.
Today's lesson is split into two sections.
We'll start by explaining the differences between while and for loops and then we'll move on to use a for loop to inspect each item in a list.
Let's make a start by explaining the differences between while and for loops.
There are two main types of iteration used in programming.
In condition-controlled iteration, instructions are repeatedly executed until a condition is met.
In count-controlled iteration, instructions are repeatedly executed a set number of times.
Let's think about the example if a coach asks you to do press-ups.
What instructions could they give you that would be an example of count-controlled iteration? What instructions could they give you that would be an example of condition-controlled iteration? Maybe pause your video whilst you have a think.
Ah, Sam and Jun have got some excellent examples.
Sam says, an example for count-controlled iteration could be do 20 press-ups, doing a set number of times.
Jun says an example for condition-controlled iteration could be do press-ups until the alarm goes off.
So, doing something until a condition is met.
For loops are count-controlled, meaning that you specify the number of times that you wish the instructions to be repeated.
In Python, the range function can be used to specify the number of times that a for loop should iterate.
So, here's an example of code.
On line 1, I have the statement for count in range and then I open my brackets and I have the value 1, 6 and then I close my brackets, I have a colon and then on line 2, I have the indent block of code which is going to run, which is going to print the variable held in count.
Here, the number 1 represents the starting value and the number 6 represents the stopping value.
If you only provide one argument, which will be for the stopping value the starting value will be 0 by default.
So, you can see here the line 1 code has been altered and this time it says for count in range, 6 in brackets.
So, this will automatically start at 0 and the stopping value will be 6.
Sam says, when I run the code, it does not display the value 6, it stops at 5.
Let's have a look at the output from Sam's program.
Can you explain why this is happening? The stopping value of the range function is exclusive.
This means that the final value of the count will be up to but will not include the stopping value.
So, 6 will not be printed.
In Python, while loops are used to implement condition-controlled iteration and for loops are used to implement count-controlled iteration.
Here we have two examples of code which are doing the same thing but in a slightly different way.
The left-hand side uses condition-controlled iteration.
So, on line 4 I have a while loop which says while new is not equal to no and then it's going to run the block of code within the while loop.
So, asking the user to pick an instrument storing that instrument as input, appending that instrument to the list called band and then printing pick another and asking them to input it again.
On the right-hand side, the code uses count-controlled iteration.
So, on line 3 we have a for loop which says for x in range 4.
It then has the block of code so we have print pick an instrument, we have the input assigned to the variable instrument and then we're adding the instrument to the list band.
The while loop will continue while the user does not answer no.
Whereas the for loop will run for 4 times.
X starts from 0 and increments by 1 as default, and then stops before a specified number which in this case is 4.
This is the example output from the two programs. So, on the left-hand side we have the condition-controlled loop.
You can see that the user has asked to pick an instrument and they respond with piano.
Pick another, yes, they say drums pick another, they say no and then the loop ends and the list is printed.
So, the list contains piano followed by drums. On the right-hand side, we have the count-controlled example.
So, the user picks an instrument, drums, picks an instrument again, piano picks an instrument again, guitar, picks an instrument again, saxophone and then the loop ends.
The list contains 4 items, drums, piano, guitar and saxophone.
Time to check your understanding.
What is this code an example of? Is it A, condition-controlled iteration or B, count controlled iteration? Pause the video whilst you have a think.
Did you select B, count controlled iteration? Well done.
Remember a for loop is used to implement count-controlled iteration in Python.
What do you think will be displayed when this program is run? Maybe pause the video whilst you have a look at the code and have a think.
Jun says, it will display each item in the list.
So, 1, 4, 3 and 6.
That's correct Jun, well done.
What do you think will be displayed when this program is run? Again, maybe pause the video whilst you have a look at the code.
Sam says, the count variable is initialised to 0 and is incremented by 1 for each item in the list.
So, it will display 4.
Well done Sam.
Time to check your understanding.
What will be displayed when this program is run? A, 4, B, 3 followed by 6 or C, 2? Pause the video whilst you have a think.
Did you select C? Well done.
2 is the correct answer.
Let's have a look at why that's the case.
The count variable is initialised to 0 and is incremented by 1 each time the item in the list is greater than 3.
So, if we have a look at each item in the list the first item 1 is not greater than 3 so count will not be incremented.
4 is greater than 3, so it will be incremented.
3 is not greater than 3, so it won't be incremented.
And 6 is, so it will be incremented.
So, that's 2 times.
Okay, you're doing a fantastic job in today's lesson so far so well done for all your hard work.
We're moving on to the first set of tasks of today's lesson.
So, for task A part 1, I'd like you to explain the difference between condition controlled and count controlled iteration.
For part 2, a program contains a list of colors and you want it to display each colour held in the list.
Explain what type of loop you would use and why.
Pause the video whilst you answer the questions.
How did you get on? You're doing a great job so well done.
Let's share some sample answers together.
So, for part 1, you were asked to explain the difference between condition controlled and count controlled iteration.
Condition controlled iteration will run until a condition is met.
For example, until the flag is not true.
Count controlled iteration will run a set number of times.
For part 2, you were given a scenario.
A program contains a list of colors and you want it to display each colour held in the list.
Explain what type of loop you would use and why.
I would use a for loop.
This is because the for loop could be set to run the same number of times as the length of the number of items in the list.
The program would iterate through each item in the list and display the colour from the list each time the loop is executed.
Okay, we're moving on to the second part of the lesson now.
We're going to use a for loop to inspect each item in a list.
A for loop can be used to iterate through a list and inspect each item.
This list holds the name of capital cities.
The for loop iterates through the list and increments the variable count each time a city in the list starts with the character L.
So, you can see on line 8 we have a for loop which says 4 city in cities and then we have an indented if statement which says if city 0, so the first character in the city, is equal to L then we're going to increment the count variable by 1.
Time to check your understanding.
What code would you add to line 9 to check if each city in the list starts with a B? Is it A, B, or C? Pause the video whilst you have a think.
That's right! B is correct.
City position 0 is double equal to B.
Notice that this is case sensitive so we need to make sure if we've used capital letters in our list then we use capital letters in our search too.
The cities are stored in title case so the if statement needs to check for a capital B.
Each individual element can also be inspected using string methods.
Here the example code uses a for loop that iterates through the list and increments the variable count each time a city in the list has more than 6 characters.
So, you can see we're using the len function here.
Time to check your understanding.
What would be the value of count after this program is executed? Is it A, 2, B, 4, or C, 0? Pause the video whilst you have a think.
That's right! The correct answer is A, 2.
This is because the if statement is checking that the length of the city is greater than 6 and the city position 0 so the first letter in the city is equal to L.
There are only 2 cities that start with L and have more than 6 characters.
So far, you have iterated over lists that contain strings but the process is exactly the same for a list that contains numbers.
So, here I've got a list called ages which contains the numbers 12, 11, 13 15, 12, 14, 13 and 11.
The for loop iterates through the list, and increments the variable count each time an age is greater than or equal to 12.
What would the value of count be after the program is run? Maybe pause the video here and have a think.
That's right! Count would be equal to 6.
There are 6 ages in the list that are either equal to or greater than 12.
Okay, we're moving on to our final set of tasks for today's lesson and you're doing a fantastic job so far so well done.
A weather station in Heathrow has been monitoring rainfall in millimetres since 1948.
Open the starter file oak.
link/rainfall.
The file loads rainfall data from a text file and prints all the entries.
Remove the print statement on line 5 then amend the code to add a count variable and increment the count variable each time the rainfall is greater than 65 mm.
For part 3 display the number of times the rainfall is greater than 65 mm.
Don't worry too much about how the data is read in from the text file.
You don't need to know how that code works for this activity.
How did you get on? Great work! If you want to see a full working solution you can go to oak.
link/rainfall-solution.
You can see here on line 4 I have a for loop which says for value in rainfall data, and then on line 5 I have an if statement which says, if value is greater than 65, I'm going to increment the count variable by 1.
On line 8, I have a print statement which prints out the number of times the rainfall exceeds 65 mm.
Remember if you need to make any corrections you can always pause your video and go back a few slides.
For part 4, I'd like you to open the starter file oak.
link/rainfall-insights.
The starter file contains two variables.
One called max rainfall which is currently set to 0 and one called min rainfall which is currently set to 100.
For part A, add a for loop to iterate through the list of rainfall measurements.
For part B, if the rainfall is higher than the current amount held by max rainfall then the max rainfall measurement should be replaced with the current list item.
For part C, if the rainfall is lower than the current amount held by min rainfall then the min rainfall measurement should be replaced with the current list item.
And then, finally, for part D, at the end of the program display the max rainfall and min rainfall amounts to the user.
Pause the video whilst you complete the activity.
How did you get on? Great work.
So again, you can see a full working solution at oak.
link/rainfall-insights-solution.
So, on line 5, I have my for loop which says for value in rainfall data.
On line 6, I have an if statement which says if value is greater than max rainfall then the max rainfall is going to become equal to the value.
On line 8, I have an elif statement which says if the value is less than min rainfall then min rainfall is going to become equal to the value.
On lines 11 and 12, I'm printing out the values of max and min rainfall.
Again, if you need to make any corrections to your code you can pause the video and do that now.
We've come to the end of today's lesson and you've done a fantastic job so well done.
Let's summarise what we've learnt together.
Count controlled iteration is implemented using for loops in Python.
The range function can be used with a for loop to specify the number of times that a for loop should iterate.
For loops can be used to iterate through each item held in a list to inspect each item in turn.
I hope you've enjoyed today's lesson and I hope to see you again soon.
Bye!.