AQA 7517, section 4.1.1.6
Programs need to store values. Some of those values change as the program runs, and some never do. Programming languages give you two ways to name a stored value to reflect that.
Variables and constants
A variable is a named location in memory whose value can change while the program runs.
A constant is a named location in memory whose value is set once and cannot change while the program runs.
| Variable | Constant | |
|---|---|---|
| Value can change while running | Yes | No |
| Typical use | Scores, counters, user input | Tax rate, maximum size, pi |
| Example | lives = lives - 1 |
MAX_LIVES = 3 |
MAX_LIVES = 3 # constant: written in capitals by convention
lives = MAX_LIVES # variable: starts at 3
lives = lives - 1 # lives changes, MAX_LIVES does not
print(lives) # 2
Python can't stop you reassigning a constant. It relies on the capital-letter convention. Many languages (such as C#, Java and Visual Basic) have a keyword that makes the compiler refuse any attempt to change it.
Advantages of named constants
Using a name like VAT_RATE instead of typing 0.2 throughout your program has several advantages.
- Easier to read.
price * VAT_RATEexplains itself.price * 0.2makes the reader guess what 0.2 means. - Easier to change. If the value changes, you edit it in one place, instead of searching for every copy.
- Fewer errors. You can't mistype the name and quietly get a different number, or forget to update one copy. A typo in a name usually causes an error that you'll notice.
- Protected from accidental changes (in languages that enforce it). The program can't overwrite a value that should stay fixed.
# Without a named constant
total = subtotal + subtotal * 0.2
vat_due = subtotal * 0.2
# With a named constant
VAT_RATE = 0.2
total = subtotal + subtotal * VAT_RATE
vat_due = subtotal * VAT_RATE
Numbers written directly into code like this are sometimes called magic numbers, because their meaning isn't obvious. Replacing them with named constants is good practice.
Worked example
Task: a school runs a trip with a fixed cost per student and a limit on numbers. Show why constants help.
COST_PER_STUDENT = 24
MAX_STUDENTS = 30
booked = int(input("Students booking: "))
if booked > MAX_STUDENTS:
print("Too many. The limit is", MAX_STUDENTS)
else:
print("Total cost:", booked * COST_PER_STUDENT)
If the price goes up to £27 or the coach is replaced by a bigger one that holds 40, you change one line at the top of the program. Both the check and the message update automatically, and nothing else can be missed.
Check yourself
What is the difference between a variable and a constant?
A variable's value can change while the program runs. A constant's value is set once and does not change.
Give two advantages of using a named constant instead of the number itself
Any two of: the code is easier to read because the name explains the value; the value can be changed in one place instead of many; there is less chance of errors from mistyping or missing one copy; the value is protected from being changed by accident.
Which of these should be a constant: the number of days in a week, a player's score, the pass mark for a test?
The number of days in a week and the pass mark. The score changes as the program runs, so it is a variable.