1. A labelled box
Making a variable takes one line: a name, a single =, and a value. Read = as “becomes”.
age = 12 name = "Sam" print(age) print(name)
Once a value is in the box, you use the name and Python swaps in the value. Notice print(age) has no quotes: quotes would print the literal word age instead of opening the box.
12 is a number, "Sam" is text (a string, next lesson's star). Numbers have no quotes; text always has quotes.
2. Changing a variable
Vary-able: the value can change. Storing something new just replaces what was in the box. A variable can even use its own current value to work out its next one:
score = 0 print(score) score = 10 # replaced: the 0 is gone print(score) score = score + 5 # take the 10, add 5, store 15 back print(score)
That last line is the most important pattern in this lesson. Read it right-to-left: work out score + 5 first, then store the answer back into score. Every game score, every counter, every level-up works exactly like this.
3. Naming rules
Python is fussy about names. The rules:
| Rule | Fine | Not allowed |
|---|---|---|
| No spaces (use _ instead) | pet_name | pet name |
| Cannot start with a number | player2 | 2player |
| Only letters, numbers and _ | high_score | high-score! |
| Capitals matter | Age and age are two different boxes | |
pet_age beats x every time. In a week you will reread your code and thank yourself.
4. Maths with variables
Anywhere a number works, a variable holding a number works too:
price = 4 how_many = 3 total = price * how_many print("That costs", total, "dollars")
total is a brand-new variable made from the other two. Change price with the control and run it again: the maths follows the box, not the number that used to be in it.
5. Filling the box from the keyboard
Variables get powerful when the user fills them. input() shows a question, waits for typing, and hands you whatever they typed, ready to store:
name = input("What is your name? ") print("Nice to meet you," , name)
Same box idea: the value just arrives from the keyboard instead of being typed into the code. Whoever runs the program gets a different, personal answer.
6. Quick check Part 1 of 2
Seven quick questions. This is half of completing the module. The coding task below is the other half.
What is a variable?
After score = 10 and then score = score + 5, what is in score?
Which of these is a correct variable name?
What does name = "Sam" do?
After x = 4 and y = x * 2, what is in y?
What does age = age + 1 mean?
Which of these would Python see as TWO things, not one variable?
7. Coding task: The name tag maker Part 2 of 2
Write a real program below and press Check. Ask for a name and a favourite animal, store both in variables, and print a name tag that uses them. The robot tests it with different people, so your variables have to do the work. Passing this is the other half of completing the module.
Ctrl+Enter runs your code · Tab inserts 4 spaces · Python loads the first time you press Run or Check (5-10 seconds), then it’s instant.