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. Um,
instead of using the x or some sort of time symbol,
you actually use the asterix.
So you would say something like 3 * 2 and that would be 3
times 2.
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 will print float instead of integer.
And this is just something that happens with division in Python.
But at the end of the day, you get the result that you need and it doesn't really
matter whether if it's a float or if it's a integer.
Now the last one that's really useful is two asterisk signs and this gives you
access to the exponents or when you want to raise the number to a power.
So for example,
if you wanted to get a hold of 2 to the power of 2, then you would write it
like this and 2 to the power of 2 is of course basically just 2 times 2
which is going to be equal to 4.
Now if I change this to 2 to the power of 3,
then that's going to be 2 times 2 times 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've remember 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 3 * 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, um,
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 through 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 we'll 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, our exponents,
we don't actually have an exponent here.
We don't have 2 to the power of 3 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 all 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.0. Now if math 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
visualize. 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 divided by 3.
So it's going to carry out that next and that becomes 1.
So now 9 + 1 is going to be the next thing because it's the most to the
left and then it becomes 10 - 3 and we finally get the result of 7.
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.
Alright, so this will involve a little bit of trial and error.
And the most important tool we have access to 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. So in this case,
we know that 3 * 3 is going to happen first,
and then the multiplication, and then the addition on the left,
and then the subtraction.
But 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 calculation 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 up to the next lesson and you'll be able to discover it there.