Lesson 1 of 5 · Variables · 3 min read · Updated

What is a variable?

A variable is a name that holds a value. You will make one, print it, and see how Python keeps it.

Understand

A program often needs to remember something. It might be a price, a name or a score.

A variable is how Python remembers it.

A variable has a name and a value. The name tells Python which value you mean.

A box with a label. The label is the name, price. The box holds the value, 50.

Note

The box is only a first picture. In Python, a name actually points to a value. The last section explains this.

Use it

Here is a variable called price. The = sign gives the value on the right to the variable name on the left.

python
price = 50
print(price)
Output
50

The first line gives price the value 50. The second line asks Python to show the value of price.

A variable can hold text too. Text goes inside quotes. A variable name does not.

python
name = "Asha"
print(name)
print("name")
Output
Asha
name

Without quotes, Python shows the value. With quotes, it shows the word itself.

Practice

Store the number 50 in a variable called price. Then print it.

To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.

Expected output
50

The variable price should hold 50.

Need a hint?
  1. Replace the blank with a number.
  2. The line should read price = 50.
Show a solution
price = 50
print(price)

Make a variable called city that holds the text Madurai. Then print it.

To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.

Expected output
Madurai

The variable city should hold "Madurai".

Need a hint?
  1. Text goes inside quotes.
  2. Write city = "Madurai", then print(city).
Show a solution
city = "Madurai"
print(city)

Make two variables. pens holds 3 and rupees holds 10. Print pens first, then rupees.

To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.

Expected output
3
10

The variable pens should hold 3.

The variable rupees should hold 10.

Need a hint?
  1. Use one line for each variable.
  2. Then use two print lines.
Show a solution
pens = 3
rupees = 10
print(pens)
print(rupees)

Which line stores 20 in a variable called marks?

Why it works

In price = 50, Python reads the value on the right and assigns it to the name on the left.

A variable must exist before you use it. If you print a name that Python has not met, you get an error. The last line of the message says what went wrong:

python
print(price)
Output
NameError: name 'price' is not defined. Did you mean: 'print'?

In plain words: Python does not know a variable called price yet.

Why not write 50 every time? A name tells the reader what the number means.

And when the value has to change, you change it in one place.

Going deeper

The box is a simplification. Python keeps values in memory. A variable is a name that points to a value.

In memory, Python stores the value 50 somewhere. The name price points to it. The name is a label on the value. It is not a box that contains the value.

python
a = 5
b = a
print(a)
print(b)
Output
5
5

Here two names point to the same value. With numbers and text this causes no surprises. It starts to matter with lists, which you will meet later.

Create a free account to save your progress, or sign in.