Variables and assignment

Learn this with friends

ComputingIntro to computer science - Python4:445 check-ins

About this video

The video introduces variables as named, reserved locations in memory that let a program remember values across lines. It explains assignment statements (name, assignment operator, value), how the computer creates or updates a variable, how accessing a variable substitutes in its current value, and why a variable can hold different values at different points in a program. Variables are compared to labelled mini whiteboards, and a game example shows how many variables real programs use.

You'll be able to

  • explain what a variable is and why programs need them.
  • read and write an assignment statement like view_count = 324.
  • predict the value of a variable at different points in a program.
  • identify variables an everyday app or game might use.

Key words

Variable
A named, reserved location in the computer's memory for storing a value.
Assignment statement
An instruction that creates or updates a variable, like view_count = 324.
Assignment operator
The equal sign in an assignment statement, which stores the right-hand value in the left-hand variable.
Allocate
To set aside a new chunk of memory for storing a value.
Unreachable
A memory location with no name pointing to it, so the computer can erase it.
Transcript

- [Instructor] When we run a program, the computer executes each instruction line by line. Then, when it finishes with an instruction, it clears out its working memory. So, the computer has forgotten what it just did by the time it gets to the next line. But what if we want to remember the result of a calculation across lines?

We need a way to tell the computer, "Hey, remember the result of this instruction this time. I'm gonna need this value later." To do that, we use variables. A variable is a reserved location in the computer's memory for storing a value. We associate each location with a name, so it's easy for us as the programmers to refer to a specific location later.

We can also update the value that a specific name points to over time. Because we have a name, we can ask the computer questions like, "What value do you have stored at location view_count right now?" To create a new variable or update the value stored in an existing variable, we'll use an assignment statement.

An assignment statement starts with the name of the variable on the left hand side. Then an equal sign, also called the assignment operator. And on the right hand side, the value to be stored. We read this statement as the variable view_count is assigned the value 324.

When the computer executes this instruction, it'll go off to its short-term memory and look for a label with the name view_count. If it doesn't find one, it'll set aside or allocate a new chunk of its memory and then label that location with the name view_count.

Finally, it'll store the value on the right hand side of the assignment operator in that location. If it does find that label, it will allocate a new chunk of memory, store the new value there, and take that label from the old location and move it to point to the new location.

That old location is now unreachable. There's no way for you as the programmer to ask about that location in memory anymore because it doesn't have a name. There's no need for the computer to keep this around, so it just erases it. Anytime we wanna access the value stored at a location, we can ask the computer to retrieve it by giving it the name.

For example, if I had the instruction print (view_count), the computer would go find the memory location view_count, grab the value stored there and substitute it in. So, this would print 324. Remember, instructions in a program execute in order, so the value stored in a certain variable might be different at different points in the program.

For example, after the first line executes, the location view_count contains the value 324. But after the third line executes, the location view_count now contains the value 501. So, when I access view_count on line 2, this prints 324. But when I access view_count on line 4, this prints 501.

Note that after line 4, when the program terminates, the computer clears out all these locations in memory. That means the next time you're on the program, the computer starts fresh again. You can think of variables like mini whiteboards. When you create a new variable, you go off and get a new whiteboard.

You give that whiteboard a name, and then you write a value on it to store it there. You can create a hundred variables, which means you went off and got a hundred different whiteboards. And if you wanna know what value is stored on a specific whiteboard named color_theme, then you look for the label color_theme, grab that whiteboard and look at the value written on it.

If you have a second or third assignment statement for the same variable color_theme, the computer doesn't go off and grab a new whiteboard 'cause then it would have two whiteboards labeled color_theme, and that's confusing. Instead, it grabs the existing whiteboard named color_theme, erases the old value on the front, and then in its place writes down the new value.

When the program terminates, we erase all the whiteboards, peel the labels off the back, and put them neatly back on the shelf where we found them, so they're nice and clean for the next program execution to use. How might we use variables in a real program? Say, we're building a game.

What data might we need to keep track of? Well, we might need to remember the player's current score, what level they're on, and what character they selected. As the program runs, we'll need to update these values over time. Like, maybe when the player collects an item, their score increases by 5.

If we wanna be able to ask the computer what's the player's current score at any point in the program, we need to tell the computer to store that value in a variable. Some games might have hundreds or thousands of different pieces of data that they need to keep track of, which means hundreds or thousands of different variables.

Think closely about a piece of technology you use every day like an app or a game or an appliance in your house. What data is it keeping track of? What variables might it have? As you learn to think like a programmer, you'll start seeing variables everywhere.

Video © Khan Academy, used under CC BY-NC-SA. Watch on Khan Academy. Puzzicle is not affiliated with Khan Academy.