01-variables-constants-assignment.py
python-programming / programming-foundations  ·  Lesson 01
Lesson 01 — Programming foundations

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.

variables-constants-assignment-predict.py
score = 12
score = score + 3
MAX_SCORE = 20
print(score)
print(MAX_SCORE)
Predict before running
  • What will the two lines of output be?
  • Which name changes during the program?
  • Why is MAX_SCORE written in capitals?

Investigate

Use this code to explain exactly how the program works.

variables-constants-assignment-investigate.py
lives = 3
lives = lives - 1
bonus = 5
score = 10
score = score + bonus
print(lives, score)
Investigate the code
  • Trace each assignment from top to bottom.
  • After score = score + bonus, what value is stored in score?
  • 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.

assignment.py
score = 8
score = score + 2
print(score)
Output
10
How it works
  • score = 8 stores 8 in score.
  • score + 2 is 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.

constants.py
VAT_RATE = 0.20
price = 15.00
tax = price * VAT_RATE
print(tax)
Output
3.0
Assignment versus comparisonUse = 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.

OCR focusYou need practical understanding of variables, constants and assignment. OCR Exam Reference Language uses const to declare a constant; Python normally uses an UPPER_CASE naming convention instead.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
score = 12score = 12Assignment uses = in both forms.
const MAX_SCORE = 20MAX_SCORE = 20OCR 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.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This example deliberately changes the same variable several times so you can see how reassignment works.

lesson-01-worked.py
score = 10
BONUS = 5

score = score + BONUS
score = score - 2

print(score)
Output
13
Work through it line by line
  • score starts with the value 10.
  • BONUS is intended to stay fixed at 5.
  • For score = score + BONUS, Python first evaluates the right side: 10 + 5 = 15. It then stores 15 back in score.
  • 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.

lesson-01-example-2.py
age = 15
print(age == 15)
Output
True
Why this example matters
  • age = 15 stores a value.
  • age == 15 asks a question and produces the Boolean result True.

Common mistakes

Avoid these errors
  • Using == when you mean to assign a value.
  • Reading score = score + 1 as an algebra equation. In programming it means update the stored value.
  • Claiming that Python prevents an UPPER_CASE constant from changing. It does not.
Exam-safe warningIn Python, = is assignment and == is equality comparison. They are not interchangeable.

Exam-ready checklist

You should now be able to
  • Explain what a variable and a constant are.
  • Trace the value of a variable through several assignments.
  • Use = correctly for assignment.
  • Recognise the OCR const keyword 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.