< Return to Video

Variables (6 mins)

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

English subtitles

Revisions