Variables, Constants & Assignment
Store values clearly and change them when the program needs to.
A program remembers data by giving values names. Variables can change while a program runs; constants are values we intend to keep unchanged.
Predict
Read the code first. Do not run it yet.
score = 12
score = score + 3
MAX_SCORE = 20
print(score)
print(MAX_SCORE)- What will the two lines of output be?
- Which name changes during the program?
- Why is
MAX_SCOREwritten in capitals?
Investigate
Use this code to explain exactly how the program works.
lives = 3
lives = lives - 1
bonus = 5
score = 10
score = score + bonus
print(lives, score)- Trace each assignment from top to bottom.
- After
score = score + bonus, what value is stored inscore? - Explain why
=here means assignment, not “is equal to”.
Variables
A variable is a named storage location for a value. Assignment uses =: the expression on the right is evaluated first, then its value is stored in the name on the left.
score = 8
score = score + 2
print(score)10
score = 8stores 8 inscore.score + 2is evaluated using the current value, giving 10.- The new value 10 replaces the old value in
score.
Constants
A constant is a value that should not change while the program runs. Python does not enforce constants, so the normal convention is to write constant names in UPPER_CASE and leave them unchanged.
VAT_RATE = 0.20
price = 15.00
tax = price * VAT_RATE
print(tax)3.0
= to assign a value. Later, use == to test whether two values are equal.Self-study guide
A variable is a named place where a program stores a value. Assignment means calculating the value on the right-hand side of = and then storing that result in the name on the left. A constant is a value that is intended not to change while the program runs.
Python does not enforce constants with a special keyword. In Python lessons we therefore use an UPPER_CASE name to show that a value should be treated as constant. This is a convention, not a restriction imposed by Python.
const to declare a constant; Python normally uses an UPPER_CASE naming convention instead.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
score = 12 | score = 12 | Assignment uses = in both forms. |
const MAX_SCORE = 20 | MAX_SCORE = 20 | OCR ERL has const; Python has no enforced constant keyword. |
global userID = "Cust001" | userID = "Cust001" | A Python name created outside a function is global. The Python global statement is only needed when assigning to a global name from inside a function. |
Worked example
This example deliberately changes the same variable several times so you can see how reassignment works.
score = 10
BONUS = 5
score = score + BONUS
score = score - 2
print(score)13
scorestarts with the value 10.BONUSis intended to stay fixed at 5.- For
score = score + BONUS, Python first evaluates the right side: 10 + 5 = 15. It then stores 15 back inscore. - The next assignment replaces 15 with 13.
- A variable can therefore hold different values at different times.
Another example
Assignment and comparison look similar but do completely different jobs.
age = 15
print(age == 15)True
age = 15stores a value.age == 15asks a question and produces the Boolean resultTrue.
Common mistakes
- Using
==when you mean to assign a value. - Reading
score = score + 1as an algebra equation. In programming it means update the stored value. - Claiming that Python prevents an UPPER_CASE constant from changing. It does not.
= is assignment and == is equality comparison. They are not interchangeable.Exam-ready checklist
- Explain what a variable and a constant are.
- Trace the value of a variable through several assignments.
- Use
=correctly for assignment. - Recognise the OCR
constkeyword and the Python UPPER_CASE convention. - Distinguish assignment from equality comparison.
What you have learned
- Variables store values that may change.
=assigns a value to a variable.- A constant is intended not to change; Python convention uses
UPPER_CASE. - The right-hand side of an assignment is evaluated before the result is stored.