Python Foundations Lesson 2 of 7

Python variables and assignment

25 min, 5 exercises

Builds on lesson 1. Start at lesson 1

Learn

What a variable is

A variable is a name you stick on a value so you can use it later.

name = "Rio"
score = 12
name ← "Rio"
score ← 12
name = "Rio"
score = 12

Read = as "now refers to", not "equals". The right side is worked out first, then the name on the left gets attached to the result. That is why this line, which looks like nonsense in a maths class, is completely normal in Python:

score = score + 1

Learn

Names point at values

score = score + 1 takes the current score (12), adds 1, and moves the name onto the new value, 13.

Think of a variable as a name tag, not a box.

b = a puts a second tag on the same value. Nothing is copied.

Moving one tag later, like a = 0, does not move the other. b still points at the old value.

Learn

Naming rules

Use letters, digits and underscores, and don't start with a digit. player2 works, 2player is an error.

Case matters: Score and score are two different names.

Python style is snake_case, like high_score. Pick names that say what the thing is.

Using a name before it is assigned gives a NameError. Python runs top to bottom, so the assignment has to come first.

Look it upNameErrorPython cheatsheet

Try

Run it: a changing score

The example is already in the editor. Run it, change one thing, and run it again.

In pseudocode
name = "Rio"
score = 12
print(str(name) + " " + "has" + " " + str(score) + " " + "points")
score = score + 5
print(str(name) + " " + "now has" + " " + str(score) + " " + "points")
best = score
score = 0
print("score:" + " " + str(score) + " " + "best:" + " " + str(best))
name ← "Rio"
score ← 12
OUTPUT INT_TO_STRING(name) + " " + "has" + " " + INT_TO_STRING(score) + " " + "points"
score ← score + 5
OUTPUT INT_TO_STRING(name) + " " + "now has" + " " + INT_TO_STRING(score) + " " + "points"
best ← score
score ← 0
OUTPUT "score:" + " " + INT_TO_STRING(score) + " " + "best:" + " " + INT_TO_STRING(best)
name = "Rio"
score = 12
print(name, "has", score, "points")

score = score + 5      # take the old value, add 5, re-attach the name
print(name, "now has", score, "points")

best = score           # a second name tag on the same value
score = 0              # moving 'score' does not move 'best'
print("score:", score, "best:", best)
example.py to run
name = "Rio"
score = 12
print(name, "has", score, "points")

score = score + 5      # take the old value, add 5, re-attach the name
print(name, "now has", score, "points")

best = score           # a second name tag on the same value
score = 0              # moving 'score' does not move 'best'
print("score:", score, "best:", best)

Practice

Add two to a level

The variable level starts at 3. Add a line that increases level by 2 using level = ..., so that the program prints Level: 5. Do not change the print line or type the number 5 anywhere.

Constraint

  • Don't type 5. Let Python add it up.
  • Keep line 1 as level = 3.
  • Add 2 with a level = ... line.
  • Keep the print line as it was.
exercise_1.py to run
level = 3
# your line goes here
print("Level:", level)

Practice

Swap the names

a holds "left" and b holds "right". Swap them so the program prints right left. Use a third variable called temp to help. Do not change the first two lines or the last line.

Constraint

  • Keep the first two lines as they were.
  • Don't type left or right again. Move the values with variables.
  • Use temp: save a in temp, then put temp into b.
  • Keep the last line as print(a, b).
exercise_2.py to run
a = "left"
b = "right"
# swap a and b here
print(a, b)

PracticePredict the output

Trace the assignments

Type exactly what this prints. Work through it one line at a time, the way Python does.

exercise_3.py
coins = 5
coins = coins + 10
spent = coins
coins = 0
print(coins, spent)

Break/fixFix the bug

NameError on line 1

This should print Welcome Kai, but it stops with a NameError. Fix it by changing the order of the lines, not the text.

Constraint

  • Keep both lines: set username first, then print it.
  • Only move the lines. Don't add new ones.
exercise_4.py to run
print("Welcome", username)
username = "Kai"

Your turn

Coins after a quest

A player has 120 coins. They buy a sword for 45 coins, then finish a quest worth 30 coins. Use a variable called coins and change it with coins = ... lines, then print it so the output is Coins: 105. Do not type 105 anywhere: let Python work it out.

Constraint

  • Don't type 105: let Python work it out.
  • Change coins using its own value.
  • Start with coins = 120.
  • Use the 45 and the 30 in your coins lines.
  • Print the coins variable.

On your own. Hints open after your first check.

exercise_5.py to run
coins = 120
Python 3.13

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.