WEBVTT

0
00:00.140 --> 00:01.040
To begin,

1
00:01.040 --> 00:05.600
the first thing I want to talk about is more on functions.

2
00:05.600 --> 00:09.290
So we've already seen a couple of things that we can do with functions.

3
00:09.320 --> 00:15.860
The first thing I showed you was how to create a very simple bog-standard function using the def, the

4
00:15.860 --> 00:22.310
name, the empty parentheses, and then the colon, and then within the body of the function, you can

5
00:22.310 --> 00:30.200
do a bunch of things which will be carried out every single time you trigger, or call the function like

6
00:30.200 --> 00:30.830
this.

7
00:30.860 --> 00:36.500
And the first type of function basically just helps you reduce the amount of code you have to write

8
00:36.500 --> 00:39.950
when you have instructions that you want to execute repeatedly.

9
00:40.460 --> 00:45.590
Now the next type of function we saw actually had something inside the parentheses.

10
00:45.590 --> 00:52.190
And this something is an input which can be passed over when we call the function.

11
00:52.220 --> 00:59.420
So in this case 123 is the argument which gets passed in to this parameter called something,

12
00:59.420 --> 01:02.760
and then it gets used within the body of the function.

13
01:02.790 --> 01:06.630
The second type of function, the functions which allow for inputs,

14
01:06.660 --> 01:13.260
gives us the ability to modify the code in the function, and to get it to do something different each

15
01:13.260 --> 01:16.710
time, depending on what input was passed in.

16
01:16.740 --> 01:20.910
We're now onto the final sort of flavor of functions,

17
01:20.910 --> 01:28.500
and these are functions which allow you to have an output once the function is completed.

18
01:28.530 --> 01:31.500
We start off with a simple function,

19
01:31.500 --> 01:36.930
and in this case, you'll notice that even though I could, I'm actually not giving it any inputs, the

20
01:36.930 --> 01:40.080
inputs and the outputs are completely separate from each other.

21
01:40.080 --> 01:43.680
So this is a normal function that you've seen many times now.

22
01:43.680 --> 01:49.230
And within the body of the function I'm going to do some piece of calculation.

23
01:49.230 --> 01:55.050
Let's say I'm going to calculate what is 3 * 2, and then save it to a variable called result.

24
01:55.080 --> 02:03.570
Now once that's done I can use the output keyword, which is return, and then after that keyword I

25
02:03.570 --> 02:08.340
can put whatever it is I want to be the output of this function.

26
02:08.340 --> 02:11.730
So in this case the output is the result.

27
02:11.730 --> 02:21.270
And what that means is when I call this function later on and it runs, then it will go ahead and output

28
02:21.270 --> 02:22.530
this result,

29
02:22.530 --> 02:27.930
and it replaces this line of code where this function was called.

30
02:27.960 --> 02:32.760
Now that result is held in the code where the function was called,

31
02:32.760 --> 02:36.360
and I could save it to another variable.

32
02:36.390 --> 02:39.330
Result of course equals, 3 * 2,

33
02:39.330 --> 02:42.990
and that means this output now equals 6.

34
02:42.990 --> 02:45.360
So this is what the code might look like.

35
02:45.360 --> 02:52.440
Somewhere I would define my function which has an output which you can tell by the 'return' keyword.

36
02:52.470 --> 02:59.710
Now at some other point in my code, I decide to call that function, and once that function executes,

37
02:59.710 --> 03:07.360
that return or whatever is outputted replaces the function call, and at the very end, it means this

38
03:07.360 --> 03:11.260
variable output will store whatever the output is.

39
03:11.260 --> 03:14.920
So in this case, output would be equal to six.

40
03:15.400 --> 03:21.100
The way that I like to think about functions with outputs is almost kind of like a machine, right?

41
03:21.130 --> 03:28.840
If you had a machine where in goes some empty bottles and after some processing or some inputs, out

42
03:28.840 --> 03:31.480
comes a bottle filled with milk.

43
03:31.480 --> 03:36.550
So in this case this function has an input the empty glass bottle,

44
03:36.550 --> 03:40.240
it has an output, the glass bottle filled with milk,

45
03:40.270 --> 03:47.050
and in the middle there's some processes or some code that's being executed to create this change.

46
03:47.230 --> 03:50.920
This ties together all the things that we've learned about functions.

47
03:50.920 --> 03:54.520
So now head over to the starting file for today's lessons.

48
03:54.520 --> 04:00.920
Let's start off by creating a new function, which I'm going to call format_name.

49
04:00.920 --> 04:09.290
And the reason is because this function is going to take a first name and a last name, and it's going

50
04:09.290 --> 04:16.580
to change those inputs, even if it was lowercase or if it was all caps, it's going to change it into

51
04:16.610 --> 04:23.210
title case, something like this, where the first letter is capitalized in each word.

52
04:23.900 --> 04:30.890
To begin, I want you to create this function and then go ahead and add two inputs to it, one called

53
04:30.920 --> 04:31.640
f_name,

54
04:31.640 --> 04:37.010
so first name, and another called l_name, which is the last name.

55
04:37.040 --> 04:38.840
Pause the video and give that a go.

56
04:40.700 --> 04:41.270
All right.

57
04:41.270 --> 04:48.590
So all we have to do to add inputs to our function is just to put the names of these parameters inside

58
04:48.590 --> 04:49.970
the parentheses.

59
04:49.970 --> 04:57.620
So now our function is able to take these parameters as inputs whenever this function gets called.

60
04:58.250 --> 05:03.650
The next step is we're going to convert these strings that get passed in,

61
05:03.650 --> 05:07.100
so it could be something like this all lowercase,

62
05:07.100 --> 05:11.210
it could be all capitalized, or it could be kind of random, right?

63
05:11.240 --> 05:14.240
Like some are capitalized, some are not.

64
05:14.270 --> 05:17.090
But we're going to make it all uniform.

65
05:17.090 --> 05:20.210
So all of these are going to end up like this.

66
05:20.210 --> 05:24.380
And to do this we can search around in Google,

67
05:24.380 --> 05:28.730
and you might come across a similar question in StackOverflow.

68
05:28.760 --> 05:32.810
"How do you convert a string to Title Case in Python?"

69
05:32.840 --> 05:34.250
They have an example,

70
05:34.250 --> 05:38.030
and it's trying to do exactly what we're trying to do.

71
05:38.060 --> 05:44.540
If we take a look at the answers, you can see that somebody is helpfully pointing to this title()

72
05:44.540 --> 05:45.290
function.

73
05:45.290 --> 05:52.040
So you take a string and then you write .title(), and then afterwards the string gets transformed

74
05:52.040 --> 05:56.480
and every word now starts off with a capital letter.

75
05:57.140 --> 06:03.290
And if you want to know more about how this title case works, then you can click on that link in title

76
06:03.290 --> 06:06.560
and then have a read through the Python documentation.

77
06:07.490 --> 06:15.530
Once you've read through that, go ahead and modify this f_name and l_name, so that they end up being

78
06:15.530 --> 06:17.510
both in title case.

79
06:17.540 --> 06:19.520
Pause the video and give that a go.

80
06:22.010 --> 06:22.640
All right.

81
06:22.640 --> 06:25.460
So we want to go ahead and take this f_name,

82
06:25.460 --> 06:26.960
and then write a dot,

83
06:26.960 --> 06:31.820
and then write title (.title), to convert it to title case.

84
06:31.850 --> 06:36.770
Once we've done that then we're going to do the same with the l_name.

85
06:37.700 --> 06:41.690
And if we go ahead and actually print these out...

86
06:42.620 --> 06:47.270
And of course in order for the function to actually execute, we'll have to call the function.

87
06:47.270 --> 06:49.940
So I'm going to put two inputs in here,

88
06:49.940 --> 06:54.960
one which is going to be just angela, all in lowercase,

89
06:54.990 --> 06:58.350
and then the second one is going to be ANGELA,

90
06:58.380 --> 06:59.670
all in uppercase.

91
06:59.700 --> 07:06.690
And now when I run this code, you'll see that the result is they both get converted to title case.

92
07:06.810 --> 07:14.430
In essence, this part of the code becomes whatever the previous value of f_name is, converted to title case.

93
07:15.000 --> 07:21.300
You might have spotted already that this title() function actually has an output.

94
07:21.330 --> 07:29.220
It returns a version of the string where each word is title cased. Instead of simply just printing

95
07:29.220 --> 07:29.670
this,

96
07:29.700 --> 07:33.060
we can also capture it inside a variable.

97
07:33.090 --> 07:45.660
Let's create a new variable called formatted f_name, and also formatted l_name.

98
07:46.350 --> 07:56.160
Now what's happening here is we are transforming this input f_name, turning it into title case,

99
07:56.160 --> 08:04.500
and then once this function executes, it has an output, and that output replaces this part of the

100
08:04.500 --> 08:05.160
code.

101
08:05.160 --> 08:08.970
And then it gets stored inside this variable.

102
08:09.330 --> 08:15.690
Now we can go ahead and print the final version of our function, which is going to use an f-string

103
08:15.720 --> 08:26.820
to take the formatted f_name, and then add a space, and then print out the formatted l_name like this.

104
08:26.880 --> 08:35.850
Now if I start out with my first name, but all in strange sort of casing, and then my last name in

105
08:35.850 --> 08:39.720
all caps, let's go ahead and hit Run and see what happens.

106
08:39.750 --> 08:45.540
You can see that my first name gets converted to title case, my last name gets converted to title case,

107
08:45.540 --> 08:49.260
and then that result is stored in formatted f_name.

108
08:49.260 --> 08:56.590
And this result becomes stored in this l_name, and then they get printed and formatted like so.

109
08:57.430 --> 09:05.920
Now instead of printing this result out, we could also return it as well, in the same way that this

110
09:05.920 --> 09:11.290
title function returns, the output replaces this part of the code,

111
09:11.290 --> 09:15.310
we can also do the same with our format_name() function.

112
09:15.310 --> 09:21.340
So let's delete the print function and go ahead and use return instead.

113
09:22.000 --> 09:26.050
Now this formatted string becomes the output.

114
09:26.050 --> 09:34.270
And as we mentioned before what happens with functions with outputs is this output replaces the part

115
09:34.270 --> 09:37.270
of the code where the function was called.

116
09:37.300 --> 09:45.820
Now if this is the string that we want to print, then all we have to do is just to save the output

117
09:45.820 --> 09:51.140
which replaces this part of the code inside a variable like this.

118
09:51.140 --> 09:57.350
And then all we have to do is just go ahead and print that formatted_string.

119
09:57.620 --> 10:01.430
As you can see, when I run the code, the same thing happens.

120
10:01.430 --> 10:09.350
Now you can and you might have alternatively just taken this function call and put it straight inside

121
10:09.350 --> 10:10.430
the print statement.

122
10:10.430 --> 10:12.860
That works exactly the same way,

123
10:12.860 --> 10:15.980
and we can visualize this using Thonny.

124
10:16.010 --> 10:26.000
If I go ahead and paste everything we have inside the editor and go ahead and run Debug, then we can

125
10:26.000 --> 10:29.840
step into and see what are the steps that happen.

126
10:29.840 --> 10:37.730
So the first thing when this Line 6 gets executed, is it tries to call this function format_name().

127
10:37.730 --> 10:40.880
So it passes in these two inputs.

128
10:41.510 --> 10:48.890
And we're now within the function where we have access to these two inputs f_name and l_name.

129
10:48.980 --> 10:55.520
Now the formatted_f_name becomes the title case version of the first name, and the same thing happens

130
10:55.520 --> 10:56.720
with the last name.

131
10:56.720 --> 11:03.830
So we now have a formatted_f_name and a formatted_l_name, and that is what's going to be returned as

132
11:03.830 --> 11:04.940
the output.

133
11:05.840 --> 11:13.190
Now, once that's completed, that output replaces whatever was in between the print statement, which

134
11:13.190 --> 11:15.680
is where we called this format_name() function.

135
11:15.680 --> 11:18.890
And this is what gets printed into the console.

136
11:19.100 --> 11:25.130
As I mentioned before, we've actually already been using functions that we didn't create that have

137
11:25.130 --> 11:27.140
all of these functionalities, right?

138
11:27.140 --> 11:32.330
For example, the len() function takes an input and has an output.

139
11:32.330 --> 11:35.450
The input is going to be a string,

140
11:35.450 --> 11:39.320
my name again, I seem to be really obsessed with my own name.

141
11:39.980 --> 11:45.230
So now this line of code is going to run this built-in len() function,

142
11:45.230 --> 11:51.120
calculating the number of letters inside this input that we've given it, and then it's going to return

143
11:51.120 --> 11:56.430
the result as an output, which we can now capture inside a variable.

144
11:56.610 --> 11:58.530
This is the input.

145
11:58.530 --> 12:00.480
This is the function.

146
12:00.480 --> 12:09.390
And once this is completed the return or whatever it is after the return statement is going to replace

147
12:09.390 --> 12:11.190
this part of the code,

148
12:11.220 --> 12:16.350
then that result or output then gets stored inside this variable.

149
12:17.160 --> 12:23.370
So this return keyword is really the most important thing in order to create a function that has an

150
12:23.370 --> 12:30.000
output, because everything that comes after it is going to replace where the function was called.

151
12:30.030 --> 12:37.710
Now students often ask me, well, what is the difference then between a print statement and the output?

152
12:37.710 --> 12:43.740
Because it seems like we could use either to get to print to the output area.

153
12:43.890 --> 12:46.800
Well, here is the explanation

154
12:46.800 --> 12:53.730
if you also have this question in mind right now. Let's consider that we create a different function,

155
12:53.730 --> 12:57.330
and this function doesn't actually print its output at all.

156
12:57.330 --> 13:01.140
So let's define a function called function_1(),

157
13:01.140 --> 13:04.410
and it takes some text as input.

158
13:04.410 --> 13:11.940
And all it does is it outputs the original text, but it adds the original text together.

159
13:11.940 --> 13:20.640
So this way if I had the word for example, "hello", then the output would become "hellohello" like an

160
13:20.640 --> 13:21.450
echo.

161
13:21.600 --> 13:27.060
So now if I go ahead and create another function, let's call it function_2(),

162
13:27.060 --> 13:30.090
and it also takes some text as input,

163
13:30.120 --> 13:37.500
well in this case, this function_2() is going to return the text, but it's going to return it in title

164
13:37.500 --> 13:38.280
case.

165
13:38.280 --> 13:42.450
So we've already seen this before and we know how this works.

166
13:42.450 --> 13:44.850
It's going to take whatever input it is here,

167
13:44.850 --> 13:46.800
make the first letter capitalized

168
13:46.840 --> 13:49.300
and the rest of the letters lowercase.

169
13:49.300 --> 13:52.750
So neither of these functions have a print statement.

170
13:52.750 --> 13:57.790
So let's go ahead and call our first function and pass

171
13:57.790 --> 14:00.400
in a word let's say, "hello".

172
14:01.090 --> 14:05.620
Now we can save this to an output variable,

173
14:05.620 --> 14:08.350
and then go ahead and print it.

174
14:08.830 --> 14:13.420
Now you'll see I have that echo thing that I told you about earlier.

175
14:13.450 --> 14:15.430
But now here's the key part,

176
14:15.460 --> 14:26.140
what if, if we take the output of this function_1 and we use it as an input into function_2, how

177
14:26.140 --> 14:27.250
would we do that?

178
14:27.280 --> 14:33.070
Well, we know that the output of function_1, this is in fact a string,

179
14:33.070 --> 14:34.810
and it is a piece of text.

180
14:34.810 --> 14:38.950
So we can then put it into function2 as the input.

181
14:38.950 --> 14:44.230
And we would do it like this function2(), and then brackets,

182
14:44.260 --> 14:50.590
and so now the output of function_1 becomes the input for function_2,

183
14:50.620 --> 14:54.280
and then that input is then title cased.

184
14:54.280 --> 15:01.690
So now if we run our code you'll see that not only do we have the Hellohello functionality being carried

185
15:01.690 --> 15:06.820
out, we've also got the final text capitalized at the beginning.

186
15:07.240 --> 15:09.850
So that's pretty interesting right?

187
15:09.880 --> 15:16.690
We're only able to do this because we understand how the return function works.

188
15:16.690 --> 15:19.990
And we've got two functions that have outputs.

189
15:19.990 --> 15:25.960
And we've effectively chained the output of one function into the input of another one.

190
15:25.990 --> 15:29.440
Now this might mess with your head a little bit, so don't worry.

191
15:30.310 --> 15:35.350
In the next lesson, we'll take a quick look at the return keyword in more detail.

192
15:35.380 --> 15:41.770
Specifically, let's see what happens when we have more than one return statement in the same function.

193
15:41.800 --> 15:44.920
All right, for all of that and more, I'll see you there.