variables
variables
other building block of our programs. In this case, variables. And variables are
simply containers for some value that can change over time.
So let's say I want to make a guessing game, where the user can guess some number.
And I'll tell them if they guessed correctly or incorrectly. Now, it would be
probably worthwhile to have some way of referring to the user's guess in my
program. What number did they guess? And that is a great use case for a variable.
So I'll call this guess here. And now I want to assign this container called guess
some value, some number to hold as the user's guess. So let's say the first guess
by the user is 10. I could store that value, 10, by saying guess equals 10.
And this equals sign doesn't necessarily say that guess is equal to 10 now, but
more so that we're going to assign this value 10 to be inside the container that
we're going to call guess.
And now that I've done that, if I want to print out the guess, see what is inside
this container, I can do that on line two. I could say, print guess. Now, I'll say
python of guess.py, and I should see 10 printed back out to me on the screen.
Now, variables come-- really powerful when we combine them with things like
functions. So let's try to make our own function that can get a user's guess. Now,
one way to do this is to define some new function.
And I might call it maybe like get_guess. So I'll say def, define me a function,
called get_guess. And it will take no inputs. But I'll then, inside this function,
define myself a variable called guess. And I'll set it equal to 10 here, just like
I did before.
And now, I'll have that function return to me, as we saw in the return value short,
this guess here. Return guess itself. Now, down below, I could say, print
get_guess, just like this, which will run the get_guess function, and then print
the return value, which in this case is this variable called guess. So I'll say
python of guess.py. And I should see 10 again.
Now, it would be not a great program if the user had to open up guess.py and change
their guess every time. So ideally, we would actually let them type in their guess
in the terminal. And there is a function for this, called input in Python.
So I could set this variable, instead of being equal to the number 10, equal to
whatever the user types in at the terminal. And I could say input, just like this,
enter, enter a guess. So now, before we assign this variable called guess a value,
we'll run the input function.
We'll print this text, enter a guess, and whatever the user types in will be now
stored in this variable called guess. So let's try it. I'll say python of guess.py.
And now I'll type in 20. And I should see I printed out 20. So that means that the
return value of get_guess is 20.
What if I did python of guess.py again and I entered 5? Well, in that case, it
seems like the return value of get_guess changes as I type in numbers here. So
that's pretty handy for me.
Now, what if I also had a way to compare the guess against some actual number here.
Well, I could go ahead and define the main part of my program and get working on
the functionality to check the number that the user actually gave us.
So I'll say define the main function now. And inside of here, I'll maybe make a new
variable called guess. Guess equals. And then say get_guess. So now what's going to
happen is get_guess will run, and its value will be assigned to the guess variable
here.
Now, one question you might have is that it seems like I'm using these variables
guess multiple times. And isn't that something I shouldn't be doing? Well, it turns
out that because these variables are inside of different functions, they can be
used in different contexts, in which case they will never refer to that same value.
They can be separated, let's say, by these two functions.
So now, I'll go ahead and I will run python of guess.py. Actually, before I do
that, let me go ahead and actually run the main function down below. And let me try
to run python of guess.py. And also, you enter a guess. I'll type 10.
Nothing yet. But I haven't really printed anything over here. So why don't I go
ahead and now print the guess I got from the user. And now, I see 10 overall.
And again, this worked, because these two variables are inside of what we call
different scopes. This variable is inside the scope of this function, get_guess.
Whereas this guess variable is inside the scope of this function, called main.
So let's go ahead and actually compare the number here. And the number I'm thinking
of is number called 50. So I'll say, if guess is equal to 50, why don't I go ahead
and print correct. Just like this.
Or let's say maybe the guess isn't 50. In this case, I'll print-- I'll print
incorrect. Just like that. So now, this is our program in its entirety. And this
here simply is a question we're asking.
If guess is equal to the number 50, we'll print out correct. If it's not, we'll
print out incorrect. So now, I'll go ahead and I will run python of guess.py, hit
enter. And I'll say, maybe I'm going to guess 50. But I get incorrect.
Hmm. So it seems like I typed in the number 50, but when I went to check if guess
equals this number 50, didn't seem to be correct-- didn't seem to see that it is
that same number.
Now, this is a bit of a tricky scenario if you're new to programming. But it's also
one that kind of shows us the different kinds of variables we can have.
Now, it turns out that in Python, there are many types of variables you can have.
One of them being an integer, as in this number 50 here. And one of them being a
string, where a string is some collection of characters.
And in this case, when I use the input function, I'm actually getting not the
integer 50, but the string of numbers, like 5-0. So to illustrate this real quick
here, I'll say, this is the number 50, but this is the string 50.
And if I were to say, does 50 equal the string 50, I would get back that they
actually don't equal each other. So this is an example of different variable types.
And it's often useful to compare variables of the same type, because if you were
comparing like, let's say, the word cat to the number 50, doesn't quite make sense.
So let's try to actually convert the result of the input function to a whole
number. And I can do that by using this function called int, which takes some text
and converts it to, or at least tries to convert it to a whole number, like 50 or
25, for instance.
So now, I will store inside of this guess variable, inside the get_guess function
the number 50. And then later on, I'll compare that number 50 against the number 50
down below. So I'll say, python of guess.py, hit Enter, and I'll say 50. And now I
see it's correct.
I'll say python of guess.py, and let me try now 25. And I guess incorrect. Let's
say I want the user, though, to actually type in the-- like let's say type in like
50, for instance. Or 10, just like this. That would be an example of a string, not
some whole number like an integer.
So I could change my program. I could simply revert the int from this, and now
it'll give me just plain text. And I could compare. I could say, is guess equal to
this string called 50?
I could check that. I'll say python guess.py, and now I'll say 50, and that seems
to be correct. I could say python, guess.py, and guess 10, and that seems to be
incorrect. So the type of variable you use often depends on the context.
And as you go off and write more programs, hopefully you get to see how powerful
variables can be and what type might best suit you for each scenario.