WEBVTT

0
00:00.200 --> 00:04.370
Now, in this lesson, I want to talk to you about Python variables,

1
00:04.370 --> 00:06.980
and it's a really, really useful concept.

2
00:06.980 --> 00:15.140
The first thing I want to do is to take a look at this line of code that you should have already, and

3
00:15.140 --> 00:18.770
when we run this, we should be pretty familiar with what happens, right?

4
00:18.770 --> 00:23.300
It will ask us for our name and when we hit Enter, nothing happens,

5
00:23.300 --> 00:31.760
but behind the scenes, this name or this input has now been received by this function.

6
00:31.760 --> 00:36.830
But once that's done, it kind of just disappears, right?

7
00:36.830 --> 00:41.690
And there's no way for us to be able to refer to it in the future.

8
00:42.320 --> 00:43.580
Or is there?

9
00:45.260 --> 00:48.710
Well, this is where variables come in really handy,

10
00:48.770 --> 00:53.060
if I give the result of this action, a name, well,

11
00:53.060 --> 00:55.700
then I'll be able to refer to it later on.

12
00:55.730 --> 01:02.300
So at the beginning of the line, I'm going to call it name, and then I'm going to add an equal sign,

13
01:02.300 --> 01:10.820
and what this does is it assigns whatever it is that the user typed in as the input to this prompt to

14
01:10.820 --> 01:13.640
a variable called name.

15
01:14.570 --> 01:21.700
So now if I go ahead and run my code again and I enter a name.

16
01:22.210 --> 01:28.630
Now, once I hit Enter, normally there's no way for me to get hold of this anymore, right?

17
01:28.660 --> 01:32.740
But now I can actually go ahead and write

18
01:32.740 --> 01:33.520
print,

19
01:33.520 --> 01:37.360
I'm going to print the value of this variable name.

20
01:37.480 --> 01:42.980
So now if I run my code and I write a name, hit Enter,

21
01:42.980 --> 01:45.500
you'll see that this gets printed.

22
01:45.500 --> 01:55.280
And I can use this at any point in my code just by referring to the name that's attached to this value.

23
01:55.940 --> 02:03.500
So it's almost like we've saved the data from this action to a name.

24
02:03.890 --> 02:10.800
And if you think about it, if you had a phone book, let's say, and you just jotted down the numbers

25
02:10.800 --> 02:17.880
of people, the next time you look at this, there's no way for you to know whose number that is, right?

26
02:18.060 --> 02:21.330
In a sense, it's the same thing with the computer,

27
02:21.330 --> 02:28.410
even though we've inputted this piece of data, there's no way for us to be able to refer to this data

28
02:28.410 --> 02:30.980
unless we give it a name.

29
02:30.980 --> 02:38.390
So in our phone book, we might say that this particular number is associated with the name James.

30
02:38.390 --> 02:43.370
And in programming, we would call this name, james, a variable.

31
02:43.640 --> 02:49.280
So we could write something like James =, and then his phone number.

32
02:49.640 --> 02:56.290
And this means that in the future, if we ever need this piece of data, we can just refer to it by

33
02:56.290 --> 02:57.130
its name,

34
02:57.520 --> 02:59.380
the variable name of james.

35
02:59.950 --> 03:08.470
Now, as the name variable suggests, it's something that can be changed or can be varied.

36
03:08.500 --> 03:15.400
So for example, just because this name is set to something up here doesn't mean that I can't change

37
03:15.400 --> 03:16.450
it later on.

38
03:16.480 --> 03:20.810
So to make things simple, let's go ahead and delete the input() function,

39
03:20.810 --> 03:24.740
and let's just say name is equal to jack. print("name").

40
03:24.740 --> 03:31.520
When I run that, jack is what gets printed inside the console, because I'm now referring to that piece

41
03:31.520 --> 03:34.100
of data by the variable name.

42
03:34.370 --> 03:42.710
But if later on I decide to give this variable a different piece of data to hold on to, let's say,

43
03:42.800 --> 03:50.010
Angela, and I print(name) at this time point after I've changed it,

44
03:50.010 --> 03:53.640
what do you think these two lines will print?

45
03:53.640 --> 03:55.740
What do you think will first be printed?

46
03:55.740 --> 03:57.840
What do you think will be printed second?

47
03:58.350 --> 04:06.390
So even though they're both printing the same variable at this point, when we first call print, it's

48
04:06.390 --> 04:08.480
holding on to the value of jack,

49
04:08.480 --> 04:14.780
but by Line 5, I've now changed it so that it's holding on to the value of Angela.

50
04:16.340 --> 04:20.420
So to practice that, I have a challenge for you.

51
04:20.420 --> 04:29.690
So I want you to be able to figure out how many characters are in the name that the user types in using

52
04:29.690 --> 04:33.420
the input() function. So you already know how the input function works,

53
04:33.420 --> 04:39.630
you already know how to get the user to enter it, and then to get that data back into our code and

54
04:39.630 --> 04:40.410
print it out,

55
04:40.410 --> 04:42.330
but here's the extra step,

56
04:42.330 --> 04:47.700
"Can you calculate how many characters there are in the name that they type in?"

57
04:47.700 --> 04:50.520
For example, if they typed in jack, that would be four.

58
04:50.520 --> 04:53.310
If they typed in Angela that would be six.

59
04:53.310 --> 04:58.880
Now how do we figure out things when we're programming and we're trying to do something that we haven't

60
04:58.880 --> 04:59.720
done before?

61
04:59.750 --> 05:04.910
Well, one of the most important skills as a programmer is to be able to use Google well.

62
05:04.910 --> 05:10.280
It's to be able to find the answers that you need because it's inevitable that you won't remember

63
05:10.280 --> 05:14.390
every single function and you won't know every single function.

64
05:14.390 --> 05:19.070
So we need to read documentation and we need to know how to get hold of it.

65
05:19.070 --> 05:20.570
So very simple,

66
05:20.620 --> 05:26.530
we start off with our query, "how to find the length of a string."

67
05:26.530 --> 05:31.840
And then we add our programming language because this will be different in every programming language.

68
05:31.840 --> 05:34.540
So I normally just add Python to the end of it.

69
05:34.540 --> 05:40.210
And then once we hit Enter, normally we'll get hold of some form of documentation,

70
05:40.210 --> 05:44.080
and the W3Schools documentation for Python is not bad.

71
05:44.080 --> 05:48.110
And you can also browse through all of the other examples.

72
05:48.110 --> 05:52.820
So normally for Python, I stick to W3Schools or StackOverflow,

73
05:52.820 --> 05:55.490
and both of these will give you the right answer.

74
05:55.490 --> 06:02.060
But if we are reading documentation then you can see that normally there is a whole bunch of other things

75
06:02.060 --> 06:05.990
that we can look up, just like a dictionary of Python.

76
06:05.990 --> 06:10.890
Now, as you wouldn't start with a dictionary when you're learning a new language,

77
06:10.890 --> 06:11.940
believe me, I tried that,

78
06:11.940 --> 06:12.900
it did not work,

79
06:12.900 --> 06:19.560
you also wouldn't want to learn through everything in the documentation because it's very dry.

80
06:19.560 --> 06:25.350
And when you don't have a real-life application, when you're not trying to use a particular function

81
06:25.350 --> 06:29.100
or some sort of component, then you also don't remember it very well.

82
06:29.100 --> 06:34.130
So I recommend to find what you need when you need it,

83
06:34.130 --> 06:37.460
and our brain is for thinking and not for storing,

84
06:37.460 --> 06:44.390
because these days, as you know, it's super cheap to buy a single terabyte, storage hard disk

85
06:44.390 --> 06:45.320
online.

86
06:45.320 --> 06:50.210
I think it costs something like 10 or $20 and it's just getting cheaper every day.

87
06:50.210 --> 06:52.850
So don't use your brain to store things,

88
06:52.850 --> 06:53.750
use it to think.

89
06:53.750 --> 07:00.580
But coming back to our documentation here, in order to get the Python string length, we use the len()

90
07:00.580 --> 07:01.180
function.

91
07:01.180 --> 07:04.690
And just like the print() function, it's an inbuilt function in Python,

92
07:04.690 --> 07:07.210
so we can simply just write len(),

93
07:07.210 --> 07:10.780
and then we can use whatever it is we want to measure.

94
07:10.780 --> 07:19.180
For example, in this case it's, "Hello world!" and it will output the number of characters in that string.

95
07:19.240 --> 07:22.220
So now give it a go yourself.

96
07:22.430 --> 07:26.330
So the pause one is the instruction set.

97
07:26.330 --> 07:29.720
And I want you to get user input.

98
07:29.720 --> 07:35.900
I want you to take that user input and calculate the length of the string that the user inputted,

99
07:35.900 --> 07:40.280
and finally to print that number out into the output area.

100
07:40.280 --> 07:44.030
And everything could be done in just one line of code.

101
07:44.030 --> 07:49.570
So try to find the documentation, try to figure out how to do this, and I'll see you on the other

102
07:49.570 --> 07:51.100
side where we run through the solution.

103
07:51.100 --> 07:52.420
So pause the video now.

104
07:55.270 --> 07:55.810
All right.

105
07:55.810 --> 08:00.670
So referring to the documentation it's pretty straightforward.

106
08:00.670 --> 08:04.480
The first thing we're going to do is we're going to get hold of the input.

107
08:04.480 --> 08:07.240
So we know the input function looks like this,

108
08:07.240 --> 08:09.670
and we can add in whatever prompt we want.

109
08:09.670 --> 08:12.350
So what is your name?

110
08:12.740 --> 08:18.590
And then we're going to wrap this input inside another set of parentheses.

111
08:18.590 --> 08:22.520
And you can do that without having to type open and closing brackets,

112
08:22.520 --> 08:27.440
if you just highlight the entire line you want to wrap and then type the open bracket,

113
08:27.440 --> 08:30.200
and it should wrap everything inside.

114
08:30.200 --> 08:36.610
So the next step is to calculate the length which is using the len() function that we just heard about.

115
08:36.610 --> 08:41.440
And then finally I'm going to wrap everything in just one more bracket,

116
08:41.440 --> 08:44.320
and this one is the print() function.

117
08:44.320 --> 08:49.690
So the three steps are we get hold of the user input,

118
08:49.690 --> 08:53.530
we calculate the number of characters in that input,

119
08:53.530 --> 08:57.220
and then finally we print it out into the output area.

120
08:57.220 --> 08:59.990
So now let's go ahead and run our code.

121
08:59.990 --> 09:05.120
So first line that comes out is these two print statements from before.

122
09:05.120 --> 09:07.910
And then the third line asks me what is my name.

123
09:07.910 --> 09:12.470
And you can see the process is paused, waiting for me to type that in.

124
09:12.470 --> 09:20.060
And then once I hit Enter, that name is going to replace this part of our code and it's going to calculate

125
09:20.060 --> 09:20.810
the length.

126
09:20.810 --> 09:25.830
And then finally that length is going to be printed and we get 6, which is correct.

127
09:25.830 --> 09:28.650
That's the number of characters in my name.

128
09:28.740 --> 09:33.900
So hopefully you've learned a little bit about documentation and also search.

129
09:33.900 --> 09:38.730
And also the importance of being able to get your own answers as a programmer.

130
09:39.750 --> 09:43.260
So now I have another coding challenge for you.

131
09:43.290 --> 09:50.660
Can you use what you've learned about creating variables and using variables in order to take these

132
09:50.660 --> 09:58.250
two parts, the input() function, and the output of the len() function and store them in separate variables.

133
09:58.250 --> 10:06.020
So there should be a variable called username that will store whatever the user enters into the input

134
10:06.020 --> 10:06.830
area.

135
10:06.830 --> 10:13.350
And then using that variable you're going to create a variable called length to calculate the length

136
10:13.350 --> 10:14.760
of the username.

137
10:14.760 --> 10:24.750
So then instead of this combined print statement, we can end up with simply just printing length at the

138
10:24.750 --> 10:25.410
end.

139
10:25.410 --> 10:29.460
So that way we can get the same result as this,

140
10:29.460 --> 10:36.150
but we use several steps to get there by creating and using some variables.

141
10:36.170 --> 10:38.600
So give this a try.

142
10:38.630 --> 10:43.460
Pause the video and if it doesn't work, come back and we'll go through the solution together.

143
10:47.270 --> 10:47.840
Okay.

144
10:47.840 --> 10:53.330
So the first step is we're going to create our first variable which is called username.

145
10:53.330 --> 11:00.080
And this is simply going to be set to store whatever the user types into the input.

146
11:00.080 --> 11:03.820
And that will get stored under this reference username.

147
11:03.820 --> 11:08.560
And then we're going to use that to calculate the length of the username.

148
11:08.560 --> 11:17.440
But instead of putting the input into the length, we can now refer to it using our variable name, username.

149
11:17.440 --> 11:26.570
So now we can print this new variable that we created on Line 9 and through these three steps, we've

150
11:26.570 --> 11:31.880
made this one single line code hopefully a little bit easier to understand.

151
11:31.880 --> 11:34.250
So we get the username from the input,

152
11:34.250 --> 11:36.470
we calculate the length from the username,

153
11:36.470 --> 11:38.090
and then finally we print the length.

154
11:38.090 --> 11:42.230
And when we hit Run you'll see exactly the same thing happens.

155
11:42.230 --> 11:44.030
We get the same code.

156
11:44.300 --> 11:48.560
Now at this point, you might be wondering to yourself what exactly is the point of variables?

157
11:48.560 --> 11:53.730
We've just turned one very, very short line into three very long lines.

158
11:53.760 --> 11:57.300
Now, of course, we're working with very, very simple examples here.

159
11:57.300 --> 12:04.020
And it might be hard to see the exact use case, but as things get more complicated, you will be glad

160
12:04.020 --> 12:06.030
to know how to use variables.

161
12:06.030 --> 12:11.670
Even in this simple situation, you can see that instead of this line of code, having these three lines

162
12:11.670 --> 12:14.120
of code makes our code a lot more readable

163
12:14.120 --> 12:19.250
because we're labelling each step of our code, we're saying, well, from this line of code, we're

164
12:19.250 --> 12:24.140
going to get hold of the username, from this line of code, we're going to work out the length of the

165
12:24.140 --> 12:24.920
username.

166
12:24.920 --> 12:29.390
And then finally we're going to use that final variable and print it out.

167
12:29.420 --> 12:38.380
Now as things get more complex, and being able to split up code into smaller modules will become a

168
12:38.380 --> 12:39.400
lifesaver.

169
12:39.400 --> 12:46.780
So in the next lesson, I've got a coding exercise for you and hopefully through more and more examples,

170
12:46.780 --> 12:49.630
you'll see just how useful variables can be.

171
12:49.660 --> 12:56.710
Right now, bear with me and just try to learn to create variables and use variables,

172
12:56.710 --> 13:02.080
and I promise it's going to all become very, very clear very soon just how useful they are.