Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,530 --> 00:00:03,000
Welcome back.
2
00:00:03,000 --> 00:00:06,470
One type of expression that you make right
in Python is what's called the function
3
00:00:06,470 --> 00:00:07,970
call expression.
4
00:00:07,970 --> 00:00:10,880
So you can think of
functions as boxes that
5
00:00:10,880 --> 00:00:13,460
contain some machinery inside of them.
6
00:00:13,460 --> 00:00:18,390
So I'm going to visually represent
them here just using this gray box.
7
00:00:18,390 --> 00:00:21,830
And you can think of a function as
something that takes in some inputs.
8
00:00:21,830 --> 00:00:24,890
So here,
the inputs feed into the top here and
9
00:00:24,890 --> 00:00:28,680
then these inputs go
inside of this machinery.
10
00:00:28,680 --> 00:00:31,280
This machinery does something
with the inputs, and
11
00:00:31,280 --> 00:00:37,090
then after it's done its work with
the inputs, it spits out an output.
12
00:00:37,090 --> 00:00:41,040
So visually again, you can think of
it as working something like this.
13
00:00:42,190 --> 00:00:45,946
So here we have a function,
it takes in three inputs or arguments and
14
00:00:45,946 --> 00:00:51,800
then it does work, and then it spits
out an output or return value.
15
00:00:51,800 --> 00:00:53,260
So let's watch that again.
16
00:00:53,260 --> 00:00:58,240
So again, we have the function, and it
takes in three inputs here or arguments,
17
00:00:58,240 --> 00:01:03,040
then it does work, and then it spits
out an output or a return value.
18
00:01:04,190 --> 00:01:08,270
Now, let's translate this visual metaphor
into actual working Python code.
19
00:01:09,850 --> 00:01:13,130
Here's an example of how we would actually
write a function call expression in
20
00:01:13,130 --> 00:01:14,200
Python.
21
00:01:14,200 --> 00:01:17,180
Suppose that we have
a function named square and
22
00:01:17,180 --> 00:01:22,340
our square function takes in one
argument which is going to be a number.
23
00:01:22,340 --> 00:01:27,860
And then it returns, or outputs,
whatever that argument is squared.
24
00:01:27,860 --> 00:01:32,900
So for example, if we say square and
then open parenthesis and
25
00:01:32,900 --> 00:01:36,290
we put our argument in between
the parentheses here, so
26
00:01:36,290 --> 00:01:41,890
this is called calling the square
function with 4 as an argument.
27
00:01:41,890 --> 00:01:46,010
So this is how we actually write that
in Python again, we say square, or
28
00:01:46,010 --> 00:01:48,068
whatever the name of the function is.
29
00:01:48,068 --> 00:01:51,990
Then inside of parentheses immediately
after the name of the function,
30
00:01:51,990 --> 00:01:55,010
we pass in our arguments.
31
00:01:55,010 --> 00:01:57,306
So the arguments go here.
32
00:02:01,590 --> 00:02:05,062
And then the value of this
overall expression, so
33
00:02:05,062 --> 00:02:10,280
whatever the value of this expression is,
is going to be the return value.
34
00:02:15,239 --> 00:02:19,230
And so, in the case of square,
if square squares whatever input or
35
00:02:19,230 --> 00:02:23,174
arguments you give it,
then the return value is going to be 16.
36
00:02:24,330 --> 00:02:27,280
And this is what it looks like
when we translate this Python
37
00:02:27,280 --> 00:02:30,470
code into that visual representation
that I just mentioned.
38
00:02:30,470 --> 00:02:35,310
So we have the square function,
again represented as a box, and the square
39
00:02:35,310 --> 00:02:40,310
function in this case we suppose that
it takes in one input or argument.
40
00:02:40,310 --> 00:02:44,430
And if we pass in 4, it does some work,
and it spits out an output or
41
00:02:44,430 --> 00:02:45,560
return value of 16.
42
00:02:45,560 --> 00:02:50,740
To watch that again, we pass in 4,
the square function does some work,
43
00:02:50,740 --> 00:02:55,360
and it computes an output or
return value, in this case of 16.
44
00:02:55,360 --> 00:02:58,420
And again,
this is how we write that in Python code.
45
00:02:58,420 --> 00:03:03,415
We say the name of the function, square,
then open parentheses to say that we
46
00:03:03,415 --> 00:03:07,430
want to call that function and
then in between parentheses,
47
00:03:07,430 --> 00:03:10,790
then we pass in whatever arguments
we want our functions to take.
48
00:03:11,790 --> 00:03:16,930
Now, Python functions can take one,
two or any number of arguments or
49
00:03:16,930 --> 00:03:21,210
inputs, but
there's always exactly one output.
50
00:03:21,210 --> 00:03:26,070
So, for example, here this function
takes in three inputs, or arguments, but
51
00:03:26,070 --> 00:03:28,810
it spits out one return value.
52
00:03:28,810 --> 00:03:32,890
So again, functions always take
any number of arguments, but
53
00:03:32,890 --> 00:03:36,780
there's always one value
of that function call.
54
00:03:36,780 --> 00:03:41,060
So, let's look at a little bit more
Python code and calling functions.
55
00:03:41,060 --> 00:03:46,003
So, here on the first line we're actually
calling two functions technically because
56
00:03:46,003 --> 00:03:48,847
here print is the function
that we're calling and
57
00:03:48,847 --> 00:03:52,841
we're passing in as an argument to print,
an expression which just so
58
00:03:52,841 --> 00:03:55,027
happens to be another function call.
59
00:03:55,027 --> 00:03:58,788
And this function call uses the square
function that I just described.
60
00:03:58,788 --> 00:04:02,080
So, again,
the square function that I am describing,
61
00:04:02,080 --> 00:04:04,340
this isn't a built-in with Python.
62
00:04:04,340 --> 00:04:08,630
This is something that we defined for the
purpose of the explaining function calls.
63
00:04:08,630 --> 00:04:12,050
But we defined it in a way
that it takes in one argument,
64
00:04:12,050 --> 00:04:16,800
in this case square(3), and
then it spits out that number squared.
65
00:04:16,800 --> 00:04:20,140
So if we say,
print out the value of square(3),
66
00:04:20,140 --> 00:04:23,770
then this is going to
print out the integer 9.
67
00:04:23,770 --> 00:04:26,400
So let's see that in action.
68
00:04:26,400 --> 00:04:31,750
So I'm going to comment out the rest
of these lines and run my code.
69
00:04:31,750 --> 00:04:37,073
And I can see that the value of
this expression, square(3) is 9.
70
00:04:40,171 --> 00:04:45,194
If we call a function, but we don't
have a print statement, so let's suppose
71
00:04:45,194 --> 00:04:50,030
that I say square(5), the value of
this expression is going to be 10.
72
00:04:50,030 --> 00:04:51,260
But you'll notice when I save and
73
00:04:51,260 --> 00:04:54,020
run my code, it's not going to
actually affect the output.
74
00:04:54,020 --> 00:04:58,510
And that's because we never actually
printed the value of square(5).
75
00:04:58,510 --> 00:05:01,040
We just computed the value of square(5).
76
00:05:01,040 --> 00:05:03,890
Excuse me, it's not 10, it's 25 of course.
77
00:05:03,890 --> 00:05:08,725
So here the value of this is 25 but
we never print out its value.
78
00:05:08,725 --> 00:05:13,970
Now, let's suppose that we have
another function, called sub.
79
00:05:13,970 --> 00:05:18,730
So sub takes in two arguments.
80
00:05:18,730 --> 00:05:20,766
So I can represent that
visually like this.
81
00:05:23,505 --> 00:05:28,560
So it takes in one and two arguments.
82
00:05:29,880 --> 00:05:34,775
And just like every other function,
it's going to spit out one returned value.
83
00:05:38,067 --> 00:05:43,730
Now, let's suppose that sub subtracts the
second argument from the first argument.
84
00:05:43,730 --> 00:05:47,117
So, in other words,
suppose if I passed in 6,
85
00:05:50,319 --> 00:05:53,608
If I passed in 6 and 4,
86
00:05:55,066 --> 00:05:59,836
Then my sub function is going to spit
87
00:05:59,836 --> 00:06:04,130
out 2 because 6 minus 4 is 2.
88
00:06:04,130 --> 00:06:09,550
So we would call the sub function
by saying print out and then sub.
89
00:06:09,550 --> 00:06:12,950
And then here you'll notice
that we have two arguments and
90
00:06:12,950 --> 00:06:15,530
we separate those arguments by a comma.
91
00:06:15,530 --> 00:06:19,211
So you print out the value of sub(6, 4).
92
00:06:19,211 --> 00:06:25,114
And we see that the value of
this expression, sub(6, 4) is 2.
93
00:06:25,114 --> 00:06:30,836
If we call it again, so
if we call sub with arguments 5 and
94
00:06:30,836 --> 00:06:37,054
9, then this is going to give
us -4 because 5- 9 is -4.
95
00:06:37,054 --> 00:06:43,680
We can also combine function calls with
other operators including function calls.
96
00:06:43,680 --> 00:06:47,769
So here if we print out
the value of square(3) + 2.
97
00:06:47,769 --> 00:06:53,515
Then Python is going to first compute
the value of this overall expression by
98
00:06:53,515 --> 00:07:00,270
evaluating the value of square(3) and then
it's going to add the result of that to 2.
99
00:07:00,270 --> 00:07:03,830
So Python is going to compute
the value of square(3) and
100
00:07:03,830 --> 00:07:08,240
it's going to get 9 and then it's going
to compute the value of this expression,
101
00:07:08,240 --> 00:07:11,460
which has a value, 2,
because it's a literal expression.
102
00:07:11,460 --> 00:07:15,876
And then it's going to add these
numbers together to give us 11.
103
00:07:15,876 --> 00:07:22,940
Now this expression on line
2 is much more complicated.
104
00:07:22,940 --> 00:07:26,580
So here we can see that we're
calling the sub function.
105
00:07:26,580 --> 00:07:28,770
We're calling it with two arguments.
106
00:07:28,770 --> 00:07:34,408
So the first argument is square(3),
so this is arg 1,
107
00:07:34,408 --> 00:07:40,404
the second argument is square(1 + 1),
so this is arg 2.
108
00:07:41,460 --> 00:07:44,860
Now, whenever Python calls
a function call expression,
109
00:07:44,860 --> 00:07:48,520
it needs to figure out what
the values of the arguments are.
110
00:07:48,520 --> 00:07:52,220
So Python kind of computes the values
of a function call expression
111
00:07:52,220 --> 00:07:53,820
from the inside out.
112
00:07:53,820 --> 00:07:57,974
So it's going to go from
the value of 1 + 1.
113
00:07:59,190 --> 00:08:04,180
To the value of square(1+1) and
then the value of square(3), and
114
00:08:04,180 --> 00:08:08,084
then after it has the values for
square(3) and square(1+1),
115
00:08:08,084 --> 00:08:12,930
it's going to subtract those two
values by calling the sub function.
116
00:08:12,930 --> 00:08:18,656
So again, we're kind of going from
the inside out to the next layer.
117
00:08:20,958 --> 00:08:23,404
And then out to the next
layer beyond that.
118
00:08:26,093 --> 00:08:28,126
So when Python computes this,
119
00:08:28,126 --> 00:08:32,670
it's going to first compute
the value of square(1+1), or 2.
120
00:08:32,670 --> 00:08:37,990
So it's going to compute the value of
square(2), and that's going to give us 4.
121
00:08:37,990 --> 00:08:41,560
It's going to compute the value of
square(3), and that's going to give us 9.
122
00:08:41,560 --> 00:08:46,401
And then it's going to compute
the value of sub when called on 9 and
123
00:08:46,401 --> 00:08:49,092
4, and that's going to give us 5.
124
00:08:49,092 --> 00:08:53,867
So if I run my code,
you'll see that we get
125
00:08:53,867 --> 00:08:58,104
11 from line 1 and 5 from line 2.
126
00:08:59,260 --> 00:09:03,840
Just to run through the exact steps that
Python takes when computing the code on
127
00:09:03,840 --> 00:09:08,520
line 2, Python first computes,
what's the value of square(3)?
128
00:09:08,520 --> 00:09:12,730
It gets 9, then it computes what's 1 + 1,
and it gets 2.
129
00:09:13,800 --> 00:09:17,740
Then it computes what's square(2),
and it gets 4..
130
00:09:17,740 --> 00:09:23,350
Then it subtracts 9 and 4 and gets 5 and
that's what it ultimately prints out.
131
00:09:23,350 --> 00:09:28,670
Now, one thing to note is that
functions are objects in Python.
132
00:09:28,670 --> 00:09:31,530
So if I print out what's
the value of square,
133
00:09:31,530 --> 00:09:35,330
then Python is going to tell
me that square is a function.
134
00:09:35,330 --> 00:09:40,058
So if I run my code, you can see that
line 1 prints out function square.
135
00:09:43,214 --> 00:09:47,093
In order to actually call the function or
run that machinery,
136
00:09:47,093 --> 00:09:51,493
we need to have the name of the function,
and then open parenthesis and
137
00:09:51,493 --> 00:09:54,788
pass in our arguments inside
of those parentheses.
138
00:09:54,788 --> 00:09:58,508
So in other words, we need to have open
parentheses right after the name of
139
00:09:58,508 --> 00:10:01,628
the function in order to say that
we want to call the function,
140
00:10:01,628 --> 00:10:05,780
we don't want to just reference
that function object itself.
141
00:10:05,780 --> 00:10:07,440
That's all for now, until next time.12933
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.