Loading...
Hello, my name is Mrs. Holborow and welcome to Computing.
I'm so pleased that you've been able to join me for the lesson today.
In today's lesson, we are going to look at how we can apply a range of mathematical operations to items held in a list.
Welcome to today's lesson from the unit, Python programming with sequences of data.
This lesson is called Mathematical Operations in Data Structures, and by the end of today's lesson, you will be able to perform a range of mathematical operations on list items and return a result.
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.
Operator.
Operator: a symbol or word that instructs the computer to perform a specific calculation or action.
List.
List: a collection of data in a particular sequence.
Today's lesson is split into two parts.
We'll start by performing mathematical operations on list items and then we'll move on to create a game using a list.
Let's make a start by performing mathematical operations on list items. An operator is a symbol or special word that instructs the computer to perform a specific calculation or action.
So here we have a table that contains some common operators.
So we have the plus symbol that is used for addition, the subtraction symbol, the asterisk, which is used for multiplication in computing and the forward slash which is used for division.
This list holds a series of numbers, so the list has been initialized and called "numbers" and it holds the numbers 2, 4, 8, and 16.
Jun has a question, How could I add up each item in the list?" Maybe pause the video whilst you have a quick think.
Did you come up with a solution? Here's some code that adds together each item held in the list.
So you can see we've got another variable that we've called sum, and we've set that initial value to zero.
And then we are going through each item in the list starting at position[0], and we are adding that value to the variable sum.
Sam says, "This would work, but I'm not sure it's the best way." Can you think of another solution? Maybe pause the video whilst you have a think.
This program has been made more efficient using a loop.
So you can see this time on line 2 I still have my variable called sum and I'm setting its initial value to 0.
But then on line 3, I'm using a for loop to iterate through the list.
So for number in numbers and then within that for loop I have the statement sum = sum + number.
So this will loop through each item in the list until it gets to the end.
So if I added more numbers to my list, it would just keep going.
The for loop iterates through each item in the list and adds the value held to the current value of sum.
Here's another program which uses a list.
This program is being used to display the times table for a number entered by the user.
So you can see on line 3, we are asking the user which multiplication table they want.
And on line 4, we are storing that answer in a variable called multiplied by.
And then within our for loop, we are multiplying the number held in the list item by the value held by multiplied by.
Time to check your understanding.
Which symbol is used for the multiplication operator in Python? Is it A, B, or C? Pause the video whilst you have a think.
That's correct.
C, the asterisk is used for multiplication in Python.
In Python, the max and min functions can be used to return the highest or lowest values in a range.
Have a look carefully at this code.
What do you think the code would return? Maybe pause the Video whilst you have a think.
Did you select 12? Well done.
The program will return 12 because 12 is the highest value stored in the list.
Relational operators are used to compare values in conditions.
So here I have a table of some common relational operators.
Remember, the double equals symbol is used to compare two values and check if they're equal to each other.
The exploration followed by the equals is used for not equal to, to check if two values are not the same.
And then we have less than or less than or equal to and then greater than or greater than or equal to.
Time to check your understanding.
What line of code should be added to line 2 to find the lowest value held in the list numbers? Is it A: max_no = max (numbers)? B: min_no = max (numbers)? Or C: min_no = min (numbers)? Pause the video whilst you have a think.
That's right.
C is the correct answer.
min_no = min (numbers) will return the lowest value held in the list, because remember, the min function is used in Python to return the smallest value.
Mathematical operations can also be used to change or update values held in a list.
This program takes each number held in the list and multiplies the value by 2.
So you can see here the original list, my_list, contains the values 2, 3, and 4, and then we've got a variable called position, which is set to zero.
For our for loop, we are going through each number held in the list and we are using the line number = number * 2, so multiplying the value held in the list item.
But then this time we are using my_list[position] which is set to zero to start off with and we're assigning that to the new value held by number.
And then we are incrementing position by one.
So each time it goes through the loop that position value will change by one.
So you can see the expected output here is 4, 6, and 8 because we are multiplying each value in the list by 2.
Okay, we are moving on to the first set of tasks for today's lesson and you're doing a fantastic job so far.
So, well done.
I'd like you to open the starter program at oak.
link/test-scores A teacher has a list of test scores from the class stored in a list.
They want to calculate: A: the highest score in the test, B: the lowest score in the test, and C: the average score in the test.
Pause the video whilst you open the code and complete the activity.
How did you get on? Let's have a look at a sample answer together.
If you want to look at the full solution, you can go to oak.
link/test-scores-solution.
So you can see I've got my list on line 1 that goes down to line 3 with all of the test scores from the students.
On line 5, I have a variable called highest_score, and that's set to max(test_scores).
So that's taking the biggest value from my test scores and storing it in that variable.
And then using a print statement with an f string to print The highest score was, and then the value held by highest score.
Lines 7 and eight are similar, but this time we are using the min function and we are printing the lowest score.
And then on line 9 I have a variable called total, which I'm setting to zero.
So this is gonna be used to help me calculate the average.
I've then got a for loop that goes through each score held in the list test_scores and adds that into that total variable.
So total = total + score.
So it's just gonna cycle through the list and keep adding each list item to that variable called total.
When it's finished that, on line 12 we have a new variable called average, which equal to total divided by the length of the list, which is test scores.
So remember, to calculate the average, we add all of the values up and we divide them by how many there are.
And then lastly on line 13, I'm just printing out that result to the user.
So the average score was and then the value held by the variable average.
Remember, if you didn't quite get all of your code working correctly, you can always pause the video here and go back and make some changes.
For part three, three students were absent on the day of the test.
They scored 25, 32 and 49 marks respectively.
Add their scores to the list.
As a hint, you'll need to use the list.
append() method and do this before you calculate the average.
For part four, recalculate the average test score.
Has it changed? Pause the video whilst you complete the activities.
How did you get on? Did you manage to add the new scores? So here's some sample code from my program.
So I've just used the test_scores, which is the name of the list.
append method, and then in the brackets I've put the new values.
So you can see I've got 25, 32, and 49.
For part four, you were asked to recalculate the average test score and to state whether or not it had changed.
And yes, the average score had changed from 24.
8 to 25.
9.
For part five, the teacher realized that the students only had half the time that they were meant to have for the assessment.
To make their results fair, the teacher wants to double each mark held in the list.
So for part A, add some code to multiply each value held in the list by 2.
And then for part B, replace the current mark with the new mark for each list item.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to correct the test scores? So we've got the same program we had before, but you can see I've altered some code inside the for loop.
It's a bit small, so I've just pulled out the section of code that we've changed here.
So we've said score = score * 2.
And then we're putting in test_scores[Position], so remember position is starting at zero and being incremented each time, = score.
And then we are incrementing that position.
So position = position + 1.
So what this will do is it will loop through the list for the entire contents.
It will take the value held in the current position and multiply it by 2.
Okay, we are now moving on to the second part of today's lesson and you've done a great job so far, so well done.
We are going to create a game using a list.
Andeep says, "I want to create a game that uses a list, but I can't think of any ideas." Sam replies with: "You could store a list of colors from the rainbow and get the user to guess the color." That's a great idea, Sam.
Do you have any other game Ideas for Andeep? The shuffle() method is part of the random library in Python.
It takes a sequence, like a list, and reorganizes the order of items. So you can see here, on line 1, I have to import the random library, but then on line 4, I'm using the random.
shuffle() method to reorder the items held in the list called numbers.
So as a sample output, you can see the new order is 1, 4, 2, 3, and 5.
If I was to run this code multiple times, the order would be different each time.
Andeep says, "That's great.
I could use the shuffle() method to shuffle a list of colors and then ask the user to guess the color held in position[0]." That's a great idea, Andeep.
Ah, Andeep's written his code, but there seems to be a problem.
Andeep says, "I've written the program, but it asks the user to guess the color even when they've guessed it correctly.
What have I done wrong?" Perhaps take a look at the code, pause the video, and have a think.
Did you spot the error? There's not a flag to change the state of the while loop.
So the while loop is always true and will continue forever.
The code has been amended now to stop the while loop when the user guesses correctly.
So you can see, on line 6, we initiate the flag, which we've called guess, to False.
On line 7, in our while loop, we say that the while loop will continue whilst the value of guess is not equal to True.
And then on line 12, if the user guesses the color correctly, we're gonna change the value of guess to True.
So we're gonna change the status of the flag.
This means the next time the program goes to run the while loop, it won't meet the condition and the loop won't run.
You've seen previously how indexing can be used to return a specific item in a list.
So this code will display the item Andeep, as this is the value held in position[0] of the list.
Sometimes it's useful to only print a certain character of a string held in a list.
For example, if you wanted to only display the initial of the first name.
So this code will display "A," as that is the value held in the position[0][0] of the list.
So the first list item and the first position in the string.
Andeep wants to improve the program to give the user a hint if they guess incorrectly.
What line of code would reveal the first letter of the color to the user? Is it A, B, or C? Have a look carefully at the code and pause your video.
That's right, the correct answer is A.
That's because Python indexing starts at zero.
Okay, we are now moving on to the final set of tasks for today's lesson and you're doing a fantastic job so far, so well done.
I'd like you to open the starter program at oak.
link/card-game.
For part two, the aim of the game is for the user to guess the card the magician is holding.
So for A, create some code to shuffle the deck.
For part B, randomly select a card from the deck.
And as a hint, you'll need to use the random.
choice() function.
For part C, if the user guesses correctly, display "You guessed correctly".
And then for part D, if the user guesses incorrectly, ask them to guess again.
Pause the video whilst you complete the activity.
For part three, the user could do with some more help.
If the user guesses incorrectly, the program should: A: Check the card selected has a value greater than the user's guess, and if so, display "The card is higher".
B: Check if the card selected has a value lower than the user's guess, and if so, display "The card is lower".
Pause the video whilst you complete the activity.
How did you get on? Did you manage to create your card game? Great work.
I've got the solution here, but it's a bit small.
So if you want to open the full solution, you can go to oak.
link/card-game-solution.
So you can see here on line 4, we used the random shuffle() method to shuffle the cards.
We then set the flag called guess to False.
And then on line 6, we are picking a random number from the list or a random card.
On line 7, we have the while loop that will continue while the flag is not equal to True.
We ask the user to guess the card and we store their input as card_picked.
If the card picked by the user is equal to the picked card from the list or the random choice, then we say they've guessed correctly, else we ask them to try again.
And then that else we have another if else, which gives them the little bit of extra help.
So if card picked is less than the picked, the card is higher, else the card is lower.
Okay, for part four, we are going to add a competitive element to the game.
So for A, if the card selected from the deck has a value of less than 5, then the points awarded are the same as the value of the card.
For B, if the card selected from the deck has a value equal to or greater than 5, then the points awarded are double the value of the card.
And then for part C, display the score to the user.
So here's a couple of examples.
If the selected card was 2, then the points would be 2.
If the selected card is 5, then the points would be 10.
Pause the video here and complete the activity.
How did you get on? Let's have a look at the amended code together.
So I've just pulled out the new lines of code here.
So we've got another selection statement, if the user guesses correctly, so it's embedded in that first if, that says if card picked is greater than or equal to 5, then score is equal to 2 multiplied by the value of the card picked, else: the score is equal to picked and we are displaying that score to the user.
Remember, if you didn't quite get the solution correct, you can always go back and have a look through the slides or you can use this solution to make some changes to your own code.
Okay, we've come to the end of today's lesson and you've done a fantastic job, so, well done.
Let's summarize what we've learned together today.
Mathematical operations can be performed on list items using arithmetic operators.
Mathematical operations can also be used to alter or update the values held in a list.
The shuffle() method is used to randomly alter the order of the items held in a list.
A flag can be used to signal when a while loop should stop running.
I hope you enjoyed the lesson today and I hope you'll join me again soon.
Bye.