Programming Fundamentals
A constant is a name for a value that is fixed when the program is written and stays the same for the whole run. It works like a variable because you refer to it by name to reach a stored value, but the programmer treats the value as fixed: it is set once and is not given a new value while the program runs.
You give the constant its value with = when you write the program, and afterwards you use it just like a variable: wherever the name appears, it stands for the stored value. If the value needs to be different, the programmer edits that line of code and runs the program again. Constants are a programming technique used in high-level languages such as Python.
A helpful way to picture it is a label on a fixed value: the program can read through the label, but what sits under the label stays put for the whole run.
A school library lets each pupil borrow the same number of books at a time, and that number is fixed by the librarian. The program below stores the limit as a constant and uses its name to work out how many more books a pupil may take out.
BORROW_LIMIT = 4
books_taken = int(input("How many books have you taken out? "))
books_left = BORROW_LIMIT - books_taken
print("You may still borrow", books_left, "books.")Output when the user types 1:
How many books have you taken out? 1
You may still borrow 3 books.
The line BORROW_LIMIT = 4 sets the constant once. The program reads the value through the name BORROW_LIMIT when it works out books_left, but it never gives the name a new value. If the school changes its rule to five books, you edit that one line and run the program again — you do not change it while the program is running.
In Python, programmers mark a constant by writing its name in capitals and giving it its value a single time near the top of the program, before any code that uses it. From then on, the capitalised name stands for that value wherever it appears later in the program.
ENTRYFEE = 4
visitors = int(input("How many visitors? "))
print(ENTRYFEE * visitors)
ENTRYFEE = 6
print(ENTRYFEE * visitors)Output when the user types 10:
How many visitors? 10
40
60The name ENTRYFEE is set to 4 at the top and used in the calculation. The capital letters do not make the value fixed — they are simply how a constant is written.
Python does not stop a constant from being changed while the program runs. If a later line assigns a new value to the capitalised name, Python carries the assignment out without any error or warning, as the last two lines of the program show.
The capital letters are a convention: an unwritten agreement that tells any programmer reading the code that the value must not be changed. Because the language will not enforce it, the convention is the only thing protecting the value, so programmers follow it and do not assign a new value to a constant after it has been set.
This also means a capitalised name that is reassigned later in the program is not being used as a constant, even though it looks like one. Its capital letters promise a fixed value that the code then breaks.
A variable is a named value that can change while a program runs. A constant is a named value that is set to a value once and keeps that value for the whole run. The program reads a constant by its name, and no line in the program gives it a different value.
Use a constant when a value is fixed for the whole run, and a variable when the value changes during the run or is different each time the program runs.
| Question | Constant | Variable |
|---|---|---|
| Can it be given a new value later in the run? | No | Yes |
| When is it useful? | When the value is fixed for the whole run | When the value changes, or differs each run |
| Coach trip example | Seats on the coach | Seats booked so far |
Ask whether the value is fixed for the whole run or changes. If it is fixed, a constant fits. If it changes, or is different each time the program runs, a variable fits. A value that happens not to change during one run is not automatically a constant: if it can be different the next time the program runs, it is a variable. The choice is about the role the value plays in the program, not its data type.
For a coach trip, the number of seats on the coach is fixed for the run, so it can be a constant. The number of seats booked goes up as people book, so it is a variable.
COACHSEATS = 48 # fixed for the whole run
seats_booked = 33 # this will change
spaces_left = COACHSEATS - seats_booked
print(spaces_left)
seats_booked = 41
spaces_left = COACHSEATS - seats_booked
print(spaces_left)Output:
15
7
In the program, COACHSEATS is set once and used in both calculations. seats_booked is given a new value before the second calculation, so it is a variable. The constant is never reassigned; the variable is.
Constants are named values that are intended to stay fixed while a program runs. Using them has three main benefits: the program is easier to read, easier to change and less likely to contain mistakes. These benefits do not make the program run faster or use less memory.
A meaningful constant name tells you what a value represents. In a line such as fee = 6 * 12, the 12 could be a price, a number of weeks or a distance. If the code uses CLUBFEE instead of the bare 12, you can see that 12 is the fee charged to join a club. The name explains the value wherever it is used, so you do not have to guess.
A constant is intended to be set once. If the value needs to change, you edit the line that sets the constant, and every place that uses the constant gets the new value. This is maintainability: the program is easier to keep correct as requirements change. If the fixed value were typed again and again, you would have to find and change every copy. Missing one would leave part of the program using an old value.
Writing the value once also helps prevent mistakes. You cannot mistype a second copy as 120 instead of 12, or as 12.0 instead of 12, because there is no second copy. Replacing a repeated fixed value with a constant therefore puts the value in one place and uses its name throughout.
| Aspect | Repeated bare value | Named constant |
|---|---|---|
| Reading the code | The purpose of the number may be unclear. | The name shows what the value represents. |
| Changing the value | Every copy must be found and edited. | Only the line that sets the constant is edited. |
| Risk of mistakes | Copies can be typed differently. | The value is written once. |
CLUBFEE = 12
junior_amount = 4 * CLUBFEE
senior_amount = 7 * CLUBFEE
print("Junior members:", junior_amount)
print("Senior members:", senior_amount)Output:
Junior members: 48
Senior members: 84
Here CLUBFEE is used in two calculations: one for junior members and one for senior members. If the club changes its fee, only the first line needs editing; both amounts then use the new fee. The constant name also makes the purpose of the number clear to anyone reading the code.
When you read a program, look for a constant: a name in capital letters that is given a value once, near the top of the program, and is not given a new value later. For example, TICKET_FEE set to 4.5 and then used in a calculation is being used as a constant.
A capitalised name that is reassigned later is not being used as a constant, even though it looks like one. Once a later line assigns a new value to that name, the programmer is treating it like a variable; the capital letters are only a signal, and they do not stop the name being changed.
A fixed value such as 0.05 can appear again and again in a program. A bare value like this does not explain what it means, and if it needs to change, you must find and edit every copy. To improve the program, replace the repeated fixed value with a constant: set it once near the top with a meaningful capitalised name, then use the name throughout instead of writing the value again and again. The repeated bare values disappear, so the program is easier to read and check, and a change is made in one place only.
pages = int(input("How many pages? "))
print("Single: " + str(pages * 0.05))
print("Double: " + str(pages * 2 * 0.05))Output when the user types 20:
How many pages? 20
Single: 1.0
Double: 2.0
The first program writes 0.05 twice, so it is hard to tell what the value is for, and a change would mean editing two lines. In the improved program, COST_PER_PAGE is set once and used twice, so the price is easy to find and change. It is being used as a constant because it is set once and only read afterwards.
COST_PER_PAGE = 0.05
pages = int(input("How many pages? "))
print("Single: " + str(pages * COST_PER_PAGE))
print("Double: " + str(pages * 2 * COST_PER_PAGE))Output when the user types 20:
How many pages? 20
Single: 1.0
Double: 2.0