Show contents
Chapter 2 of 3 · 20 min
Variables, boxes with a label
By the end of this chapter, you'll be able to
- Create a variable and give it a value
- Use a variable in a calculation or in a display
- Change the value of a variable
- Choose clear and valid variable names
A box with a label
Think of a box on a shelf. You stick a label on it ("score") and put something inside (the number 10). Later, you find the box again thanks to its label.
The exact definition: a variable is a name that refers to a value stored in memory. You create it with an assignment, which uses the = sign:
score = 10
Careful: in Python, = doesn't mean "is equal to" as it does in maths. It means "store the value on the right under the name on the left". You read it from right to left.
Then it stores 15 in score. The old value 10 is replaced.
Text description of the animation
- Three lines of code and an empty memory.
- Line 1: a box labelled first_name is created, holding the text "Kenji".
- Line 2: a box labelled score is created, holding 10.
- Line 3: Python first evaluates the right-hand side with the old value: 10 + 5 = 15.
- Then it stores 15 in score. The old value 10 is replaced.
Read, calculate, change
first_name = "Kenji" # a string
score = 10 # a whole number
print(first_name, "has", score, "points")
score = score + 5 # 1) compute score + 5 → 15 2) store 15 in score
print(first_name, "now has", score, "points")
This program displays:
Kenji has 10 points
Kenji now has 15 points
The line score = score + 5 often surprises people at first. Remember the order: Python first computes the right-hand side using the old value, then stores the result in the variable. The old value is replaced.
Naming variables well
- A name starts with a letter or
_, followed by letters, digits or_:total_price,age2. - No spaces or hyphens:
total priceandtotal-priceare invalid. - Python is case-sensitive:
Scoreandscoreare two different variables. - Choose names that say what they hold:
student_countis better thanx.
Your turn
Mini-lab: the piggy bankPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: temperature conversion
You know a temperature in degrees Celsius, stored in the variable celsius = 30. Compute the temperature in Fahrenheit in a variable fahrenheit, using the formula F = C × 9 / 5 + 32, then display 30 °C = 86.0 °F.
Hint 1
* and division /.Hint 2
fahrenheit from celsius, then pass both variables to print().Show the solution and common mistakes
celsius = 30
fahrenheit = celsius * 9 / 5 + 32
print(celsius, "°C =", fahrenheit, "°F")
The result is 86.0, not 86: in Python 3, division with / always produces a decimal number (a float).
Common mistakes:
- Writing
fahrenheit = "celsius * 9 / 5 + 32": with the quotes, it's text, not a calculation. - Writing
Celsiuswith a capital letter on the second line:NameError, because Python is case-sensitive.
Summary
- 1A variable is a name that refers to a value.
- 2The = sign stores the value on the right under the name on the left.
- 3You can reuse a variable in a calculation and update it.
- 4A variable name is clear, has no spaces, and is case-sensitive.
- 5NameError means a name doesn't exist (yet) at that point in the program.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.