Now in this lesson, I want to talk to you about Python variables and it's a
really, really useful concept. But before we get started,
first I want you to head over to the course resources and get hold of the Day
1 variables start Repl.it sandbox or you can simply type the URL into the
browser.
I want you to first go ahead and fork this starting code so that you can work on
your own version of it. Now once you've done that,
then the first thing I want to do is to take a look at this line of code that
you should have already. And when we run this,
we should be pretty familiar with what happens, right?
It will ask us for our name and when we hit enter, nothing happens.
But behind the scenes,
this name or this input has now been received by this function.
But once that's done, it kind of just disappears, right?
And there's no way for us to be able to refer to it in the future. Or is there?
Well, this is where variables come in really handy.
If I give the results of this action a name,
well then I'll be able to refer to it later on.
So at the beginning of the line,
I'm going to call it name and then I'm going to add an equal sign
and what this does is it assigns whatever it is that the user typed in as the
input to this prompt to a variable called name.
So now if I go ahead and run my code again and I enter a name,
now once I hit enter,
normally there's no way for me to get hold of this anymore, right?
But now I can actually go ahead and write print.
I'm going to print the value of this variable name.
So now if I run my code and I write a name,
hit enter,
you'll see that this gets printed and I can use this at any point in my code
just by referring to the name that's attached to this value.
So it's almost like we've saved the data from this action to a name.
And if you think about it, if you had a phone book, let's say,
and you just jotted down the numbers of people,
the next time you look at this,
there's no way for you to know who's number that is, right?
In a sense it's the same thing with the computer.
Even though we've inputted this piece of data,
there's no way for us to be able to refer to this data unless we give it a name.
So in our phone book, we might say that this particular number is associated with
the name James. And in programming we would call this name James a variable.
So we could write something like James equals and then his phone number.
And this means that in the future, if we ever need this piece of data,
we can just refer to it by its name,
the variable name of James. Now,
as the name variable suggests,
it's something that can be changed or can be varied.
So for example,
just because this name is set to something up here doesn't mean that I can't
change it later on. So to make things simple,
let's go ahead and delete the input function and let's just say name is equal to
Jack. Print name.
When I run that Jack is what gets printed inside the console because I'm now
referring to that piece of data by the variable name.
But if later on I decide to give this variable a different piece of data to hold
on to,
let's say Angela and I print name at this time point after I've changed it,
what do you think these two lines will print?
What do you think will first be printed?
What do you think will be printed second? Let's hit command + enter or control +
enter and see for ourselves.
So even though they're both printing the same variable, at this point when we first
call print, it's holding on to the value of Jack,.
But by line 5 I've now changed it so that it's holding onto the value of
Angela.
This makes it a lot easier when we want to write code like what we did before.
For example,
this is the line of code that we had before and we knew that we were going to
get a piece of data from this input function and then we would calculate a new
piece of data using the Len function
and finally we're going to print everything out.
Instead of having such a long line of code, which can be quite confusing,
we can, if we wanted to, use variables to store each of these steps.
So for example,
we could say that the name was equal to whatever the user inserted as the
input
and then when we wanted to calculate the length,
then we could set that variable to equal the Len function.
And then inside the parentheses, we'll pass in the name, the value, that's stored
in the name variable right here. And finally,
all that we need to do is to print the length,
like so. So in this case,
this data is stored under this name and then that data is what's going to be
used in order to carry out this function
and then the result will get stored in yet another variable
and finally that variable will be printed out.
So let's go ahead and run this code and type in my name.
So Angela and hit enter and it does exactly the same thing,
but now we've got all of these pieces of data all associated with a name that we
can refer to at any later point in our code. Now it's time,
you guessed it, for a code challenge.
And I'm going to get you to apply what you've learned.
So for all of that and more, I'll see you on the next lesson.