Welcome back.
If you write an expression in a computed value,
then you probably want to store that value somewhere.
Variables are useful because one, they allow us to store
the values of expressions to be used later on in our programs.
And two, because they allow us to give us human readable names for
values that we can then reuse later on.
So for example, in math class, if you ever have done geometry,
then you might be familiar with pi.
And when you use pi, you usually don't write out 3.14159, etc.
Instead, you just use the symbol pi.
So, pi is almost like a variable.
It has a name and a value.
And Python has an equivalent to variables in its programming language.
So every variable has a name and a value.
So in this program, we create three variables.
The first variable is called message.
And it's created on line one.
So message has the value What's up, Doc, which is a string.
And then we create a second variable whose name is n and whose value is 17.
And then we create a third variable whose name is pi and
whose value is 3.14159.
Now, the first three lines are setting variables.
And then we reference or use those variables in the last three lines.
So if I run my code and then you'll see that I get three things printed out from
lines five, six and seven.
So when we print out message, then we print out What's up, Doc.
When we print out n, then we print out 17.
When we print out pi, then we print out the value 3.14159.
Now, it's useful to be able to kind of the execution of programs like
this in your head.
And in order to do that, we have what's called a variables values diagram or
a reference diagram.
Now, to draw a reference diagram,
what we do is we go line by line from top to bottom.
So we start at the top, line one, and then go down and for
every line, we're going to say what variable is set to what value.
A reference diagram has two columns.
It has variables, And values.
So what we do is we go line by line from the top of the program to the bottom and
we're going to say what variable is set to what value.
So in this program on line one,
then we set the variable message to the value the string What's up, Doc.
So in our variables values diagram, we say the variable message,
Is set to the value What's up, Doc.
On line two, we set the variable n to the value 17.
On line three, we set the variable
pi to the value 3.14159.
And now by the time we get to line five, when we say print out message.
Then this expression is an expression whose value
evaluates to whatever message it's pointing to.
So when Python says print message, it looks at this and
it says this is an expression,
specifically it's an expression that references the variable message.
And so the Python interpreter looks in this variables values diagram and
it asks what's the value of message.
We see a message here, and we see that messages value is What's up, Doc.
And so what that means is that when we print out message,
then we print out What's up, Doc.
And same thing for when we actually look up the value of n.
So when we say print out the value of n, then Python is going to first look
in this variables values diagram and it sees that value of n is 17
So what that means is that when we print out the value of n, we print out 17.
And same thing for when we print out the value of pi, so
Phyton looks in this reference diagram, sees that pi is 3.14159.
And when we print out pi, then we print out 3.14159.
Let me make this green just to be slightly clearer.
Okay, So
you might ask what happens if we actually overwrite the value of a variable.
So what happens if we actually have a variable that I'll
just call x and we set it to 5 and then I'll say print x and
then I'm going to later on reset x to be 10.
And I'll say print x, okay.
So in order to determine what's going to happen here,
then I'm going to write out my variables values diagram.
So line 1 sets the value of the variable x to the value 5,
and then on line 2 when we print out the value of x,
then we're going to print out 5.
On line 4 we set the value of x to 10,
and so what that does is it says x no longer points to 5,
so I'm going to erase this line.
And I'm going to cross out 5.
X now instead points to 10.
And so X now points to 10.
And here when we print out X,
then we actually are going to print out 10 rather than 5.
And you can see this if we were in our program.
So again here, the first line or the first print statement prints out 5,
the second print statement prints out 10.
So every variable has a name and a value, and
even though the value of a variable can be anything, the name has some restrictions.
So every variable name has to start out with a letter.
And variable names can only contain letters and numbers.
So for example, x is a valid variable name because it starts out with the letter and
only contains letters and numbers.
Same thing with my variable.
I can also have capital letters.
So I can say XYZ, capital XYZ =100.
And these are all valid variable names.
So if I print out the value of XYZ,
then it prints out 100.
One thing to note is that Python is what's called case sensitive.
And what that means is that the variable name XYZ in all capitals is
different from the variable name xyz in lowercase.
So if I print out XYZ in all capitals, then I get 100.
If I print out lower case xyz, then I get this other variable value.
If I print out xYz, then there is no variable that's called xYz and
so I get an error saying that this variable is not defined.
So variable names have to follow these two rules.
Start with a letter and it can only contain letters and numbers.
And what that means is that a variable name that
starts out with a number, so if I say 2x = 50.
This is not a valid variable name because it starts with a number rather than
starting with the letter.
And if I try to save and run my program,
then I'm going to get a syntax error because I have a bad variable name here.
I can have a variable called x2 because that starts with the letter,
but I can't have a variable name 2x.
One thing to not here is that Python also
treats underscores like characters, so
what that means is that I can have a variable
myvariable and I can set it to 5.5.
And if I print out the value of my variable, then I'll get 5.5.
So because Python treats this underscore character as a letter, people
often use underscores as a substitute for spaces in their variable names.
So variables are useful tool that we'll use to one,
remembering in expressions value to use later on.
And two, to give a nice name to the value of an expression so
that it's more human readable.
Until next time.