Skip to content

Variables

5 min read

So far every value in your programs has been typed in place: 45, "Selam", 250 + 30. By the end of this lesson you'll give values names, change what a name refers to, and know exactly what Python does when you write =. This is the idea the rest of programming is built on, and it has a few traps that catch almost everyone, so we'll walk into each one on purpose.

Every lesson in this module works fully in your browser.

Giving a value a name

Read this short program and decide what it prints. Take a real guess before you reveal the answer.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

price = 250
print(price)
price = 300
print(price)

Python prints

250
300

price refers to 250 when the first print runs, and to 300 when the second one runs. Python runs the lines in order, top to bottom.

price = 250 gives the value 250 a name, price. From then on, wherever you write price without quotes, Python uses the value it refers to. A name like this is a .

Read = as "gets": "price gets 250". It is not the equals sign from math. It's an instruction: work out the value on the right, then make the name on the left refer to it. We'll say "price is 250" or "price refers to 250".

Step through the same program one line at a time. Watch the table: after each line it shows what every name refers to.

Step through it

Press Next to run one line at a time and watch each name change.

price = 250
print(price)
price = 300
print(price)
Names and their values after line 1
NameValue after line 1
price250new

Output so far

Step 1 of 4

Step 1 of 4. Line 1: price = 250. price is now 250.

Now run it yourself. Then change 300 to 275 and run it again.

price = 250
print(price)
price = 300
print(price)

In the editor, press Escape then Tab to move on.

A name refers to one value at a time

Think of a variable as a name tag, not a box. The tag hangs on one value. When you write price = 300, Python takes the tag off 250 and hangs it on 300. Nothing piles up. What does this print?

What will this print?

Decide before you look. Guessing wrong is how this sticks.

name = "Abel"
name = "Hana"
print(name)
Pick the output

Python prints

Hana

The second line moves the name name from "Abel" to "Hana". "Abel" isn't kept, added, or remembered: a name refers to exactly one value, the last one it was given.

If you picked AbelHana or Abel Hana, you were imagining a box that collects things. That picture is the most common one beginners have, and it's wrong for Python. A new value replaces the old one; the name now refers only to the new value.

Quick check

After points = 0 and then points = 2, what is points?

Using a name to make a new value

The right side of = can use names, including the name on the left. In math, count = count + 1 is impossible. In Python it's normal. Read it as "count gets count plus one": Python works out the right side first, using the value count has now, then moves the name to the result.

Step through it

Press Next to run one line at a time and watch each name change.

count = 5
count = count + 1
print(count)
Names and their values after line 1
NameValue after line 1
count5new

Output so far

Step 1 of 3

Step 1 of 3. Line 1: count = 5. count is now 5.

One more trap. total = price * 2 works out a value once, at that moment. It doesn't set up a rule that keeps total up to date. Predict this one.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

price = 250
total = price * 2
price = 300
print(total)
Pick the output

Python prints

500

Line 2 worked out 250 * 2 and made total refer to 500. Changing price afterwards moves only the name price. total still refers to 500 until you give it a new value.

Change the program so it prints 600: after price = 300, work out the total again.

price = 250
total = price * 2
price = 300
print(total)

In the editor, press Escape then Tab to move on.

Swapping two values

Here's a classic puzzle. a is "tea" and b is "coffee", and you want to swap them. This looks right, but step through it and watch what happens to "tea".

A swap that doesn't work

Press Next to run one line at a time and watch each name change.

a = "tea"
b = "coffee"
a = b
b = a
print(a, b)
Names and their values after line 1
NameValue after line 1
a'tea'new
bnot made yet

Output so far

Step 1 of 5

Step 1 of 5. Line 1: a = "tea". a is now 'tea'.

After line 3, nothing refers to "tea" any more, so line 4 can't bring it back. The fix is a third name that keeps hold of "tea" first:

a = "tea"
b = "coffee"
temp = a
a = b
b = temp
print(a, b)

In the editor, press Escape then Tab to move on.

Python also has a shortcut, a, b = b, a, which you'll understand properly in Module 4.

Naming rules

A name can use letters, digits, and underscores, but it can't start with a digit, and it can't be one of Python's own words, like if or from. Case matters: price and Price are two different names. Python style is lowercase words joined with underscores, like taxi_fare or price_per_cup. Choose names that say what the value means: fare beats x, even though Python accepts both.

The name always goes on the left of =, and the value on the right:

A typo in a name is the most common error of all. Python can only find a name you've actually given a value to:

Exercise

Name a taxi trip

Give names to a trip's starting point, destination, and fare. The fare starts at 250, then the driver asks for 300, so give the fare its new value. Finally, print one sentence using all three names. (You can't call the first one from: that word already means something in Python. Try start.)

Output
From Bole to Piassa costs 300 birr

Your code

# Names for the trip, then the new fare, then one print

In the editor, press Escape then Tab to move on.

Show a solution

This is one way to solve it, not the only one. If yours prints the same thing, it works.

start = "Bole"
destination = "Piassa"
fare = 250
fare = 300
print("From", start, "to", destination, "costs", fare, "birr")

Key takeaways

  • A variable is a name that refers to a value. Read = as "gets": price = 250 is "price gets 250".
  • A name refers to one value at a time. Giving it a new value replaces the old one; nothing is added or kept.
  • Python works out the right side of = first, so count = count + 1 is normal.
  • total = price * 2 is worked out once. Changing price later doesn't change total.
  • Names use letters, digits, and underscores, can't start with a digit, and are case-sensitive.