WEBVTT

0
00:00.410 --> 00:07.760
In the previous lesson, we saw how we could use the plus sign (+) either to concatenate strings together

1
00:07.760 --> 00:15.650
or as a mathematical operation where we add two integers or two floats, basically two numbers together.

2
00:16.100 --> 00:21.410
In this lesson, I want to show you some of the other mathematical operators that you have access to

3
00:21.410 --> 00:23.420
in addition to adding.

4
00:23.420 --> 00:25.760
The next obvious one is subtraction.

5
00:25.760 --> 00:29.270
So, 7 - 3, you would just use the minus sign.

6
00:29.270 --> 00:32.620
But then when you get to multiplication it's a little bit weird.

7
00:32.890 --> 00:38.890
Instead of using the X or some sort of times symbol, you actually use the asterisks.

8
00:38.890 --> 00:41.620
So you would say something like, 3 * 2,

9
00:41.620 --> 00:44.530
and that would be three times two.

10
00:45.040 --> 00:46.990
Now the final one is division.

11
00:46.990 --> 00:49.690
And that's done using the forward slash (/).

12
00:49.690 --> 00:52.330
So, 6 / 3,

13
00:52.600 --> 00:56.210
and when that prints out, it'll give you 2.

14
00:56.660 --> 01:02.630
Now, one thing to notice here is that whenever you're dividing things, you actually always end up

15
01:02.630 --> 01:04.340
with a floating point number.

16
01:04.340 --> 01:09.320
So you can see even though six divides into three cleanly, we're still getting 2.0.

17
01:09.920 --> 01:18.020
And in fact, if I put a type-check around this division, you'll see that it'll print float instead of

18
01:18.020 --> 01:18.800
integer.

19
01:19.150 --> 01:22.090
And this is just the default Python behavior.

20
01:22.090 --> 01:29.650
It's something called implicit typecasting, because Python is implicitly converting the result here

21
01:29.650 --> 01:32.170
into a floating point number.

22
01:32.200 --> 01:34.870
Now, sometimes you might not want that.

23
01:34.870 --> 01:41.260
So there is another Python division operator that might be worth knowing about.

24
01:41.260 --> 01:44.210
And it simply done with two forward slashes (//).

25
01:44.210 --> 01:49.070
So now when I run it, you can see what comes out is actually an integer.

26
01:49.070 --> 01:53.300
And I can do a type-check just to confirm that as well.

27
01:53.300 --> 01:55.850
Instead of float, it's coming out as an integer.

28
01:55.850 --> 02:02.810
But you have to be very careful with this second type of division operator, because what it's doing

29
02:02.810 --> 02:09.730
behind the scenes is it's actually doing this step first, and then it's simply just removing all of

30
02:09.730 --> 02:10.960
the decimal places.

31
02:10.960 --> 02:17.440
So what if instead of six divided by three, which we know divides cleanly into two, what if we divided

32
02:17.440 --> 02:19.390
five by three?

33
02:19.390 --> 02:21.280
What is the result of that?

34
02:21.280 --> 02:23.740
That's 1.6 recurring.

35
02:23.740 --> 02:30.760
So if I change this into, 5 //3, using the double forward slash, you'll see that it just

36
02:30.760 --> 02:37.590
gives me 1 and it wipes out all the rest of the numbers in this integer conversion, which can be

37
02:37.590 --> 02:42.960
quite dangerous if you're working with scientific numbers. But it can be useful when you know that

38
02:42.960 --> 02:46.860
you want an end result that has no decimal places.

39
02:47.640 --> 02:51.600
Now, the last one that's really useful is two asterisk signs (**),

40
02:51.600 --> 02:54.840
and this gives you access to the exponent.

41
02:54.840 --> 02:57.690
Or when you want to raise a number to a power.

42
02:57.690 --> 03:04.210
So for example, if you wanted to get hold of two to the power of two, then you would write it like

43
03:04.210 --> 03:10.810
this (2**2), and 2² is of course basically just 2 * 2, which is going to be equal

44
03:10.810 --> 03:11.920
to 4.

45
03:13.060 --> 03:18.940
Now if I change this to 2³, then that's going to be 2 * 2 * 2.

46
03:18.970 --> 03:21.130
So that should be 8.

47
03:21.280 --> 03:23.190
And so on and so forth.

48
03:23.610 --> 03:29.460
Having the exponent being built into the language is one of the reasons why Python is really loved by

49
03:29.460 --> 03:35.070
a lot of data scientists and mathematicians, because it's really optimized towards manipulating and

50
03:35.070 --> 03:36.240
handling numbers.

51
03:36.540 --> 03:41.790
Now, one of the things that you have to be careful about when you are doing these mathematical operations

52
03:41.790 --> 03:46.170
is when you have more than one operation on the same line of code.

53
03:46.170 --> 03:50.080
Then there's a certain level of priority.

54
03:50.110 --> 03:56.800
So some of these operations, like division or multiplication, are going to be first class, whereas

55
03:56.800 --> 04:00.370
other ones are going to be more economy like the plus and minus.

56
04:00.370 --> 04:05.590
And the rule that you might have remembered from high school is something called PEMDAS.

57
04:05.770 --> 04:13.080
It basically states the order of priority is Parentheses, Exponents, Multiplication, Division, Addition,

58
04:13.080 --> 04:14.250
and Subtraction.

59
04:14.250 --> 04:20.250
So the things that happen first are the things inside brackets ( ), then it's our exponents, then it's

60
04:20.250 --> 04:23.190
our multiplication * and division /.

61
04:23.190 --> 04:28.950
And finally, the lowest priority is our addition + and subtraction -.

62
04:28.950 --> 04:32.490
Now it's a little bit deceiving because of this order.

63
04:32.490 --> 04:39.590
It makes it seem like as if multiplication happens before division, but actually they are equally important.

64
04:39.590 --> 04:44.960
And when it actually comes to your calculations, the calculation that's most to the left is the one

65
04:44.960 --> 04:48.320
that will be prioritized between multiplication and division.

66
04:48.320 --> 04:51.980
So let me give you a real life example to make this more clear.

67
04:52.010 --> 04:59.930
Let's say we had a line of code where we wanted to multiply three by three, plus, three divided by three,

68
04:59.930 --> 05:01.010
minus three (3 * 3 + 3 -3).

69
05:01.980 --> 05:08.820
If I was to execute this entire line of code and print it out into the console, here's the time where

70
05:08.820 --> 05:13.500
you play computer again and guess using what you've learned here,

71
05:13.740 --> 05:16.170
what exactly will be printed?

72
05:16.170 --> 05:17.910
Because you will get a number printed.

73
05:17.910 --> 05:20.670
It will calculate this entire line of code for you.

74
05:20.670 --> 05:22.830
But the order matters.

75
05:22.830 --> 05:28.970
Is it going to first add 3 to 3, then multiply the result by 3?

76
05:28.970 --> 05:33.380
Or is it first going to divide 3 by 3 and then add 3 to it.

77
05:33.410 --> 05:34.820
What is the order?

78
05:34.820 --> 05:41.000
And I want you to really pause the video and have a little play with it on pen and paper before you

79
05:41.000 --> 05:43.310
come back, and will show you the result.

80
05:46.220 --> 05:52.270
Using our rule, PEMDAS, we can see the first thing that happens is what's in the parentheses, so

81
05:52.270 --> 05:53.260
that doesn't matter.

82
05:53.290 --> 05:55.180
The next thing are exponents.

83
05:55.180 --> 05:56.740
We don't actually have an exponent here.

84
05:56.740 --> 06:00.580
We don't have 2³ or something to the power of anything.

85
06:00.580 --> 06:02.170
So we can ignore that as well.

86
06:02.170 --> 06:06.490
So the next level is the multiplication and division,

87
06:06.490 --> 06:09.970
and as I mentioned they are of equal importance.

88
06:09.970 --> 06:15.610
So this 3 * 3 and 3 / 3 are both equally important,

89
06:15.620 --> 06:19.310
but the calculation goes from left to right.

90
06:19.310 --> 06:22.790
So the first thing we see is actually the multiplication.

91
06:22.790 --> 06:28.490
So if it helps you, you might want to add LR to the end of this mnemonic.

92
06:28.490 --> 06:29.990
So it becomes PEMDASLR

93
06:30.890 --> 06:33.050
Or at least that's the way I would think about it.

94
06:33.170 --> 06:40.640
So even if this was division and this was multiplication, this calculation will always get executed

95
06:40.640 --> 06:41.210
first.

96
06:41.830 --> 06:46.450
Coming back to the question I asked you, what do you think this number would be?

97
06:46.480 --> 06:51.970
Let's go ahead and comment out all the other code and run this line of code.

98
06:51.970 --> 06:54.070
And it will give us 7.

99
06:54.100 --> 06:57.670
Now if maths is not your strong point, don't worry, it's not mine either,

100
06:57.670 --> 06:59.860
and you're the sort of person who would prefer to see it

101
06:59.860 --> 07:00.610
visualised,

102
07:00.610 --> 07:06.230
then I recommend again putting this line of code in Thonny, and then go ahead and clicking on the debugging

103
07:06.230 --> 07:08.960
symbol, and then just step into.

104
07:08.960 --> 07:15.440
So press F7 or this button multiple times and you'll see it evaluate this line of code step by step.

105
07:15.440 --> 07:17.570
So first it looks at the entire thing,

106
07:17.570 --> 07:19.550
and then it goes from left to right.

107
07:19.550 --> 07:23.240
And the first calculation is 3 * 3.

108
07:23.240 --> 07:25.940
And that is the one that's going to execute first,

109
07:25.940 --> 07:27.350
and it becomes 9.

110
07:27.380 --> 07:33.190
Next, it's going to look along this line of code and see that the next most important thing is this division

111
07:33.190 --> 07:35.080
here, 3 / 3.

112
07:35.110 --> 07:37.330
So it's going to carry out that next,

113
07:37.330 --> 07:38.650
and that becomes 1.

114
07:38.650 --> 07:44.710
So now 9 + 1.0 is going to be the next thing because it's the most to the left.

115
07:44.710 --> 07:49.630
And then it becomes 10.0 - 3, and we finally get the result of 7.0.

116
07:50.170 --> 07:52.660
So now here's another challenge for you,

117
07:52.690 --> 07:59.490
"How can you change this code so that instead of getting 7, we get 3?"

118
07:59.520 --> 08:04.350
How can you change this line of code given what you know about PEMDASLR?

119
08:04.650 --> 08:06.270
See if you can figure it out.

120
08:06.270 --> 08:07.410
Pause the video now.

121
08:11.670 --> 08:15.270
All right, so this will involve a little bit of trial and error,

122
08:15.270 --> 08:22.930
and the most important tool we have access to us is the parentheses or the brackets.

123
08:22.960 --> 08:28.750
This means that we can actually isolate bits of our code, which normally have very low priority, and

124
08:28.750 --> 08:31.450
turn them into higher priority operations.

125
08:31.450 --> 08:40.030
If we added a set of parentheses around our 3 + 3, then out of all of these calculations,

126
08:40.030 --> 08:45.090
this particular one suddenly becomes the highest priority and it will happen first.

127
08:45.660 --> 08:52.380
So if I change what I've got in Thonny to our new version and go ahead and debug through it, the very

128
08:52.380 --> 08:56.850
first calculation it's going to perform is this 3 + 3 inside the brackets.

129
08:56.850 --> 08:58.980
And we end up with 6.

130
08:58.980 --> 09:04.050
So then it's going to go again from left to right prioritizing multiplication and division.

131
09:04.050 --> 09:07.230
So then it's 3 * 6 is 18.

132
09:07.230 --> 09:11.380
18 / 3 is 6. 6 - 3 is 3.

133
09:12.010 --> 09:18.850
Just by isolating certain calculations, you can elevate it to right at the top of the priority list,

134
09:18.850 --> 09:22.540
and you will be able to perform the calculations that you need.

135
09:23.350 --> 09:29.080
Now that you've learned a lot about calculations and performing mathematical operations using Python,

136
09:29.080 --> 09:31.150
I've got another code challenge for you.

137
09:31.150 --> 09:34.300
So head over to the next lesson and you'll be able to discover it there.