WEBVTT

1
00:00:01.590 --> 00:00:03.720
<v ->Hi guys and welcome to your first video</v>

2
00:00:03.720 --> 00:00:05.740
in this Python refresher.

3
00:00:05.740 --> 00:00:08.113
In this video, let's talk about variables.

4
00:00:09.180 --> 00:00:13.140
A variable in Python is a name for a value.

5
00:00:13.140 --> 00:00:16.200
You can think of them as similar to those in algebra,

6
00:00:16.200 --> 00:00:18.580
but there are slight minor differences,

7
00:00:18.580 --> 00:00:21.890
that you'll understand as you programme more in Python.

8
00:00:21.890 --> 00:00:24.150
Here's how we define a variable.

9
00:00:24.150 --> 00:00:27.720
You can define a variable name, for example x,

10
00:00:27.720 --> 00:00:31.340
and then you type the equal sign

11
00:00:31.340 --> 00:00:35.560
and finally you type the value, for example, 15.

12
00:00:35.560 --> 00:00:36.970
And what Python is doing here

13
00:00:36.970 --> 00:00:38.450
and this is very important,

14
00:00:38.450 --> 00:00:43.420
is it's creating this value that's after the equal first.

15
00:00:43.420 --> 00:00:45.260
So here Python sees the value 15

16
00:00:45.260 --> 00:00:47.380
and says okay, you're gonna use the value 15,

17
00:00:47.380 --> 00:00:50.160
I will create that or store that somewhere

18
00:00:50.160 --> 00:00:53.300
and then it's gonna say, and what am I going to

19
00:00:53.300 --> 00:00:57.170
refer to that value as, and that's gonna be x.

20
00:00:57.170 --> 00:01:00.090
So the right side of this assignment,

21
00:01:00.090 --> 00:01:02.080
this equal sign happens first,

22
00:01:02.080 --> 00:01:03.963
and the left side happens later.

23
00:01:05.210 --> 00:01:08.100
Outside this point, there are few values in Python

24
00:01:08.100 --> 00:01:10.400
where Python has already created them

25
00:01:10.400 --> 00:01:12.410
before it runs your code.

26
00:01:12.410 --> 00:01:15.720
So Python doesn't create these values when you use them,

27
00:01:15.720 --> 00:01:18.720
but if you have something like 1,500

28
00:01:18.720 --> 00:01:20.460
then Python will do what I just said,

29
00:01:20.460 --> 00:01:23.780
it will create this value and store it in its memory

30
00:01:23.780 --> 00:01:28.190
and then actually become a name for this value just created.

31
00:01:28.190 --> 00:01:29.850
As well as integers,

32
00:01:29.850 --> 00:01:31.700
which are whole numbers like we've got here,

33
00:01:31.700 --> 00:01:35.790
you can define variables such as 9.99.

34
00:01:35.790 --> 00:01:39.340
And here what I've done is I've created the value 9.99

35
00:01:39.340 --> 00:01:43.400
which is a float value, or a value with a floating point

36
00:01:43.400 --> 00:01:45.950
and then told Python to give that value

37
00:01:45.950 --> 00:01:48.130
the name of price.

38
00:01:48.130 --> 00:01:50.170
Let's say we've got a variable called discount

39
00:01:50.170 --> 00:01:51.660
that equal to 0.2.

40
00:01:51.660 --> 00:01:55.130
We can then say that the final result of our price

41
00:01:55.130 --> 00:01:56.790
when the discount is applied

42
00:01:56.790 --> 00:02:01.310
would be the price multiplied by one minus the discount.

43
00:02:01.310 --> 00:02:03.430
And here's what happens when you execute this.

44
00:02:03.430 --> 00:02:05.650
Of course following the rules of mathematics,

45
00:02:05.650 --> 00:02:08.080
which Python does, this gets evaluated first,

46
00:02:08.080 --> 00:02:12.833
since it's between brackets and you get 0.8, one minus 0.2.

47
00:02:13.762 --> 00:02:17.820
Then you're gonna multiply 9.99 by 0.8

48
00:02:17.820 --> 00:02:19.720
and that value will get calculated

49
00:02:19.720 --> 00:02:22.040
and stored in Python's memory.

50
00:02:22.040 --> 00:02:25.133
Whatever that is, result will be the name for that value

51
00:02:25.133 --> 00:02:28.280
that it will refer to from here.

52
00:02:28.280 --> 00:02:30.130
If you wanted to print a value out

53
00:02:30.130 --> 00:02:31.340
and show it to the user,

54
00:02:31.340 --> 00:02:34.050
you can very easily do that with the print function.

55
00:02:34.050 --> 00:02:35.930
So here we will type print

56
00:02:35.930 --> 00:02:39.103
and then inside brackets we will type result.

57
00:02:40.240 --> 00:02:44.800
The brackets in here are not the same as they are out here.

58
00:02:44.800 --> 00:02:47.180
Here they were used for order of preference

59
00:02:47.180 --> 00:02:48.520
in mathematical expressions

60
00:02:48.520 --> 00:02:52.580
and here they're used to signal to the print function

61
00:02:52.580 --> 00:02:53.910
what we want to print.

62
00:02:53.910 --> 00:02:56.670
And this is fairly common in Python.

63
00:02:56.670 --> 00:03:00.780
Whenever you see a name, such as print,

64
00:03:00.780 --> 00:03:03.320
followed by these brackets,

65
00:03:03.320 --> 00:03:04.790
what you've got is a function.

66
00:03:04.790 --> 00:03:06.930
And a function in Python is something that

67
00:03:06.930 --> 00:03:10.430
performs an action such as showing something to the user,

68
00:03:10.430 --> 00:03:14.830
or it calculates an output, based on some inputs.

69
00:03:14.830 --> 00:03:16.070
Or it can do both.

70
00:03:16.070 --> 00:03:18.890
We're gonna look more at functions in a coming video.

71
00:03:18.890 --> 00:03:20.850
But this is how you'd show the user

72
00:03:20.850 --> 00:03:23.370
something in the console.

73
00:03:23.370 --> 00:03:25.060
So at this point, I'm gonna save this file

74
00:03:25.060 --> 00:03:26.293
and I'm going to run it.

75
00:03:27.550 --> 00:03:31.680
And you can see that the output is 7.992.

76
00:03:31.680 --> 00:03:34.600
So this output here is what this line eight,

77
00:03:34.600 --> 00:03:36.780
this print function will calculate.

78
00:03:36.780 --> 00:03:39.060
Notice that we've got a number of other variables up here

79
00:03:39.060 --> 00:03:40.780
but we are not printing them out,

80
00:03:40.780 --> 00:03:42.593
so they will never be shown.

81
00:03:44.070 --> 00:03:46.280
As well as integers and floats,

82
00:03:46.280 --> 00:03:49.682
Python has another data type called the string.

83
00:03:49.682 --> 00:03:52.590
Integers and floats are useful in mathematics

84
00:03:52.590 --> 00:03:53.990
or for calculating things,

85
00:03:53.990 --> 00:03:56.780
whereas strings are used to store characters,

86
00:03:56.780 --> 00:04:00.120
for example a person's name, a person's data of birth,

87
00:04:00.120 --> 00:04:01.960
a person's phone number, so on.

88
00:04:01.960 --> 00:04:04.430
Even though some of them do contain numbers,

89
00:04:04.430 --> 00:04:06.693
they're not gonna be used for mathematics,

90
00:04:06.693 --> 00:04:09.930
so we can store them in each string, each set.

91
00:04:09.930 --> 00:04:13.490
Let's say that we wanted the person's name to be stored.

92
00:04:13.490 --> 00:04:15.470
What we will do is we will same name equal,

93
00:04:15.470 --> 00:04:18.670
and then inside quotation marks we will type Rolf.

94
00:04:18.670 --> 00:04:20.840
What Python does is it creates this string first

95
00:04:20.840 --> 00:04:25.263
and then this name here because a name for that string.

96
00:04:26.430 --> 00:04:28.820
Notice that we can reassign a name,

97
00:04:28.820 --> 00:04:31.890
so that we can do something like this if you'd like.

98
00:04:31.890 --> 00:04:35.270
And now name, instead of being a name for Rolf,

99
00:04:35.270 --> 00:04:37.960
will be a name for Bob.

100
00:04:37.960 --> 00:04:40.370
So no two variables are created here,

101
00:04:40.370 --> 00:04:41.660
it's just the same variable,

102
00:04:41.660 --> 00:04:43.580
but we have essentially moved it.

103
00:04:43.580 --> 00:04:45.830
You can of course print this out if you want.

104
00:04:46.710 --> 00:04:48.560
And I will save this file and run it.

105
00:04:49.710 --> 00:04:52.350
And you'll see down here that we get Rolf printed out

106
00:04:52.350 --> 00:04:55.080
because that is the contents of our string.

107
00:04:55.080 --> 00:04:57.160
Note that Python uses the quotation marks

108
00:04:57.160 --> 00:04:58.810
to signal a string,

109
00:04:58.810 --> 00:05:00.690
but these quotations marks themselves

110
00:05:00.690 --> 00:05:02.460
are not part of the contents,

111
00:05:02.460 --> 00:05:04.163
they're just used as delimiters.

112
00:05:05.140 --> 00:05:06.530
Instead of double quotation marks,

113
00:05:06.530 --> 00:05:08.750
you can use single quotation marks if you like

114
00:05:08.750 --> 00:05:10.580
and that is totally fine as long as

115
00:05:10.580 --> 00:05:12.390
you don't mix and match them.

116
00:05:12.390 --> 00:05:13.550
If you mix and match them,

117
00:05:13.550 --> 00:05:16.400
then that won't work and Python will complain.

118
00:05:16.400 --> 00:05:17.630
I recommend you always stick to

119
00:05:17.630 --> 00:05:19.800
one same type of quotation marks,

120
00:05:19.800 --> 00:05:22.053
and I generally use double quotation marks.

121
00:05:23.640 --> 00:05:27.560
What happens if we print the name multiplied by two,

122
00:05:27.560 --> 00:05:30.630
which is how you would multiply any number?

123
00:05:30.630 --> 00:05:33.440
When you run this you'll See that the output is now Rolf,

124
00:05:33.440 --> 00:05:35.090
because that's line three,

125
00:05:35.090 --> 00:05:37.730
and then you get RolfRolf for line four.

126
00:05:37.730 --> 00:05:40.160
What's happened here is this has essentially done

127
00:05:40.160 --> 00:05:44.220
name plus name, it has added two names together.

128
00:05:44.220 --> 00:05:46.550
That's the same meaning as the multiplication.

129
00:05:46.550 --> 00:05:48.370
And when you add two strings together

130
00:05:48.370 --> 00:05:50.500
what Python does is it will join them

131
00:05:50.500 --> 00:05:52.870
and it will add one after another

132
00:05:52.870 --> 00:05:55.010
so you can end up with a longer string.

133
00:05:55.010 --> 00:05:56.520
This can be very useful at times,

134
00:05:56.520 --> 00:05:58.020
but normally is not what you want

135
00:05:58.020 --> 00:06:00.813
when you multiply a string by a number.

136
00:06:01.740 --> 00:06:03.260
Let's say you have two variables,

137
00:06:03.260 --> 00:06:06.620
a equals 25 and b equals eight.

138
00:06:06.620 --> 00:06:09.050
I mentioned earlier that 25 is special value,

139
00:06:09.050 --> 00:06:11.940
because it is under 255,

140
00:06:11.940 --> 00:06:15.390
so Python creates it as soon as Python starts up.

141
00:06:15.390 --> 00:06:17.710
If it was a larger value, then at this point,

142
00:06:17.710 --> 00:06:19.710
Python would create it.

143
00:06:19.710 --> 00:06:22.200
So we get the value 25 and we say that

144
00:06:22.200 --> 00:06:25.380
a is a name for that value.

145
00:06:25.380 --> 00:06:27.280
From here on we can refer to a,

146
00:06:27.280 --> 00:06:30.280
and really we'll be referring to the value 25.

147
00:06:30.280 --> 00:06:33.370
So what happens when b equals a?

148
00:06:33.370 --> 00:06:35.390
Well Python is gonna evaluate a

149
00:06:35.390 --> 00:06:38.120
and is gonna say okay, a is a name for 25,

150
00:06:38.120 --> 00:06:40.610
so we're gonna use 25 here instead.

151
00:06:40.610 --> 00:06:42.793
So this means the same thing as this.

152
00:06:44.660 --> 00:06:47.760
And then b is now a name for a,

153
00:06:47.760 --> 00:06:51.000
which really means that b is a name for 25.

154
00:06:51.000 --> 00:06:55.360
So both a and b are names for the number 25.

155
00:06:55.360 --> 00:06:56.990
So you can verify that that's correct

156
00:06:56.990 --> 00:06:58.480
by printing them both out.

157
00:06:58.480 --> 00:07:00.940
So I'm gonna print a and then print b,

158
00:07:00.940 --> 00:07:03.370
and you'll see that we get 25 and then 25,

159
00:07:03.370 --> 00:07:04.630
which is perfect.

160
00:07:04.630 --> 00:07:07.763
But what happens if we do b equal 17?

161
00:07:08.670 --> 00:07:11.243
Should a b 17 as well?

162
00:07:12.260 --> 00:07:13.470
Well let's run that.

163
00:07:13.470 --> 00:07:18.470
And now down here you get 25 and 25 for lines four and five

164
00:07:19.400 --> 00:07:24.170
and then you get 25 and 17 for lines nine and 10.

165
00:07:24.170 --> 00:07:25.770
What we've done here in line seven

166
00:07:25.770 --> 00:07:27.670
when we said b equals 17,

167
00:07:27.670 --> 00:07:30.960
is we've told Python to evaluate the value 17,

168
00:07:30.960 --> 00:07:34.920
and then make sure that b is a name for 17.

169
00:07:34.920 --> 00:07:36.930
Python completely disregards that b

170
00:07:36.930 --> 00:07:38.870
was a name for something else before

171
00:07:38.870 --> 00:07:43.160
and it just says okay, this b thing is a name for 17.

172
00:07:43.160 --> 00:07:46.060
A is completely unchanged.

173
00:07:46.060 --> 00:07:48.690
The assignment expression when used like this,

174
00:07:48.690 --> 00:07:50.780
does not change other variables,

175
00:07:50.780 --> 00:07:54.170
it just changes the variable that you refer to.

176
00:07:54.170 --> 00:07:59.050
There are ways for us to change a by using b,

177
00:07:59.050 --> 00:08:01.730
but not when we're talking about integers.

178
00:08:01.730 --> 00:08:04.090
We will talk about that later on in the videos

179
00:08:04.090 --> 00:08:06.740
when we learn about mutability in the manage.

180
00:08:06.740 --> 00:08:09.950
All right, that's it for this Python refresher on variables.

181
00:08:09.950 --> 00:08:13.340
Do remember that this is not a Python introduction course,

182
00:08:13.340 --> 00:08:16.410
because we've talked about some quite advanced things here,

183
00:08:16.410 --> 00:08:19.130
but it's just to get you back up to speed with Python

184
00:08:19.130 --> 00:08:21.010
in case you have experience from before,

185
00:08:21.010 --> 00:08:23.030
or with other programming languages.

186
00:08:23.030 --> 00:08:24.380
Thank you for joining me in this video

187
00:08:24.380 --> 00:08:26.030
and I'll see you in the next one.

