-
In this section, I wanna add the idea of
variables to the code we have. So a
-
variable is like a box in memory. And that
box can store a value like a number or a
-
string or something. So, if you look
at this diagram here. If I have the line,
-
x=7. That's called a variable
assignment. And the way to think about
-
this is that in memory there's a box. So
I've drawn this little box here. And it's
-
labeled x. And it can hold a value. So
when the code says x=7, what
-
that means is pick up the value seven and
store that into the box. That is basically
-
what variable assignment does. So if I
look at this code example here that does
-
that. So in the first line I say x=7, so I'm storing a seven in
-
there, and then the later lines mention
x. This one says print(x) or print("lucky", x).
-
And those appearances of x, not with an
equal sign; those just retrieve the value
-
out of the box. So whatever was stored in
there, they're gonna use
-
that. So if I run this. Really what I get
is just, you know, the prints will end up
-
using seven. Because, this x=7
line stored a seven in the box, and the
-
subsequent lines just refer to x. If I
change this to eight, so I just change the
-
one line and I run it, okay well now, all
three lines refer to the eight, because
-
this line is storing an eight in the box.
Try one more experiment here; I guess
-
we've seen numbers and strings. Two data
types we can get pretty far with. So if I
-
say x="hi". This is
storing the string hi in the box. So if
-
I run that then I get, you know, these
lines will mention "hi". So in reality, this
-
name x - I chose x as my first example,
cause I feel that's a common variable in
-
math classes - but really this name, we
could sort of choose anything we wanted, so
-
if, just as long as we're consistent, so
I'm gonna change this to say xyz="hi",
-
so, that will create a variable
called xyz as for the "hi" in there and
-
then, all I need to do is make sure that
each one of these subsequent lines also
-
refers to xyz. So if I run it, this
program still works. So the gist of it is
-
that you can pick whatever name you want for the variable.
-
So long as, later on, when you wanna refer
to that variable, you use the same name.
-
You have to be consistent with yourself.
So the point of this sort of simple use of
-
variables kind of comes down to just,
saving repetition. It means that there's
-
some value that I want to use in my
program, I can assign it into a variable
-
on some early line, and then just use that
variable on a bunch of subsequent lines.
-
And if later I want to change the program
to use some other value, I can maybe just
-
change it in that one place, with the
variable. And then all the uses of that
-
variable will just use the new
value. And so actually we'll use that
-
pattern, for certain, pretty often in
our code. I should mention that this use
-
of the equal sign is not the same, as the
use of an equal sign in a math class or
-
an algebra class. In math, it is a stronger
statement if I say x=y. That's sort
-
of saying well these are equal all the
time. And in the computer, the equal sign
-
does not have that complicated of a
definition. Really, it's just assignment.
-
It's when this line runs; pick up whatever
this value is, and store it in the box
-
labeled, you know, xyz, or whatever.
So it just, it sort of happens when that
-
line runs. But it doesn't then have force
over the whole program. So it is
-
more simple than the use of equal sign
that you may be familiar with, from math
-
classes. Alright, so let me try. A little
code example that uses this, so, this
-
follows the pattern where it gives a little
output, and it says "right code, change the
-
code to produce this output". And so the
idea is that there was someone you had a
-
crush on, you know, in high school you
had a crush on someone and so we're gonna
-
write this little output about that. So
let's say, I had a crush on someone named
-
Alice. So then it's gonna say "Alice,
Alice, Alice, Alice", four times. And it
-
says, "In high school I had a crush on
Alice. Now the Alice curse is lifted." Now
-
the idea with this program is to not
repeat the name Alice a bunch of times. It
-
is to use variables, to, just have the
name once, so that if I decide instead I
-
had a crush on someone named Bob, or Zoe
or whatever. Then I can just change it in
-
the one place. So I'll say, x="Alice". This is what the problem statement
-
says. And the idea is to then write the
rest of the lines just referring to x. So,
-
in order to say the person's name four
times, I can just say print(x,x,x,x).
-
So let's just try
that. See and then I get four Alices. So
-
for the next line, it says "In high school
I had a crush on", and it's kinda like fill
-
in the blank right? I just want to use
whatever the name is. So I can do that by
-
saying print(", and this part just
doing as a string. So I'll just say. "In
-
high school I had a crush on", right that
part is all the same and then I'll end the
-
string and I'll say ,x). So it'll
print the string and then it'll follow it
-
with x. Let's just try. There we go. And
I'll do the last line. You'll notice the
-
semi-colons. I always put the semi-colons
in, in JavaScript and in reality they're
-
often optional. I'll say now the, this is
a little tricky so that's the string and
-
I'll say comma x. So again I sort of mix I
have the string and then x. So I get it up
-
here in the middle curse is lifted. Okay,
let's try that. There that works, so this
-
is a fairly simple use of variables where
we use an equal to a sign to it once and
-
maybe use it a few times below, but
actually that, that is the pattern we are
-
going to use in this class. I think you
are gonna like it a lot, so that's the
-
pattern I want you to practice.