Watch on Instagram

Code Bytes · "Intro to Programming" · Episode 4 · Data

Variables

A variable is like a labelled box for a value. In Python, score = 10 makes a box called score and puts 10 inside; the = means "put this value in that box". You can swap what's inside at any time, which is why it's called a variable.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

A box with a label

Think of a box with a label stuck on the front. You put a value inside, and later you use the label to find it again. That's a variable: a name (the label) for a value (what's inside).

In Python you make one with a single line:

``python score = 10 ``

That makes a box called score and puts 10 inside.

The = sign means "put it in the box"

In math, = says two things are the same. In Python it's an instruction: put this value in that box. It's called the assignment sign.

Python always works out the right side first, then fills the box on the left. That's why the name goes on the left and the value on the right. (To check if two things are equal, Python uses ==, which a later lesson covers.)

Using the box

To see what's inside, print the name with no quotes:

``python score = 10 print(score) score = 15 print(score) ``

Output:

`` 10 15 ``

print(score) looks inside the box and shows its value. With quotes, print("score") would show the word score instead, because text in quotes is printed exactly as written.

What's inside can vary

When you write score = 15, the old 10 is thrown out and the box now holds 15. The label stays the same while the value changes. That's where the name comes from: what's inside can vary.

Try it yourself

Open any free online Python editor (search "online Python") and run:

``python lives = 3 print(lives) print("lives") lives = 2 print(lives) ``

You should see:

`` 3 lives 2 ``

The second line has quotes, so it prints the word lives, not the value. The last line shows 2 because the box was refilled. Try picking your own name, like level, and give it a new value a few times.

Good to know

  • Every snippet on this page was run with Python 3 and printed exactly what is shown.
  • The labelled box is the standard beginner picture. Strictly, a Python variable is a name that refers to a value, more like a label tied to the value than a box that holds it. That difference matters later, for lists and objects.
  • = is the assignment operator: Python evaluates the right-hand side first, then binds the name on the left. Testing whether two things are equal uses ==.
  • "The old ten is thrown out" means the name no longer refers to it. Python frees values that nothing uses any more.
  • print(score) shows the value; print("score") shows the word score.

Answer to the comment question

What does coins = 4, then coins = coins + 1, then print(coins) print? 5. Python works out the right side first: coins + 1 is 4 + 1, which is 5. Then it refills the coins box with that result, so print(coins) shows 5.

Test your knowledge

Quick quiz: Variables

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What is a variable?
2In Python, what does the = in score = 10 mean?
3What does this print? score = 10, then score = 15, then print(score)
4Which line prints the word score instead of its value?

Sources

Video transcript

What is a variable in code?

Think of a box with a label on it. You put a value inside, and use the label to find it later.

In Python, we type score = 10. That makes a box called score, and puts 10 inside.

Here, equals doesn't mean the same as in math. It means put this value in that box. Python works out the right side first, then fills the box on the left.

Now print score, with no quotes. Python looks inside the box and shows 10.

Then score = 15. The 10 is thrown out, and the box holds 15. Print it again: 15.

That's why it's called a variable. What's inside can vary.

What does this print? Comment your answer below!

That's your byte.

More from "Intro to Programming"

All "Intro to Programming" lessons