Now, in this lesson, I want to talk to you about Python variables,
and it's a really, really useful concept.
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 result 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 whose 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 =, 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?
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 on to the value of Angela.
So to practice that, I have a challenge for you.
So I want you to be able to figure out how many characters are in the name that the user types in using
the input() function. So you already know how the input function works,
you already know how to get the user to enter it, and then to get that data back into our code and
print it out,
but here's the extra step,
"Can you calculate how many characters there are in the name that they type in?"
For example, if they typed in jack, that would be four.
If they typed in Angela that would be six.
Now how do we figure out things when we're programming and we're trying to do something that we haven't
done before?
Well, one of the most important skills as a programmer is to be able to use Google well.
It's to be able to find the answers that you need because it's inevitable that you won't remember
every single function and you won't know every single function.
So we need to read documentation and we need to know how to get hold of it.
So very simple,
we start off with our query, "how to find the length of a string."
And then we add our programming language because this will be different in every programming language.
So I normally just add Python to the end of it.
And then once we hit Enter, normally we'll get hold of some form of documentation,
and the W3Schools documentation for Python is not bad.
And you can also browse through all of the other examples.
So normally for Python, I stick to W3Schools or StackOverflow,
and both of these will give you the right answer.
But if we are reading documentation then you can see that normally there is a whole bunch of other things
that we can look up, just like a dictionary of Python.
Now, as you wouldn't start with a dictionary when you're learning a new language,
believe me, I tried that,
it did not work,
you also wouldn't want to learn through everything in the documentation because it's very dry.
And when you don't have a real-life application, when you're not trying to use a particular function
or some sort of component, then you also don't remember it very well.
So I recommend to find what you need when you need it,
and our brain is for thinking and not for storing,
because these days, as you know, it's super cheap to buy a single terabyte, storage hard disk
online.
I think it costs something like 10 or $20 and it's just getting cheaper every day.
So don't use your brain to store things,
use it to think.
But coming back to our documentation here, in order to get the Python string length, we use the len()
function.
And just like the print() function, it's an inbuilt function in Python,
so we can simply just write len(),
and then we can use whatever it is we want to measure.
For example, in this case it's, "Hello world!" and it will output the number of characters in that string.
So now give it a go yourself.
So the pause one is the instruction set.
And I want you to get user input.
I want you to take that user input and calculate the length of the string that the user inputted,
and finally to print that number out into the output area.
And everything could be done in just one line of code.
So try to find the documentation, try to figure out how to do this, and I'll see you on the other
side where we run through the solution.
So pause the video now.
All right.
So referring to the documentation it's pretty straightforward.
The first thing we're going to do is we're going to get hold of the input.
So we know the input function looks like this,
and we can add in whatever prompt we want.
So what is your name?
And then we're going to wrap this input inside another set of parentheses.
And you can do that without having to type open and closing brackets,
if you just highlight the entire line you want to wrap and then type the open bracket,
and it should wrap everything inside.
So the next step is to calculate the length which is using the len() function that we just heard about.
And then finally I'm going to wrap everything in just one more bracket,
and this one is the print() function.
So the three steps are we get hold of the user input,
we calculate the number of characters in that input,
and then finally we print it out into the output area.
So now let's go ahead and run our code.
So first line that comes out is these two print statements from before.
And then the third line asks me what is my name.
And you can see the process is paused, waiting for me to type that in.
And then once I hit Enter, that name is going to replace this part of our code and it's going to calculate
the length.
And then finally that length is going to be printed and we get 6, which is correct.
That's the number of characters in my name.
So hopefully you've learned a little bit about documentation and also search.
And also the importance of being able to get your own answers as a programmer.
So now I have another coding challenge for you.
Can you use what you've learned about creating variables and using variables in order to take these
two parts, the input() function, and the output of the len() function and store them in separate variables.
So there should be a variable called username that will store whatever the user enters into the input
area.
And then using that variable you're going to create a variable called length to calculate the length
of the username.
So then instead of this combined print statement, we can end up with simply just printing length at the
end.
So that way we can get the same result as this,
but we use several steps to get there by creating and using some variables.
So give this a try.
Pause the video and if it doesn't work, come back and we'll go through the solution together.
Okay.
So the first step is we're going to create our first variable which is called username.
And this is simply going to be set to store whatever the user types into the input.
And that will get stored under this reference username.
And then we're going to use that to calculate the length of the username.
But instead of putting the input into the length, we can now refer to it using our variable name, username.
So now we can print this new variable that we created on Line 9 and through these three steps, we've
made this one single line code hopefully a little bit easier to understand.
So we get the username from the input,
we calculate the length from the username,
and then finally we print the length.
And when we hit Run you'll see exactly the same thing happens.
We get the same code.
Now at this point, you might be wondering to yourself what exactly is the point of variables?
We've just turned one very, very short line into three very long lines.
Now, of course, we're working with very, very simple examples here.
And it might be hard to see the exact use case, but as things get more complicated, you will be glad
to know how to use variables.
Even in this simple situation, you can see that instead of this line of code, having these three lines
of code makes our code a lot more readable
because we're labelling each step of our code, we're saying, well, from this line of code, we're
going to get hold of the username, from this line of code, we're going to work out the length of the
username.
And then finally we're going to use that final variable and print it out.
Now as things get more complex, and being able to split up code into smaller modules will become a
lifesaver.
So in the next lesson, I've got a coding exercise for you and hopefully through more and more examples,
you'll see just how useful variables can be.
Right now, bear with me and just try to learn to create variables and use variables,
and I promise it's going to all become very, very clear very soon just how useful they are.