Loading...
Hello, my name is Mrs. Holbrook, and welcome to Computing.
I'm so pleased that you've decided to join me for the lesson today.
In today's lesson, we're going to be looking at some common arithmetic operators that are used in programming.
These can be used to really develop your selection statements, and make your programmes more advanced.
Welcome to today's lesson from the unit Programming Sequence.
This lesson is called Arithmetic Operators.
And by the end of today's lesson, you'll be able to use a variety of arithmetic operators in a programme.
Shall we make a start? We'll be using these keywords throughout today's lesson.
Operator.
Operator.
A symbol or word that instructs the computer to perform a specific calculation or action.
Arithmetic expression.
Arithmetic expression.
An expression that results in a numeric value.
Integer division.
Integer division.
Sometimes known as floor division, is when any remainder of a division is discarded.
Modulo division.
Modulo division.
An operation that returns the remainder of a division.
Look out for these key words throughout today's lesson.
Today's lesson is split into two parts.
We'll start by using common arithmetic operators in a programme.
We'll then move on to compare modulo and integer division.
Let's make a start by using common arithmetic operators in a programme.
An operator is a symbol or special word that instructs a computer to perform a specific calculation or action.
So here's some examples.
So we have the plus symbol, which is used for addition.
We have the dash, which is used for subtraction.
We have the asterisk, which is used for multiplication.
We have the forward slash, which is used for division in computing.
And then we have the double asterisk, which is used for exponentiation.
An operator can be used in an arithmetic expression to calculate a value.
Let's have a look at these examples in the table.
So here, for addition, we have score plus one.
This will be the value of score, add one.
Score minus one.
This will be the value of score subtract one.
Score, asterisk, two.
The value of score multiplied by two.
Score, forward slash, two.
The value of score divided by two.
And then lastly, score, double asterisk, two.
The value of score to the power of two.
Time to check your understanding.
In Python, which symbol is used to perform division? Is it A, B, C, or D? Pause the video whilst you have a think.
That's right.
The forward slash is used to perform division in Python.
Remember to carefully look the way the slash is facing.
We need the forward slash for division.
Izzy says, "Imagine an arithmetic expression with lots of operations, for example, plus, minus, multiplication, division, et cetera.
How does Python know which one to do first?" Andeep's got a great response.
Andeep says, "It follows the order and priority of operations." Let's have a look at what that means.
The order of operations runs from the top to the bottom of this pyramid.
So we start with brackets, we then move on to indices, then multiplication and division, and then lastly, addition and subtraction.
Let's have a look at some of these in practise.
So what value will be held by the variable number here? So we've got number is equal to 10 minus two plus two, multiplied by five.
Maybe pause the video and have a quick think.
There are no brackets, indices, or division in this expression.
So we start with the multiplication.
Two multiplied by five is 10.
10 minus two, plus 10, is now the calculation.
Add and subtract should be read from left to right.
If the subtract appears first, then this should be carried out first.
So 10 minus two is eight.
So we're now left with the calculation, eight plus 10.
Eight plus 10 is 18, so number is equal to 18.
Time to check your understanding.
What is first in the order of operations? Is it A, indices? B, multiplication and division? or C, brackets? Pause the video whilst you have a think.
That's right.
Brackets is first in the order of operations.
So it goes at the top of our pyramid.
An expression can use the values stored in variables.
To work out this expression, the days variable must have been assigned a value first.
So you can see here on line two, we can't use days, unless days has been assigned a value, which it has done on line one.
When running a programme, a variable must have a value before that value is used in an expression.
Okay, we're moving on to the first task of today's lesson.
And you are doing a fantastic job so far.
So, well done.
I'd like you to start by creating a new Python programme in your chosen IDE.
I'd then like you to create a programme that calculates BMI, or body mass index.
The calculation for BMI is here for you.
So BMI is equal to weight in kilogrammes, or kg, divided by the square of height in metres, so metres squared.
To do this, you need to ask for the user's weight in kilogrammes, and store it in the variable weight.
You then need to ask for the user's height in centimetres, and store it in the variable height.
And then you need to calculate the BMI, and display it to the user.
As a tip, you'll need to convert the height to metres.
And the calculation for this is height divided by 100.
Pause the video whilst you have a go at the activity.
How did you get on? Did you manage to create your programme? Great work.
Let's have a look at a sample answer together.
Remember, your code may look slightly different, but let's just have a look at this together.
So on line one, we have a print statement which asks the user to enter the weight in kilogrammes.
On line two, we're storing that as a variable core weight and I'm storing it as a float value.
That's because it could hold some decimal points.
On line three, I have a similar print statement, but this time I'm asking for the height.
And on line four, again I'm storing that as a float, under the variable name height.
On line six, I have the calculation.
So I have BMI is equal to, and I've put this inside brackets, so we've got some order of operations going on here.
Weight divided by another open bracket, height divided by 100, close bracket, double asterisk, so to the power of, and then we have two.
And then I'm closing my bracket.
And then, on line seven, I'm printing out the BMI to the user.
Remember, if you struggled with this task, you can have a look at the code that's on the screen now, and go back and have another go.
Okay, we've come to the second part of today's lesson, and you're doing a great job, so, well done.
We're now going to compare modulo and integer division.
So far you've only seen real division.
In real division, there is no remainder, because the entire value is divided.
So you can see here we have number is equal to 14 divided by three, and then we're printing number out.
And the result of that is four point, and then lots of sixes and seven.
Integer division, which is sometimes known as floor division is when any remainder of a division is discarded, and you're only left with a whole number as an integer.
So this time, I've got number is equal to 14, and I've got two forward slashes this time that represents integer division in Python.
And then three and then I'm printing number out again.
But this time you can see I haven't got all of those values after the decimal point.
I've just got the number four.
So we've taken away any remainder.
Again note the use of double forward slash for integer division.
The modulo division operation will return only the remainder of a division.
So this time we've got number is equal to 14%, three, and we're printing number out.
But this time, we get the answer two.
So this is the remainder.
So what is left after three has gone into 14, four times.
And the answer to that is two.
So note the use of the percentage symbol for modulo division.
Time to check your understanding.
Which symbol is used for modulo division? Is it A, B, or C? Pause the video whilst you have a think.
That's right.
The percent symbol is used for modulo, or mod division.
Well done.
If you divide 14 counters between three people, then there'll be four counters each, with two remaining.
So you can see I've got my three people here.
They've each got four different-colored counters.
And then I've got two counters remaining.
So here in the programme, I have two different lines.
So I have on line one, counters_each is equal to 14//three.
And then I've got counters remaining is equal to 14% three.
So we're doing different types of division here.
And you can see the output from that is four and two.
So the mod doesn't simply store the value that was discarded from integer division.
It stores the whole number that was remaining.
So integer division has returned four, and modulo division has returned two.
Andeep has a really good question.
He asks, "Why would I use different types of division?" Maybe pause the video and have a think.
You would use modulo division when you need to find out the remaining whole value.
This will be helpful in many cases.
But an example might be when you want to find out if a value is odd or even.
If the value evaluates as a one, then it's an odd number.
Time to check your understanding.
What value will be held by the variable number in this programme? Will it be A, six? B, one? Or C, 6.
25? Pause the video whilst you have a think.
That's right.
The correct answer is B, one.
We're using the percentage symbol here, which means we are using modulo division.
So what is the remainder of 25 divided by four? Okay, we're moving on to our last set of tasks for today's lesson.
And you're doing a great job so far.
So, well done.
I'd like you to open the starter programme, oak.
link/split-my-pizza.
The programme should ask for the number of slices on the pizza.
Ask for how many people are sharing the pizza.
Reveal how many slices each person can have, and reveal if there will be any slices remaining.
Hint, you'll need to use modulo and integer division for this programme.
Pause the video whilst you have a go.
How did you get on? Did you manage to create your programme? Great work.
Let's have a look at a sample answer together.
So on line one, we have a print statement with an introduction to the system.
On line two, we asked the user, "How many slices does the pizza have?" And on line three, we're storing that as an integer value as slices_total.
On line four, we're asking the user how many people are sharing.
And again, we're storing that user input as an integer under the variable, this time called people.
On line seven, we have a calculation which says slices_each is equal to slices_total//people.
So we're using integer division here.
On line eight, we have slices_remaining is equal to slices_total, but this time %people, so we're using modulo division.
And then on line 10 and 11, we're printing out the results.
So on line 10, print, there are, and then we're using the variable slices each, and then followed by the text slices each, so it will return something like, "There are three slices each." On line 11, it then prints out there will be, and then the number of slices remaining, if there is a remainder.
If you want to go and have a go at this solution, or use this solution and see how it runs, you can go to oak.
link/split-my-pizza-solution.
Okay, 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 learned together today.
Arithmetic expressions are used to calculate values.
Arithmetic expressions can be used in selection statements.
Expressions either return as true or false.
In real division, there is no remainder, because the entire value is divided.
Integer division is when any remainder of a division is discarded.
Modulo division, or mod, will return only the remainder of a division.
I hope you've enjoyed today's lesson and you've worked really hard, so well done.
See you again soon.
Bye!.