Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:07,940 --> 00:00:12,060
So far we've seen simple expressions like 100,
2
00:00:12,060 --> 00:00:15,120
"Hello world" or 3.14.
3
00:00:15,120 --> 00:00:19,830
These are all literal expressions where the value is the same as the expression itself.
4
00:00:19,830 --> 00:00:22,740
But expressions are like building blocks that can be
5
00:00:22,740 --> 00:00:27,045
combined to form larger and more complicated expressions.
6
00:00:27,045 --> 00:00:30,630
Operators are one way to combine expressions.
7
00:00:30,630 --> 00:00:34,450
The first operator that we'll learn is the addition operator.
8
00:00:34,450 --> 00:00:42,270
In order to use it, we write something like print out 100 plus 200.
9
00:00:42,270 --> 00:00:47,025
When I run this code note that I get the value 300.
10
00:00:47,025 --> 00:00:52,609
So, when we execute a print statement then rather than printing out the expression
11
00:00:52,609 --> 00:00:55,790
itself the expression here being 100 plus
12
00:00:55,790 --> 00:00:59,570
200 then we print out the value of that expression.
13
00:00:59,570 --> 00:01:04,880
So, this expression has
14
00:01:04,880 --> 00:01:15,040
a value of 300.
15
00:01:20,090 --> 00:01:24,245
Now, this expression uses the addition operator.
16
00:01:24,245 --> 00:01:30,590
The addition operator which is done using the plus sign expects one expression to
17
00:01:30,590 --> 00:01:38,070
the left and one expression to the right.
18
00:01:41,230 --> 00:01:45,320
What it does is it computes the value of the expression to
19
00:01:45,320 --> 00:01:50,280
the left and the value of the expression to the right.
20
00:01:50,440 --> 00:01:54,225
It adds up these two values,
21
00:01:54,225 --> 00:01:58,430
and then the value of this overall expression
22
00:01:59,070 --> 00:02:04,585
is the value of this expression plus the value of this expression.
23
00:02:04,585 --> 00:02:07,720
So, in the case of 100 plus 200,
24
00:02:07,720 --> 00:02:12,365
we add the value 100 plus the value 200,
25
00:02:12,365 --> 00:02:17,140
and the value of this overall expression ends up being 300.
26
00:02:17,140 --> 00:02:19,480
Remember that whenever we call print,
27
00:02:19,480 --> 00:02:20,815
we print out the value,
28
00:02:20,815 --> 00:02:22,570
not the expression itself,
29
00:02:22,570 --> 00:02:25,825
and so 300 gets printed in our output.
30
00:02:25,825 --> 00:02:27,790
Beyond the addition operator,
31
00:02:27,790 --> 00:02:31,390
python includes many other kinds of operators.
32
00:02:31,390 --> 00:02:33,860
So, there's subtraction.
33
00:02:35,720 --> 00:02:43,030
So, when I print out the value of five minus two then we get three.
34
00:02:43,550 --> 00:02:50,260
If we reset our code here there's also multiplication.
35
00:02:50,390 --> 00:02:54,670
Multiplication is done using the star symbol.
36
00:02:54,670 --> 00:03:00,215
So, when we print out the value of the expression two times four then we get eight.
37
00:03:00,215 --> 00:03:03,975
There's also division.
38
00:03:03,975 --> 00:03:08,335
So, when I print out ten divided by three,
39
00:03:08,335 --> 00:03:15,435
so division is done using the slash symbol.
40
00:03:15,435 --> 00:03:18,550
When I print out the value of this expression then it's ten
41
00:03:18,550 --> 00:03:22,280
divided by three which is 3.3 repeating.
42
00:03:22,280 --> 00:03:25,970
In addition to addition, multiplication, division,
43
00:03:25,970 --> 00:03:29,929
and subtraction, Python include some other kinds of operators.
44
00:03:29,929 --> 00:03:33,875
So, you'll notice here that when we take ten divided by three,
45
00:03:33,875 --> 00:03:40,205
we had two integers ten and three and we got a float as a result.
46
00:03:40,205 --> 00:03:44,075
Sometimes we don't actually want that remainder.
47
00:03:44,075 --> 00:03:49,765
So Python also includes truncated division which leaves out the remainder,
48
00:03:49,765 --> 00:03:52,035
so that we get three back,
49
00:03:52,035 --> 00:03:55,800
and it also includes the modulo operator.
50
00:03:55,800 --> 00:04:00,520
So, if I say ten and then percent sign three,
51
00:04:00,590 --> 00:04:03,190
this is the modulo operator,
52
00:04:03,190 --> 00:04:08,070
so it gives me the remainder of ten divided by three, which is one.
53
00:04:08,320 --> 00:04:11,750
Python also includes exponentiation.
54
00:04:11,750 --> 00:04:15,935
So, if I print four star star two,
55
00:04:15,935 --> 00:04:22,295
then this expression is like saying four to the power of two or four squared.
56
00:04:22,295 --> 00:04:27,720
So, the value of this expression is four squared which is 16.
57
00:04:28,790 --> 00:04:31,880
When executing code, python follows
58
00:04:31,880 --> 00:04:35,435
the normal order of operations that you might be used to in math.
59
00:04:35,435 --> 00:04:41,850
So, at highest precedence is whatever is in parentheses,
60
00:04:47,960 --> 00:04:52,580
and then below that is exponentiation.
61
00:04:55,610 --> 00:05:01,955
So, that would be something like four to the power of two.
62
00:05:01,955 --> 00:05:06,040
Below that there is multiplication, and division.
63
00:05:14,000 --> 00:05:18,925
So, that would be something like two times four,
64
00:05:18,925 --> 00:05:21,760
and then below that there is addition and
65
00:05:21,760 --> 00:05:32,615
subtraction, two plus three.
66
00:05:32,615 --> 00:05:37,390
So, what this means is that suppose that we wanted to compute
67
00:05:37,390 --> 00:05:42,700
the average of two numbers 10 and 20.
68
00:05:42,770 --> 00:05:47,750
So, if we wanted to compute the average of these two,
69
00:05:49,640 --> 00:05:52,259
which should be 15,
70
00:05:52,259 --> 00:06:00,555
then we might intuitively write something like 10 plus 20 divided by two.
71
00:06:00,555 --> 00:06:02,680
Now if I run this code,
72
00:06:02,680 --> 00:06:06,780
you'll note that I get 20.0 rather than 15,
73
00:06:06,780 --> 00:06:08,475
which is the actual average.
74
00:06:08,475 --> 00:06:15,795
The reason is that Python because division is higher in precedence than addition,
75
00:06:15,795 --> 00:06:20,450
when computing the value of this expression then Python first computes
76
00:06:20,450 --> 00:06:26,210
the value of 20 divided by two and gets 10.0.
77
00:06:26,210 --> 00:06:30,810
Then it adds 10 plus 10.0,
78
00:06:30,810 --> 00:06:34,300
and gets the value 20.0.
79
00:06:34,490 --> 00:06:38,430
If instead we wanted to first add 10 and 20,
80
00:06:38,430 --> 00:06:39,915
and then divide by two,
81
00:06:39,915 --> 00:06:42,855
then we could add parenthesis around 10 and 20.
82
00:06:42,855 --> 00:06:50,415
So, I would say print out 10 plus 20 in parentheses divided by two.
83
00:06:50,415 --> 00:06:55,265
Now what Python is going to do is it's first going to add 10 plus 20
84
00:06:55,265 --> 00:07:00,195
to get 30 and divide by two to get 15.0.
85
00:07:00,195 --> 00:07:08,070
You can see that now we're correctly getting the average of 10 and 20. See you next time.7049
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.