00:01
So here we want to figure out what these expressions result in in python and we will verify it by running it inside a python environment.
00:11
So one of the things you might notice here is that we're gonna evaluate it for both python 2 and python 3 and there will be some differences actually.
00:18
And the reason why is that many textbooks and in particular the textbook which has this exercise uses python 2 because in the time that it was written, while python 3 was out already, python 2 was still the most used version.
00:35
But nowadays python 3 is much much more common than python 2.
00:38
In fact, python 2 is what we would refer to as being sunsetted.
00:43
So something that's soon, you know, like the community will be not supporting python 2 anymore.
00:53
Okay, so the first thing that we want to evaluate is 9 minus 3 and that would just work with, you know, common arithmetic.
01:01
9 minus 3 is 6.
01:03
The next is 8 times 2 .5.
01:06
So the way i like to think about it, we have to do 8 times 2 which is 16 plus 8 times 0 .5 which is 4.
01:15
So 16 plus 4 which should give us 20.
01:19
And for both python 2 and python 3 that would also give us 20.
01:22
And in particular because we are multiplying an int and the float together and floats in theory, floats are a superset of ints.
01:36
So like every int can be represented as a float but not vice versa.
01:40
So float is sort of the bigger set here, which means that our answer will be a float not an int.
01:46
So it will be 20 .0 and the point 0 indicates that it's a float not an int.
01:51
Okay, now here we see the difference, right? so here we are doing 9 divided by 2 both of which are integers.
02:00
So in python 2 it results in an integer.
02:03
So 9 divided by 2 is 4 .5 and the way that python turns that into an integer is that it rounds down.
02:10
So 4 .5 rounded down is 4.
02:15
Now python 3 says, well, it's 4 .5.
02:18
So it's 4 .5 and it will represent it as a float.
02:23
Now 9 divided by negative 2, that's negative 4 .5, right? in python 2 again, these are both integers.
02:31
So the result will be an integer and negative 4 .5 rounded down.
02:37
So this is a little bit more tricky, right? if you draw the number line, right? so this is negative 5.
02:41
This is negative 4.
02:43
This is negative 4 .5.
02:45
So if we round down this should be negative 5.
02:49
And in python 3 again, it's negative 4 .5.
02:53
So python 3 doesn't bother with keeping it an integer despite both operands being integers.
03:00
So it stays negative 4 .5.
03:04
Okay, now we have sort of this other operator, which is an inner common arithmetic, but it's very useful for computer programming language, computer programming, which is the modulo operator.
03:16
So this is the operator.
03:19
You can think about this as a remainder operator, right? so if we divide 9 by 2, what is the remainder? it will be 1.
03:27
And in python 3, it will be the same.
03:29
It will be 1.
03:30
Now, if we divide 9 by negative 2, right? so what is 9 divided by negative 2? it's going to be negative, you know, to get the closest you can to 9, you would multiply negative 2 by negative 4 to get 8.
03:49
And the remainder is 1.
03:51
But in python, the second operator, sorry, the second operand determines the sign of the modulo result.
04:04
So 9 mod negative 2 is negative 1 rather than 1.
04:08
And that's the same in python 3.
04:11
Now, negative 9 mod 2, again, the sign is taken from the second operand.
04:18
If you divide negative 9 by 2, it's not an even number, so it doesn't have remainder 0.
04:23
It's an odd number, so it must have remainder 1.
04:26
And again, the sign is going to be positive because 2, which is the second operand, is positive.
04:32
So it will be 1.
04:34
Now, 9 divided by negative 2 .0, and the difference between this and this, these two, or sorry, rather these two, is that this now is a float.
04:46
So the result, right? so this is an integer divided by a float.
04:49
So the result will be a float in both python 2 and python 3.
04:54
So here we have negative 4 .5 and negative 4 .5, which are both floats.
05:01
And finally, here, these last two sort of demonstrate order of operations, right? if you guys remember order of operations in grade school, it's pemdas, where multiplication comes before addition...