So friends, in a previous skill we discussed the concept of variables with
respect to Ansible.
So what we're going to do now is to discuss the same type of subject this time
in the context of
the Python programming language. So pretty much the same concepts do apply.
This is going to
allow us to effectively work with dynamic values as opposed to, you know,
typing in static hard coded
values. And in the world of programming, this really is very, very useful now
with respect to variables.
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
Variables are honestly one of the most, what would you say? Well known, well
used,
popular concepts in all of programming. You can see this within the go-line
programming language.
You can see it within Python. You can see it within C. You can see it in pretty
much all programming
languages. So this is not an overstatement to say that the concept of variables
is a core component,
a core fundamental practice of programming. Basically, what I'm telling you is
that what we're
learning here is really, really useful. Okay, so hopefully I have convinced you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
learning here is really, really useful. Okay, so hopefully I have convinced you
to pay attention
and to understand that this is important. With that said, let's see how we can
actually work with
variables within Python. So check it out. What I'm going to do is I'll just
create a little folder
here and let's just call this Python stuff. Okay, and let's just change into
this directory here.
Okay, now what I will do is I will create a little file here and I'll just call
this example.py.
This can be an example Python felt that we can use to explore our variables. So
I'll just open this
up right here. Okay, okay. Now before we actually start the very first thing
that I should say is that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that I should say is that
Python happens to be a dynamic programming language. What this means is that
with respect to data
types, Python really does not care. It's going to allow you to assign a
variable without specifying
the type of data that you want to store. What on earth am I even actually blab
bing about? Well,
we'll actually talk about data types in the upcoming video. But just for the
moment, understand that when
we assign a variable value, it's going to be accepted dynamically with Python
and we don't have
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
to specify the type of data. Okay, so whatever the types can be, we'll discuss
that very, very shortly.
Now just to be clear here, what is actually happening is that when we happen to
assign a variable,
we are essentially putting a value in a placeholder that has ultimately been
kept in memory. Okay,
and when we want to retrieve that value, well, this placeholder here basically
points to that
particular point in memory and we can retrieve the value sound complex. Well,
it really isn't. Let's
actually see an action then. So here is how we can assign a variable value in
Python. It's really
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
really hard. You're going to have to really focus. Let's just say I wanted to
create a variable called,
well, I don't know, let's just say food. Now to assign this a value, I just use
the equal sign,
and then I just correlate the value that I want to pass in and that is it. No,
really that is it.
This is how we actually assign variables within Python. We've got the variable
name and we have
associated this with the variable value. So what I can then do is I can say
print out the food variable. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
print out the food variable.
And if I just save this and I run the script by saying python3example.py and to
enter,
what we're actually doing is printing out the value that is associated with
this variable.
Now here is the thing. If I want to actually see whereabouts in memory this
particular value is stored,
I can also use this particular function known as the ID function. Okay, if I
just save this as
opposed to printing at the value, we're essentially going to see the memory
location. This is exactly where
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
the data happens to be stored within my computer. Now this might be a little
bit esoteric,
but let me just show you why this is actually quite important. Say for example,
I wanted to update the
value of this particular variable. What I could do is just simply reassign it.
I'll just use the same name
and then also equals and then just give it a new value. Let's go with, I don't
know, let's go with
cheesers time. Okay, so now if I print out the food, here's what's going to
happen. We create a variable
called food, assign it pizza at this point in the program when you print the
variable, the value WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
variable, the value
will be pizza. However, we can then overwrite it here, give it a new value and
at this point in the
program, when we print it out, we're now going to see cheesers. So check this
out if I run the program
one more time. Look at this right here, we print out pizza, then we print out
cheese. Notice though,
if I happen to print out the ID, what do you think is going to happen is the
number going to stay the
same or is the number actually going to change? We know the value definitely
changed, but let's see
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
the actual place in memory. So let's run the script and as we can see here, we
actually have two
different values. The first one ends in 040 and the second one is in 104. So
essentially here, the
actual value is not being modified, it's really been rewritten entirely in a
new memory location.
And now the variable value as to find here is simply pointing to a new place in
memory,
industrial retrieving this new value. Now here's a thing with respect to the
naming of our variables
that actually are some particular rules. What we can do is we can write names
of variables in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
lowercase if you so wish, we can also do the same by utilizing uppercase. We
actually can use
digits in the name of a variable, although a variable cannot start with a digit
. And we can also
utilize underscores and this actually is very, very common. It's a nice way to
separate out long variables
to make them a lot more readable. Now one thing we do not want to be utilizing
as our variable names
is special characters. So things such as you know like question marks or
percentage signs or maybe
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
the at logo or the hashtag that type of thing, these are not going to be
accepted. It's just going to
give you a problem when you try to run your script. Let me just show you how
that actually operates.
So we know I can say food is equal to pizza and let me demonstrate that it's
okay to use capital
letters here. So let's just print out food. There we go. And let's run the
script. There we go.
As we can see, we can use things like underscores. We can say my food and again
, let's just
reference this as we print it out. Save you. Arru up. Try again. Print out no
problem. We can
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
add in digits such as my first food. If I go to print this out, run it again.
There'll be no problem.
But what I can't do is I can't start the name of the variable with the number.
Okay. If I save this,
we can actually see already here. We're getting a little bit of a warning with
this quickly line.
If I run the program, we're just going to get an invalid value here. Okay. So
be aware of that.
Similarly, we can't use special characters. So I can't say, you know, my ad for
my email and just say
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
john@example.com. If I try to save this and print it, we're going to get an
error one more time. However,
if I change this to be, you know, my email, again, this will be my email, save
you. Run it. It's
going to work just fine. Now one last thing I just want to point out with the
use of our variables
is that it's really quite important, at least with the readability of your code
to use
meaningful names. Okay. So don't actually just name things randomly. This is
just going to serve to
confuse people who look at your code. In fact, it's probably going to just
confuse you further down
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
the line when you revisit old code. Okay. So don't do it. What do I mean? I say
this. Well, don't say
food is equal to and then give like an email address. Okay. Technically, well,
this is absolutely
going to work. It's not going to break Python after all. Okay. This here is
just a placeholder for
this value and it's actually conforming to the rules of Python. It's in lower
case, not using
special characters, so on, so forth. So if we run it, it is going to print at
the value. But when you
have tons and tons and tons of code, lines of codes, this is just going to give
you a headache. It's
going to make troubleshooting really quite awkward, really try to avoid it. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to make troubleshooting really quite awkward, really try to avoid it.
Definitely do not do it.
So those are some of the basic bones of actually creating variables and
updating these values. We
know some of the rules about how we can actually name them. With that now said,
let's take a look at
some of the data we are assigning to our variables by first talking about these
data types.