In the previous lesson, we saw how we could use the plus sign (+) either to concatenate strings together
or as a mathematical operation where we add two integers or two floats, basically two numbers together.
In this lesson, I want to show you some of the other mathematical operators that you have access to
in addition to adding.
The next obvious one is subtraction.
So, 7 - 3, you would just use the minus sign.
But then when you get to multiplication it's a little bit weird.
Instead of using the X or some sort of times symbol, you actually use the asterisks.
So you would say something like, 3 * 2,
and that would be three times two.
Now the final one is division.
And that's done using the forward slash (/).
So, 6 / 3,
and when that prints out, it'll give you 2.
Now, one thing to notice here is that whenever you're dividing things, you actually always end up
with a floating point number.
So you can see even though six divides into three cleanly, we're still getting 2.0.
And in fact, if I put a type-check around this division, you'll see that it'll print float instead of
integer.
And this is just the default Python behavior.
It's something called implicit typecasting, because Python is implicitly converting the result here
into a floating point number.
Now, sometimes you might not want that.
So there is another Python division operator that might be worth knowing about.
And it simply done with two forward slashes (//).
So now when I run it, you can see what comes out is actually an integer.
And I can do a type-check just to confirm that as well.
Instead of float, it's coming out as an integer.
But you have to be very careful with this second type of division operator, because what it's doing
behind the scenes is it's actually doing this step first, and then it's simply just removing all of
the decimal places.
So what if instead of six divided by three, which we know divides cleanly into two, what if we divided
five by three?
What is the result of that?
That's 1.6 recurring.
So if I change this into, 5 //3, using the double forward slash, you'll see that it just
gives me 1 and it wipes out all the rest of the numbers in this integer conversion, which can be
quite dangerous if you're working with scientific numbers. But it can be useful when you know that
you want an end result that has no decimal places.
Now, the last one that's really useful is two asterisk signs (**),
and this gives you access to the exponent.
Or when you want to raise a number to a power.
So for example, if you wanted to get hold of two to the power of two, then you would write it like
this (2**2), and 2² is of course basically just 2 * 2, which is going to be equal
to 4.
Now if I change this to 2³, then that's going to be 2 * 2 * 2.
So that should be 8.
And so on and so forth.
Having the exponent being built into the language is one of the reasons why Python is really loved by
a lot of data scientists and mathematicians, because it's really optimized towards manipulating and
handling numbers.
Now, one of the things that you have to be careful about when you are doing these mathematical operations
is when you have more than one operation on the same line of code.
Then there's a certain level of priority.
So some of these operations, like division or multiplication, are going to be first class, whereas
other ones are going to be more economy like the plus and minus.
And the rule that you might have remembered from high school is something called PEMDAS.
It basically states the order of priority is Parentheses, Exponents, Multiplication, Division, Addition,
and Subtraction.
So the things that happen first are the things inside brackets ( ), then it's our exponents, then it's
our multiplication * and division /.
And finally, the lowest priority is our addition + and subtraction -.
Now it's a little bit deceiving because of this order.
It makes it seem like as if multiplication happens before division, but actually they are equally important.
And when it actually comes to your calculations, the calculation that's most to the left is the one
that will be prioritized between multiplication and division.
So let me give you a real life example to make this more clear.
Let's say we had a line of code where we wanted to multiply three by three, plus, three divided by three,
minus three (3 * 3 + 3 -3).
If I was to execute this entire line of code and print it out into the console, here's the time where
you play computer again and guess using what you've learned here,
what exactly will be printed?
Because you will get a number printed.
It will calculate this entire line of code for you.
But the order matters.
Is it going to first add 3 to 3, then multiply the result by 3?
Or is it first going to divide 3 by 3 and then add 3 to it.
What is the order?
And I want you to really pause the video and have a little play with it on pen and paper before you
come back, and will show you the result.
Using our rule, PEMDAS, we can see the first thing that happens is what's in the parentheses, so
that doesn't matter.
The next thing are exponents.
We don't actually have an exponent here.
We don't have 2³ or something to the power of anything.
So we can ignore that as well.
So the next level is the multiplication and division,
and as I mentioned they are of equal importance.
So this 3 * 3 and 3 / 3 are both equally important,
but the calculation goes from left to right.
So the first thing we see is actually the multiplication.
So if it helps you, you might want to add LR to the end of this mnemonic.
So it becomes PEMDASLR
Or at least that's the way I would think about it.
So even if this was division and this was multiplication, this calculation will always get executed
first.
Coming back to the question I asked you, what do you think this number would be?
Let's go ahead and comment out all the other code and run this line of code.
And it will give us 7.
Now if maths is not your strong point, don't worry, it's not mine either,
and you're the sort of person who would prefer to see it
visualised,
then I recommend again putting this line of code in Thonny, and then go ahead and clicking on the debugging
symbol, and then just step into.
So press F7 or this button multiple times and you'll see it evaluate this line of code step by step.
So first it looks at the entire thing,
and then it goes from left to right.
And the first calculation is 3 * 3.
And that is the one that's going to execute first,
and it becomes 9.
Next, it's going to look along this line of code and see that the next most important thing is this division
here, 3 / 3.
So it's going to carry out that next,
and that becomes 1.
So now 9 + 1.0 is going to be the next thing because it's the most to the left.
And then it becomes 10.0 - 3, and we finally get the result of 7.0.
So now here's another challenge for you,
"How can you change this code so that instead of getting 7, we get 3?"
How can you change this line of code given what you know about PEMDASLR?
See if you can figure it out.
Pause the video now.
All right, so this will involve a little bit of trial and error,
and the most important tool we have access to us is the parentheses or the brackets.
This means that we can actually isolate bits of our code, which normally have very low priority, and
turn them into higher priority operations.
If we added a set of parentheses around our 3 + 3, then out of all of these calculations,
this particular one suddenly becomes the highest priority and it will happen first.
So if I change what I've got in Thonny to our new version and go ahead and debug through it, the very
first calculation it's going to perform is this 3 + 3 inside the brackets.
And we end up with 6.
So then it's going to go again from left to right prioritizing multiplication and division.
So then it's 3 * 6 is 18.
18 / 3 is 6. 6 - 3 is 3.
Just by isolating certain calculations, you can elevate it to right at the top of the priority list,
and you will be able to perform the calculations that you need.
Now that you've learned a lot about calculations and performing mathematical operations using Python,
I've got another code challenge for you.
So head over to the next lesson and you'll be able to discover it there.