Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
In the previous lesson we saw how we could use the plus sign either to
2
concatenate strings together or as a mathematical operation where we add two
3
integers or two floats, basically two numbers together.
4
In this lesson I want to show you some of the other mathematical operators that
5
you have access to
6
in addition to adding. The next obvious one is subtraction.
7
So 7 - 3 , you would just use the minus sign,
8
but then when you get to multiplication it's a little bit weird. Um,
9
instead of using the x or some sort of time symbol,
10
you actually use the asterix.
11
So you would say something like 3 * 2 and that would be 3
12
times 2.
13
Now the final one is division and that's done using the forward-slash, so 6
14
/ 3 and when that prints out,
15
it'll give you 2.
16
Now one thing to notice here is that whenever you're dividing things,
17
you actually always end up with a floating point number.
18
So you can see even though six divides into three cleanly,
19
we're still getting 2.0. And in fact if I put a type check around this division,
20
you'll see that it will print float instead of integer.
21
And this is just something that happens with division in Python.
22
But at the end of the day, you get the result that you need and it doesn't really
23
matter whether if it's a float or if it's a integer.
24
Now the last one that's really useful is two asterisk signs and this gives you
25
access to the exponents or when you want to raise the number to a power.
26
So for example,
27
if you wanted to get a hold of 2 to the power of 2, then you would write it
28
like this and 2 to the power of 2 is of course basically just 2 times 2
29
which is going to be equal to 4.
30
Now if I change this to 2 to the power of 3,
31
then that's going to be 2 times 2 times 2. So that should be 8 and so
32
on and so forth.
33
Having the exponent being built into the language is one of the reasons why
34
Python is really loved by a lot of data scientists and mathematicians because
35
it's really optimized towards manipulating and handling numbers. Now,
36
one of the things that you have to be careful about when you are doing these
37
mathematical operations is when you have more than one operation on the same
38
line of code, then there's a certain level of priority.
39
So some of these operations like division or multiplication are going to be
40
first class,
41
whereas other ones are going to be more economy like the plus and minus.
42
And the rule that you might've remember from high school is something called
43
PEMDAS.
44
It basically states the order of priority is parentheses,
45
exponents, multiplication, division, addition and subtraction.
46
So the things that happen first are the things inside brackets, then it's our exponents,
47
then it's our multiplication and division.
48
And finally, the lowest priority is our addition and subtraction.
49
Now it's a little bit deceiving because of this order.
50
It makes it seem like as if multiplication happens before division,
51
but actually they are equally important.
52
And when it actually comes to your calculations,
53
the calculation that's most to the left is the one that will be prioritized
54
between multiplication and division.
55
So let me give you a real-life example to make this more clear.
56
Let's say we had a line of code where we wanted to multiply 3 * 3 +
57
3 / 3 - 3.
58
If I was to execute this entire line of code and print it out into the console,
59
here's the time where you play computer again and guess using what you've
60
learned here, um,
61
what exactly will be printed because you will get a number printed,
62
it will calculate this entire line of code for you. But the order matters.
63
Is it going to first add 3 to 3 then multiply the result by 3 or is
64
it first going to divide through by 3 and then add 3 to it?
65
What is the order?
66
And I want you to really pause the video and have a little play with it on pen
67
and paper before you come back and we'll show you the result.
68
Using our rule, PEMDAS,
69
we can see the first thing that happens is what's in the parentheses,
70
so that doesn't matter. The next thing, our exponents,
71
we don't actually have an exponent here.
72
We don't have 2 to the power of 3 or something to the power of anything.
73
So we can ignore that as well.
74
So the next level is the multiplication and division.
75
And as I mentioned, they are all equal importance.
76
So this 3 * 3 and 3 / 3 are both equally important,
77
but the calculation goes from left to right.
78
So the first thing we see is actually the multiplication. So if it helps you,
79
you might want to add LR to the end of this mnemonic.
80
So it becomes PEMDASLR, or at least that's the way I would think about it.
81
So even if this was division and this was multiplication,
82
this calculation will always get executed first.
83
Coming back to the question I asked you, what do you think this number would be?
84
Let's go ahead and comment out all the other code and run this line of code and
85
it will give us 7.0. Now if math is not your strong point, don't worry,
86
it's not mine either and you're the sort of person who would prefer to see it
87
visualize. Then I recommend again putting this line of code in Thonny,
88
and then go ahead and clicking on the debugging symbol and then just step into,
89
so press F7 or this button multiple times and you'll see it evaluate
90
this line of code step-by-step.
91
So first it looks at the entire thing and then it goes from left to right.
92
And the first calculation is 3 * 3 and that is the one that's going
93
to execute first and it becomes 9.
94
Next it's going to look along this line of code and see that the next most
95
important thing is this division here, 3 divided by 3.
96
So it's going to carry out that next and that becomes 1.
97
So now 9 + 1 is going to be the next thing because it's the most to the
98
left and then it becomes 10 - 3 and we finally get the result of 7.
99
So now here's another challenge for you.
100
How can you change this code so that instead of getting 7,
101
we get 3?
102
How can you change this line of code given what you know about PEMDASLR?
103
See if you can figure it out. Pause the video now.
104
Alright, so this will involve a little bit of trial and error.
105
And the most important tool we have access to is the parentheses or the
106
brackets. This means that we can actually isolate bits of our code,
107
which normally have very low priority and turn them into higher priority
108
operations. So in this case,
109
we know that 3 * 3 is going to happen first,
110
and then the multiplication, and then the addition on the left,
111
and then the subtraction.
112
But if we added a set of parentheses around our 3 + 3,
113
then out of all of these calculations,
114
this particular one suddenly becomes the highest priority and it will happen
115
first.
116
So if I change what I've got in Thonny to our new version and go ahead and debug
117
through it,
118
the very first calculation it's going to perform is this 3 + 3 inside
119
the brackets and we end up with 6.
120
So then it's going to go again from left to right,
121
prioritizing multiplication and division.
122
So then it's 3 * 6 is 18. 18 / 3 is 6. 6 - 3 is
123
3. Just by isolating certain calculations,
124
you can elevate it to right at the top of the priority list and you will be able
125
to perform the calculation that you need.
126
Now that you've learned a lot about calculations and performing mathematical
127
operations using Python, I've got another code challenge for you.
128
So head up to the next lesson and you'll be able to discover it there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.