WEBVTT

1
00:00.000 --> 00:03.960
So friends, in a previous skill we discussed the concept of variables with

2
00:03.960 --> 00:05.520
respect to Ansible.

3
00:05.520 --> 00:09.930
So what we're going to do now is to discuss the same type of subject this time

4
00:09.930 --> 00:10.960
in the context of

5
00:10.960 --> 00:15.620
the Python programming language. So pretty much the same concepts do apply.

6
00:15.620 --> 00:16.080
This is going to

7
00:16.080 --> 00:20.080
allow us to effectively work with dynamic values as opposed to, you know,

8
00:20.080 --> 00:21.600
typing in static hard coded

9
00:21.600 --> 00:26.000
values. And in the world of programming, this really is very, very useful now

10
00:26.000 --> 00:28.160
with respect to variables.

11
00:28.160 --> 00:32.720
Variables are honestly one of the most, what would you say? Well known, well
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:28.160 --> 00:32.720
Variables are honestly one of the most, what would you say? Well known, well

12
00:32.720 --> 00:33.040
used,

13
00:33.040 --> 00:36.880
popular concepts in all of programming. You can see this within the go-line

14
00:36.880 --> 00:37.600
programming language.

15
00:37.600 --> 00:41.350
You can see it within Python. You can see it within C. You can see it in pretty

16
00:41.350 --> 00:42.720
much all programming

17
00:42.720 --> 00:47.780
languages. So this is not an overstatement to say that the concept of variables

18
00:47.780 --> 00:49.360
is a core component,

19
00:49.360 --> 00:53.950
a core fundamental practice of programming. Basically, what I'm telling you is

20
00:53.950 --> 00:54.640
that what we're

21
00:54.640 --> 01:00.080
learning here is really, really useful. Okay, so hopefully I have convinced you
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

21
00:54.640 --> 01:00.080
learning here is really, really useful. Okay, so hopefully I have convinced you

22
01:00.080 --> 01:00.800
to pay attention

23
01:00.800 --> 01:04.160
and to understand that this is important. With that said, let's see how we can

24
01:04.160 --> 01:05.040
actually work with

25
01:05.040 --> 01:09.110
variables within Python. So check it out. What I'm going to do is I'll just

26
01:09.110 --> 01:10.160
create a little folder

27
01:10.160 --> 01:14.290
here and let's just call this Python stuff. Okay, and let's just change into

28
01:14.290 --> 01:15.360
this directory here.

29
01:15.360 --> 01:18.290
Okay, now what I will do is I will create a little file here and I'll just call

30
01:18.290 --> 01:20.160
this example.py.

31
01:20.160 --> 01:24.970
This can be an example Python felt that we can use to explore our variables. So

32
01:24.970 --> 01:25.920
I'll just open this

33
01:25.920 --> 01:29.620
up right here. Okay, okay. Now before we actually start the very first thing

34
01:29.620 --> 01:31.040
that I should say is that
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

34
01:29.620 --> 01:31.040
that I should say is that

35
01:31.040 --> 01:35.360
Python happens to be a dynamic programming language. What this means is that

36
01:35.360 --> 01:36.800
with respect to data

37
01:36.800 --> 01:41.140
types, Python really does not care. It's going to allow you to assign a

38
01:41.140 --> 01:43.040
variable without specifying

39
01:43.040 --> 01:47.330
the type of data that you want to store. What on earth am I even actually blab

40
01:47.330 --> 01:48.080
bing about? Well,

41
01:48.080 --> 01:52.080
we'll actually talk about data types in the upcoming video. But just for the

42
01:52.080 --> 01:53.200
moment, understand that when

43
01:53.200 --> 01:57.070
we assign a variable value, it's going to be accepted dynamically with Python

44
01:57.070 --> 01:57.840
and we don't have

45
01:57.840 --> 02:01.700
to specify the type of data. Okay, so whatever the types can be, we'll discuss
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

45
01:57.840 --> 02:01.700
to specify the type of data. Okay, so whatever the types can be, we'll discuss

46
02:01.700 --> 02:03.120
that very, very shortly.

47
02:03.120 --> 02:06.580
Now just to be clear here, what is actually happening is that when we happen to

48
02:06.580 --> 02:07.760
assign a variable,

49
02:07.760 --> 02:11.600
we are essentially putting a value in a placeholder that has ultimately been

50
02:11.600 --> 02:13.360
kept in memory. Okay,

51
02:13.360 --> 02:17.570
and when we want to retrieve that value, well, this placeholder here basically

52
02:17.570 --> 02:18.480
points to that

53
02:18.480 --> 02:22.290
particular point in memory and we can retrieve the value sound complex. Well,

54
02:22.290 --> 02:23.600
it really isn't. Let's

55
02:23.600 --> 02:27.700
actually see an action then. So here is how we can assign a variable value in

56
02:27.700 --> 02:28.480
Python. It's really

57
02:28.480 --> 02:32.550
really hard. You're going to have to really focus. Let's just say I wanted to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
02:28.480 --> 02:32.550
really hard. You're going to have to really focus. Let's just say I wanted to

58
02:32.550 --> 02:34.160
create a variable called,

59
02:34.160 --> 02:39.260
well, I don't know, let's just say food. Now to assign this a value, I just use

60
02:39.260 --> 02:40.480
the equal sign,

61
02:40.480 --> 02:46.640
and then I just correlate the value that I want to pass in and that is it. No,

62
02:46.640 --> 02:47.360
really that is it.

63
02:47.360 --> 02:52.480
This is how we actually assign variables within Python. We've got the variable

64
02:52.480 --> 02:53.520
name and we have

65
02:53.520 --> 02:59.250
associated this with the variable value. So what I can then do is I can say

66
02:59.250 --> 03:01.280
print out the food variable.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

66
02:59.250 --> 03:01.280
print out the food variable.

67
03:01.280 --> 03:07.350
And if I just save this and I run the script by saying python3example.py and to

68
03:07.350 --> 03:07.840
enter,

69
03:07.840 --> 03:12.440
what we're actually doing is printing out the value that is associated with

70
03:12.440 --> 03:13.440
this variable.

71
03:13.440 --> 03:18.160
Now here is the thing. If I want to actually see whereabouts in memory this

72
03:18.160 --> 03:19.520
particular value is stored,

73
03:19.520 --> 03:23.240
I can also use this particular function known as the ID function. Okay, if I

74
03:23.240 --> 03:24.320
just save this as

75
03:24.320 --> 03:27.720
opposed to printing at the value, we're essentially going to see the memory

76
03:27.720 --> 03:29.920
location. This is exactly where

77
03:29.920 --> 03:33.120
the data happens to be stored within my computer. Now this might be a little
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

77
03:29.920 --> 03:33.120
the data happens to be stored within my computer. Now this might be a little

78
03:33.120 --> 03:33.840
bit esoteric,

79
03:33.840 --> 03:37.990
but let me just show you why this is actually quite important. Say for example,

80
03:37.990 --> 03:40.000
I wanted to update the

81
03:40.000 --> 03:44.580
value of this particular variable. What I could do is just simply reassign it.

82
03:44.580 --> 03:46.320
I'll just use the same name

83
03:46.320 --> 03:49.760
and then also equals and then just give it a new value. Let's go with, I don't

84
03:49.760 --> 03:50.480
know, let's go with

85
03:50.480 --> 03:54.160
cheesers time. Okay, so now if I print out the food, here's what's going to

86
03:54.160 --> 03:55.680
happen. We create a variable

87
03:55.680 --> 03:59.490
called food, assign it pizza at this point in the program when you print the

88
03:59.490 --> 04:00.480
variable, the value
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

88
03:59.490 --> 04:00.480
variable, the value

89
04:00.480 --> 04:05.600
will be pizza. However, we can then overwrite it here, give it a new value and

90
04:05.600 --> 04:06.480
at this point in the

91
04:06.480 --> 04:10.400
program, when we print it out, we're now going to see cheesers. So check this

92
04:10.400 --> 04:11.600
out if I run the program

93
04:11.600 --> 04:15.820
one more time. Look at this right here, we print out pizza, then we print out

94
04:15.820 --> 04:17.440
cheese. Notice though,

95
04:17.440 --> 04:20.250
if I happen to print out the ID, what do you think is going to happen is the

96
04:20.250 --> 04:21.440
number going to stay the

97
04:21.440 --> 04:25.140
same or is the number actually going to change? We know the value definitely

98
04:25.140 --> 04:26.000
changed, but let's see

99
04:26.000 --> 04:31.010
the actual place in memory. So let's run the script and as we can see here, we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

99
04:26.000 --> 04:31.010
the actual place in memory. So let's run the script and as we can see here, we

100
04:31.010 --> 04:32.000
actually have two

101
04:32.000 --> 04:38.160
different values. The first one ends in 040 and the second one is in 104. So

102
04:38.160 --> 04:39.600
essentially here, the

103
04:39.600 --> 04:43.600
actual value is not being modified, it's really been rewritten entirely in a

104
04:43.600 --> 04:44.880
new memory location.

105
04:44.880 --> 04:49.200
And now the variable value as to find here is simply pointing to a new place in

106
04:49.200 --> 04:49.760
memory,

107
04:49.760 --> 04:53.600
industrial retrieving this new value. Now here's a thing with respect to the

108
04:53.600 --> 04:54.640
naming of our variables

109
04:54.640 --> 04:58.880
that actually are some particular rules. What we can do is we can write names

110
04:58.880 --> 05:00.000
of variables in
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

111
05:00.000 --> 05:04.930
lowercase if you so wish, we can also do the same by utilizing uppercase. We

112
05:04.930 --> 05:05.920
actually can use

113
05:05.920 --> 05:10.950
digits in the name of a variable, although a variable cannot start with a digit

114
05:10.950 --> 05:11.600
. And we can also

115
05:11.600 --> 05:15.720
utilize underscores and this actually is very, very common. It's a nice way to

116
05:15.720 --> 05:17.600
separate out long variables

117
05:17.600 --> 05:21.690
to make them a lot more readable. Now one thing we do not want to be utilizing

118
05:21.690 --> 05:22.800
as our variable names

119
05:22.800 --> 05:26.580
is special characters. So things such as you know like question marks or

120
05:26.580 --> 05:28.400
percentage signs or maybe

121
05:28.400 --> 05:31.710
the at logo or the hashtag that type of thing, these are not going to be
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

121
05:28.400 --> 05:31.710
the at logo or the hashtag that type of thing, these are not going to be

122
05:31.710 --> 05:33.040
accepted. It's just going to

123
05:33.040 --> 05:36.410
give you a problem when you try to run your script. Let me just show you how

124
05:36.410 --> 05:37.840
that actually operates.

125
05:37.840 --> 05:42.080
So we know I can say food is equal to pizza and let me demonstrate that it's

126
05:42.080 --> 05:43.120
okay to use capital

127
05:43.120 --> 05:47.080
letters here. So let's just print out food. There we go. And let's run the

128
05:47.080 --> 05:48.320
script. There we go.

129
05:49.120 --> 05:52.840
As we can see, we can use things like underscores. We can say my food and again

130
05:52.840 --> 05:53.360
, let's just

131
05:53.360 --> 05:56.770
reference this as we print it out. Save you. Arru up. Try again. Print out no

132
05:56.770 --> 05:58.560
problem. We can

133
05:58.560 --> 06:03.080
add in digits such as my first food. If I go to print this out, run it again.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

133
05:58.560 --> 06:03.080
add in digits such as my first food. If I go to print this out, run it again.

134
06:03.080 --> 06:04.560
There'll be no problem.

135
06:04.560 --> 06:08.800
But what I can't do is I can't start the name of the variable with the number.

136
06:08.800 --> 06:09.920
Okay. If I save this,

137
06:09.920 --> 06:12.840
we can actually see already here. We're getting a little bit of a warning with

138
06:12.840 --> 06:14.080
this quickly line.

139
06:14.080 --> 06:19.000
If I run the program, we're just going to get an invalid value here. Okay. So

140
06:19.000 --> 06:19.920
be aware of that.

141
06:19.920 --> 06:24.000
Similarly, we can't use special characters. So I can't say, you know, my ad for

142
06:24.000 --> 06:25.520
my email and just say

143
06:25.520 --> 06:30.960
john@example.com. If I try to save this and print it, we're going to get an
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

143
06:25.520 --> 06:30.960
john@example.com. If I try to save this and print it, we're going to get an

144
06:30.960 --> 06:32.480
error one more time. However,

145
06:32.480 --> 06:36.590
if I change this to be, you know, my email, again, this will be my email, save

146
06:36.590 --> 06:37.520
you. Run it. It's

147
06:37.520 --> 06:41.450
going to work just fine. Now one last thing I just want to point out with the

148
06:41.450 --> 06:42.800
use of our variables

149
06:42.800 --> 06:46.740
is that it's really quite important, at least with the readability of your code

150
06:46.740 --> 06:47.360
to use

151
06:47.360 --> 06:51.240
meaningful names. Okay. So don't actually just name things randomly. This is

152
06:51.240 --> 06:52.240
just going to serve to

153
06:52.240 --> 06:55.910
confuse people who look at your code. In fact, it's probably going to just

154
06:55.910 --> 06:57.200
confuse you further down

155
06:57.200 --> 07:01.410
the line when you revisit old code. Okay. So don't do it. What do I mean? I say
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

155
06:57.200 --> 07:01.410
the line when you revisit old code. Okay. So don't do it. What do I mean? I say

156
07:01.410 --> 07:02.560
this. Well, don't say

157
07:02.560 --> 07:06.160
food is equal to and then give like an email address. Okay. Technically, well,

158
07:06.160 --> 07:07.120
this is absolutely

159
07:07.120 --> 07:10.490
going to work. It's not going to break Python after all. Okay. This here is

160
07:10.490 --> 07:11.600
just a placeholder for

161
07:11.600 --> 07:15.490
this value and it's actually conforming to the rules of Python. It's in lower

162
07:15.490 --> 07:16.560
case, not using

163
07:16.560 --> 07:20.290
special characters, so on, so forth. So if we run it, it is going to print at

164
07:20.290 --> 07:21.440
the value. But when you

165
07:21.440 --> 07:25.650
have tons and tons and tons of code, lines of codes, this is just going to give

166
07:25.650 --> 07:26.560
you a headache. It's

167
07:26.560 --> 07:30.090
going to make troubleshooting really quite awkward, really try to avoid it.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

167
07:26.560 --> 07:30.090
going to make troubleshooting really quite awkward, really try to avoid it.

168
07:30.090 --> 07:31.680
Definitely do not do it.

169
07:31.680 --> 07:35.480
So those are some of the basic bones of actually creating variables and

170
07:35.480 --> 07:36.720
updating these values. We

171
07:36.720 --> 07:40.320
know some of the rules about how we can actually name them. With that now said,

172
07:40.320 --> 07:41.120
let's take a look at

173
07:41.120 --> 07:45.370
some of the data we are assigning to our variables by first talking about these

174
07:45.370 --> 07:46.240
data types.
