A constant is a named value in a program that is set once and never changes while the program runs. Unlike a variable, which can be updated at any point, a constant holds a fixed value throughout. Using constants makes programs easier to read, understand, and maintain — especially when the same number appears many times in the code.
What is the difference between a constant and a variable?
A variable is a named storage location whose value can be read, changed, or reassigned as the program runs. A constant is also a named storage location, but its value is fixed at the start and cannot be changed later.
| Feature | Variable | Constant |
|---|---|---|
| Value can change | Yes | No |
| Declared once | Yes | Yes |
| Re-assigned later | Yes | No — attempting to change it is an error |
| Named by convention | camelCase or snake_case | UPPER_CASE (common convention) |
| Purpose | Store data that changes | Store data that stays fixed |
Analogy: a variable is like a whiteboard you can rub out and rewrite; a constant is like text carved into stone — set permanently at the beginning.
What are "magic numbers" and why are they a problem?
A magic number is a literal number written directly in the code with no explanation of what it represents. Consider this piece of pseudocode:
price ← quantity * 14.99
tax ← price * 0.2
What do 14.99 and 0.2 mean? Anyone reading this code — including you, three months later — has to guess. If 14.99 appears in twenty different places and the price changes, you must find and update every single occurrence, and it is easy to miss one.
Now compare:
CONST UNIT_PRICE ← 14.99
CONST VAT_RATE ← 0.2
price ← quantity * UNIT_PRICE
tax ← price * VAT_RATE
The meaning is immediately clear, and if the price changes, you update exactly one line. This is why professional programmers replace magic numbers with named constants.
How do you declare a constant in pseudocode?
The AQA GCSE pseudocode style uses the keyword CONST:
CONST PI ← 3.14159
CONST MAX_PLAYERS ← 4
CONST SCHOOL_NAME ← "Thornton Academy"
Once declared, you use the constant name exactly as you would a variable name in expressions:
circumference ← 2 * PI * radius
IF players > MAX_PLAYERS THEN
OUTPUT "Too many players"
ENDIF
Attempting to reassign a constant is a logic error that well-designed languages will catch.
How do you declare a constant in Python?
Python does not enforce constants at the language level — you cannot create a truly immutable value the same way some other languages can. By convention, programmers write constant names in UPPER_CASE to signal "do not change this":
MAX_SPEED = 120
GRAVITY = 9.81
WELCOME_MESSAGE = "Welcome to the quiz!"
# Later in the program:
speed = MAX_SPEED * 0.9 # fine — reading the constant
MAX_SPEED = 150 # wrong by convention — do not reassign
Some Python programmers use the Final type hint (from the typing module) to document constants more formally, but at KS3 level, the UPPER_CASE convention is the standard approach.
Where are constants used in real programs?
Constants appear wherever a fixed value is central to the program's logic:
| Domain | Constant | Value |
|---|---|---|
| Mathematics | PI | 3.14159… |
| Physics simulation | GRAVITY | 9.81 m/s² |
| Game design | MAX_LIVES | 3 |
| File handling | MAX_FILE_SIZE | 10485760 (10 MB in bytes) |
| Network | HTTP_OK | 200 |
| User interface | SCREEN_WIDTH | 1920 |
In each case, the constant name communicates intent clearly, and centralising the value makes maintenance straightforward.
Why do constants prevent bugs?
Constants prevent two specific categories of bug:
-
Accidental overwrite: a variable could be reassigned by mistake in a complex program. If
scoreis declared as a constant and code later triesscore ← score + 10, the language (or a strict reading of the pseudocode) immediately flags the error — the bug is caught before it causes wrong results. -
Inconsistency: if
14.99is scattered through a 500-line program and you update 19 out of 20 occurrences, the twentieth produces a wrong result that may be difficult to locate. A constant means there is only one place to change.
Frequently asked questions
Does Python actually stop you changing a constant?
No. Python does not enforce the UPPER_CASE convention at the language level — it is purely a signal to other programmers (and your future self). If you write PI = 3.14 and then PI = 5, Python will allow it without complaint. True language-level constants exist in languages such as Java (final), C (const), and Rust (const). In GCSE pseudocode, CONST is treated as truly immutable for exam purposes.
Should a constant go at the top of the program?
Yes. The standard practice is to declare all constants at the very beginning of a program or at the top of the relevant section, before any executable code. This makes them easy to find and update. When reading an unfamiliar program, experienced programmers look at the constants first to understand the key fixed values the program is built around.
What is the difference between a constant and a literal?
A literal is a value written directly in code, such as 42, 3.14, or "hello". A named constant gives that value a meaningful name: const ANSWER = 42. The named constant is almost always preferable because it communicates intent. The only reasonable use of a literal in a program is for the most obvious and universal values — zero and one are common exceptions, because x + 1 is usually clearer with the literal than with x + ONE.
Can two constants have the same value?
Yes. Multiple constants can hold the same numerical value, but each should have a distinct meaning. For example, WINNING_SCORE = 100 and PENALTY_THRESHOLD = 100 both equal 100, but they represent different concepts. Changing one should not affect the other. If they share a name, changing one would inadvertently change both — defeating the purpose.
Professor Turing can help you master constants, variables, and all KS3 programming fundamentals with Socratic questions at aitutors.me.