Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,670 --> 00:00:05,670
In the previous lesson we saw how we could use the plus sign either to
2
00:00:05,770 --> 00:00:10,770
concatenate strings together or as a mathematical operation where we add two
3
00:00:11,710 --> 00:00:15,370
integers or two floats, basically two numbers together.
4
00:00:16,390 --> 00:00:20,230
In this lesson I want to show you some of the other mathematical operators that
5
00:00:20,230 --> 00:00:21,220
you have access to
6
00:00:21,670 --> 00:00:25,810
in addition to adding. The next obvious one is subtraction.
7
00:00:25,840 --> 00:00:29,080
So 7 - 3 , you would just use the minus sign,
8
00:00:29,590 --> 00:00:33,130
but then when you get to multiplication it's a little bit weird. Um,
9
00:00:33,160 --> 00:00:37,060
instead of using the x or some sort of time symbol,
10
00:00:37,390 --> 00:00:38,950
you actually use the asterix.
11
00:00:39,070 --> 00:00:43,630
So you would say something like 3 * 2 and that would be 3
12
00:00:43,630 --> 00:00:44,470
times 2.
13
00:00:45,160 --> 00:00:50,160
Now the final one is division and that's done using the forward-slash, so 6
14
00:00:50,800 --> 00:00:54,820
/ 3 and when that prints out,
15
00:00:54,850 --> 00:00:56,020
it'll give you 2.
16
00:00:56,830 --> 00:01:00,550
Now one thing to notice here is that whenever you're dividing things,
17
00:01:01,030 --> 00:01:04,360
you actually always end up with a floating point number.
18
00:01:04,450 --> 00:01:07,420
So you can see even though six divides into three cleanly,
19
00:01:07,630 --> 00:01:12,630
we're still getting 2.0. And in fact if I put a type check around this division,
20
00:01:14,890 --> 00:01:18,520
you'll see that it will print float instead of integer.
21
00:01:19,210 --> 00:01:21,880
And this is just something that happens with division in Python.
22
00:01:22,420 --> 00:01:26,200
But at the end of the day, you get the result that you need and it doesn't really
23
00:01:26,200 --> 00:01:29,350
matter whether if it's a float or if it's a integer.
24
00:01:30,220 --> 00:01:35,220
Now the last one that's really useful is two asterisk signs and this gives you
25
00:01:35,500 --> 00:01:39,760
access to the exponents or when you want to raise the number to a power.
26
00:01:40,210 --> 00:01:40,930
So for example,
27
00:01:40,930 --> 00:01:45,930
if you wanted to get a hold of 2 to the power of 2, then you would write it
28
00:01:46,270 --> 00:01:51,270
like this and 2 to the power of 2 is of course basically just 2 times 2
29
00:01:52,210 --> 00:01:54,040
which is going to be equal to 4.
30
00:01:55,600 --> 00:01:58,660
Now if I change this to 2 to the power of 3,
31
00:01:58,660 --> 00:02:03,660
then that's going to be 2 times 2 times 2. So that should be 8 and so
32
00:02:04,360 --> 00:02:05,260
on and so forth.
33
00:02:05,890 --> 00:02:09,610
Having the exponent being built into the language is one of the reasons why
34
00:02:09,610 --> 00:02:14,610
Python is really loved by a lot of data scientists and mathematicians because
35
00:02:14,800 --> 00:02:19,300
it's really optimized towards manipulating and handling numbers. Now,
36
00:02:19,300 --> 00:02:22,510
one of the things that you have to be careful about when you are doing these
37
00:02:22,510 --> 00:02:27,280
mathematical operations is when you have more than one operation on the same
38
00:02:27,280 --> 00:02:32,020
line of code, then there's a certain level of priority.
39
00:02:32,710 --> 00:02:37,710
So some of these operations like division or multiplication are going to be
40
00:02:37,900 --> 00:02:38,740
first class,
41
00:02:38,770 --> 00:02:42,460
whereas other ones are going to be more economy like the plus and minus.
42
00:02:42,940 --> 00:02:47,140
And the rule that you might've remember from high school is something called
43
00:02:47,170 --> 00:02:48,003
PEMDAS.
44
00:02:48,250 --> 00:02:52,750
It basically states the order of priority is parentheses,
45
00:02:52,780 --> 00:02:56,290
exponents, multiplication, division, addition and subtraction.
46
00:02:56,800 --> 00:03:01,180
So the things that happen first are the things inside brackets, then it's our exponents,
47
00:03:01,630 --> 00:03:05,320
then it's our multiplication and division.
48
00:03:05,680 --> 00:03:10,680
And finally, the lowest priority is our addition and subtraction.
49
00:03:11,530 --> 00:03:14,740
Now it's a little bit deceiving because of this order.
50
00:03:14,830 --> 00:03:18,400
It makes it seem like as if multiplication happens before division,
51
00:03:18,850 --> 00:03:21,580
but actually they are equally important.
52
00:03:22,150 --> 00:03:24,640
And when it actually comes to your calculations,
53
00:03:24,850 --> 00:03:28,360
the calculation that's most to the left is the one that will be prioritized
54
00:03:28,390 --> 00:03:30,280
between multiplication and division.
55
00:03:30,850 --> 00:03:34,120
So let me give you a real-life example to make this more clear.
56
00:03:34,540 --> 00:03:39,540
Let's say we had a line of code where we wanted to multiply 3 * 3 +
57
00:03:39,940 --> 00:03:43,150
3 / 3 - 3.
58
00:03:44,500 --> 00:03:49,500
If I was to execute this entire line of code and print it out into the console,
59
00:03:50,170 --> 00:03:54,670
here's the time where you play computer again and guess using what you've
60
00:03:54,670 --> 00:03:56,350
learned here, um,
61
00:03:56,470 --> 00:04:00,190
what exactly will be printed because you will get a number printed,
62
00:04:00,190 --> 00:04:04,990
it will calculate this entire line of code for you. But the order matters.
63
00:04:05,260 --> 00:04:10,260
Is it going to first add 3 to 3 then multiply the result by 3 or is
64
00:04:11,710 --> 00:04:15,610
it first going to divide through by 3 and then add 3 to it?
65
00:04:15,910 --> 00:04:17,050
What is the order?
66
00:04:17,260 --> 00:04:21,760
And I want you to really pause the video and have a little play with it on pen
67
00:04:21,760 --> 00:04:25,450
and paper before you come back and we'll show you the result.
68
00:04:28,660 --> 00:04:30,400
Using our rule, PEMDAS,
69
00:04:30,430 --> 00:04:34,210
we can see the first thing that happens is what's in the parentheses,
70
00:04:34,270 --> 00:04:37,480
so that doesn't matter. The next thing, our exponents,
71
00:04:37,510 --> 00:04:39,040
we don't actually have an exponent here.
72
00:04:39,040 --> 00:04:42,790
We don't have 2 to the power of 3 or something to the power of anything.
73
00:04:42,880 --> 00:04:44,140
So we can ignore that as well.
74
00:04:44,740 --> 00:04:48,700
So the next level is the multiplication and division.
75
00:04:48,940 --> 00:04:52,120
And as I mentioned, they are all equal importance.
76
00:04:52,450 --> 00:04:57,450
So this 3 * 3 and 3 / 3 are both equally important,
77
00:04:58,120 --> 00:05:01,540
but the calculation goes from left to right.
78
00:05:01,720 --> 00:05:06,220
So the first thing we see is actually the multiplication. So if it helps you,
79
00:05:06,310 --> 00:05:10,690
you might want to add LR to the end of this mnemonic.
80
00:05:10,780 --> 00:05:15,130
So it becomes PEMDASLR, or at least that's the way I would think about it.
81
00:05:15,730 --> 00:05:20,080
So even if this was division and this was multiplication,
82
00:05:20,500 --> 00:05:23,320
this calculation will always get executed first.
83
00:05:24,190 --> 00:05:28,420
Coming back to the question I asked you, what do you think this number would be?
84
00:05:28,900 --> 00:05:33,900
Let's go ahead and comment out all the other code and run this line of code and
85
00:05:34,840 --> 00:05:38,890
it will give us 7.0. Now if math is not your strong point, don't worry,
86
00:05:38,890 --> 00:05:42,130
it's not mine either and you're the sort of person who would prefer to see it
87
00:05:42,130 --> 00:05:46,180
visualize. Then I recommend again putting this line of code in Thonny,
88
00:05:46,660 --> 00:05:51,220
and then go ahead and clicking on the debugging symbol and then just step into,
89
00:05:51,220 --> 00:05:56,220
so press F7 or this button multiple times and you'll see it evaluate
90
00:05:56,290 --> 00:05:57,680
this line of code step-by-step.
91
00:05:57,830 --> 00:06:01,520
So first it looks at the entire thing and then it goes from left to right.
92
00:06:02,120 --> 00:06:07,120
And the first calculation is 3 * 3 and that is the one that's going
93
00:06:07,130 --> 00:06:09,260
to execute first and it becomes 9.
94
00:06:09,950 --> 00:06:13,670
Next it's going to look along this line of code and see that the next most
95
00:06:13,670 --> 00:06:17,120
important thing is this division here, 3 divided by 3.
96
00:06:17,660 --> 00:06:20,690
So it's going to carry out that next and that becomes 1.
97
00:06:21,200 --> 00:06:26,200
So now 9 + 1 is going to be the next thing because it's the most to the
98
00:06:26,330 --> 00:06:31,330
left and then it becomes 10 - 3 and we finally get the result of 7.
99
00:06:32,630 --> 00:06:34,700
So now here's another challenge for you.
100
00:06:35,240 --> 00:06:39,980
How can you change this code so that instead of getting 7,
101
00:06:40,340 --> 00:06:41,570
we get 3?
102
00:06:41,990 --> 00:06:46,170
How can you change this line of code given what you know about PEMDASLR?
103
00:06:46,170 --> 00:06:49,520
See if you can figure it out. Pause the video now.
104
00:06:54,110 --> 00:06:57,290
Alright, so this will involve a little bit of trial and error.
105
00:06:57,890 --> 00:07:02,890
And the most important tool we have access to is the parentheses or the
106
00:07:04,400 --> 00:07:08,600
brackets. This means that we can actually isolate bits of our code,
107
00:07:08,630 --> 00:07:12,800
which normally have very low priority and turn them into higher priority
108
00:07:12,800 --> 00:07:15,020
operations. So in this case,
109
00:07:15,050 --> 00:07:17,810
we know that 3 * 3 is going to happen first,
110
00:07:18,170 --> 00:07:20,930
and then the multiplication, and then the addition on the left,
111
00:07:20,990 --> 00:07:22,310
and then the subtraction.
112
00:07:22,940 --> 00:07:27,940
But if we added a set of parentheses around our 3 + 3,
113
00:07:29,450 --> 00:07:31,760
then out of all of these calculations,
114
00:07:32,180 --> 00:07:36,440
this particular one suddenly becomes the highest priority and it will happen
115
00:07:36,440 --> 00:07:37,273
first.
116
00:07:37,640 --> 00:07:42,640
So if I change what I've got in Thonny to our new version and go ahead and debug
117
00:07:43,100 --> 00:07:43,820
through it,
118
00:07:43,820 --> 00:07:48,020
the very first calculation it's going to perform is this 3 + 3 inside
119
00:07:48,020 --> 00:07:50,720
the brackets and we end up with 6.
120
00:07:51,170 --> 00:07:53,690
So then it's going to go again from left to right,
121
00:07:53,690 --> 00:07:55,910
prioritizing multiplication and division.
122
00:07:56,240 --> 00:08:01,240
So then it's 3 * 6 is 18. 18 / 3 is 6. 6 - 3 is
123
00:08:02,630 --> 00:08:06,500
3. Just by isolating certain calculations,
124
00:08:06,890 --> 00:08:11,890
you can elevate it to right at the top of the priority list and you will be able
125
00:08:11,900 --> 00:08:14,300
to perform the calculation that you need.
126
00:08:15,530 --> 00:08:19,250
Now that you've learned a lot about calculations and performing mathematical
127
00:08:19,250 --> 00:08:23,060
operations using Python, I've got another code challenge for you.
128
00:08:23,180 --> 00:08:26,060
So head up to the next lesson and you'll be able to discover it there.
11652
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.