Python variables and assignment
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 = 12name ← "Rio"
score ← 12name = "Rio"
score = 12Read = 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 + 1Learn
Name tags and naming rules
Python takes the current score (12), adds 1, and moves the name score onto the new value, 13. The old value is not kept anywhere.
Think of variables as name tags, not boxes. When you write b = a, you are not copying something into a new box; you are putting a second tag on the same value. For numbers and strings this difference rarely bites you, but it will matter later with lists.
Naming rules:
- Letters, digits and underscores only, and no digit at the start.
player2is fine,2playeris an error. - Names are case-sensitive:
Scoreandscoreare two different variables. - Python style uses
snake_case: lowercase words joined by underscores, likehigh_score. - Pick names that say what the thing is.
xtells you nothing;lives_lefttells you everything.
Using a variable before you have created it gives a NameError. Python only knows names you have actually assigned, and order matters: the assignment has to run before the line that uses it.
GCSE exam boards
- OCR 2.2.1 Programming fundamentals, 2.2.2 Data types and casting
- AQA 3.2.1 Data types, 3.2.2 Sequence, selection, iteration
- Edexcel 6.3.2 Variables and constants
Try
Run it: a changing score
The example is already in the editor. Run it, change one thing, and run it again.
In exam 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)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
- Work it out with code. Printing the answer as text doesn't count.
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
- Work it out with code. Printing the answer as text doesn't count.
a = "left" b = "right" # swap a and b here print(a, b)
PracticePredict the output
Follow the name tags
Type exactly what this prints. Work through it one line at a time, the way Python does.
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
- Work it out with code. Printing the answer as text doesn't count.
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
- Let Python work out 105.
- Change coins using its own value.
On your own. Hints open after your first check.
Passed
You can now