yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Variables and assignment | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

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 going to 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 want to 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 two, this prints 324. But when I access view count on line four, this prints 51.

Note that after line four, when the program terminates, the computer clears out all these locations in memory. That means the next time you run 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 100 variables, which means you went off and got 100 different whiteboards.

If you want to 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, because 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 five. If we want to 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.

More Articles

View All
180° Kathmandu, City of Temples | National Geographic
Carved into the foothills of some of the world’s highest peaks, the Kathmandu Valley has been a unique witness to the development of Buddhism and Hinduism. The valley is dotted by more than a hundred sacred spots: temples, stupas, and monasteries, many sh…
Who has the Deathly-est Hallows? Harry Potter or Dr Strange --NERD WARS
Hey everybody! Welcome to Nerd Wars. I’m Fatih and I’m Jeff. We decided to do one topical: it’s Harry Potter versus Doctor Strange. I’ll be arguing Harry Potter, and I’ll be arguing Doctor Strange. Harry Potter is real! It’s real! They got a wand, and yo…
How To Invest in 2024 (How ANYONE can be RICH)
What’s up you guys, it’s Graham here. So this is a tragedy. After posting the video about the money mistakes to avoid in your 20s, which by the way, if you haven’t seen that video already, make sure to check that out so you can smash that like button. Tw…
15 Things You Can LEARN from LUXURY BRANDS
We all have our favorite luxury brands, brands that tug at our heartstrings when we see them in store windows, as we slow our pace down to absorb the beauty of the products on display. But it’s more than just the beautiful display, isn’t it? Everything ab…
How I Made My First Million Dollars Part 2 | Ask Mr. Wonderful Shark Tank's Kevin O'Leary
Hey, Mr. Wonderful here, and welcome to another episode of Ask Mr. Wonderful. Now, this week, as always, has been brought to you by questions from the audience, which I think is the best way to do this. There are two that I found absolutely fascinating, k…
Changes in Momentum Worked Examples | Momentum and Impulse | AP Physics 1 | Khan Academy
So here’s a pink ball rolling toward a green cube that’s sitting at rest on a frictionless surface. When the pink ball hits and slams into the green cube, it’s going to exert a force to the right on the green cube, and the green cube’s going to speed up. …