All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Variables and constants

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Variables hold values that change; constants hold values that should not. Choosing and naming them well is part of the clean code marks.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Creation, assignment and access (spec 7.1.2)

A variable is created the first time a value is assigned to it with =. It is accessed by using its name in an expression. The right-hand side is worked out first, then stored:

score = 10
score = score + 5
bonus = score * 2
print(score, bonus)

Output:

15 30

Identifiers

Identifiers use letters, digits and underscores, start with a letter, and are case-sensitive: Total and total are different variables. Use meaningful names in snake_case, such as max_speed, not ms.

Constants

Python has no enforced constant, so by convention constants are written in UPPER CASE and assigned once near the top of the program. A named constant explains a value and means it only needs changing in one place.

VAT_RATE = 0.2
MAX_TRIES = 3
price = 50
print(price * (1 + VAT_RATE))

Output:

60.0

In the sample Unit 2 paper, using the supplied constants instead of typing literal values earns marks.

Swapping and multiple assignment

To swap two values you need a temporary variable, or Python's tuple assignment:

a = 3
b = 8
temp = a
a = b
b = temp
print(a, b)
a, b = b, a
print(a, b)

Output:

8 3
3 8

Worked example

Trace x = 4, y = x + 1, x = y * 2, y = x - y. What are x and y?

  1. x = 4
  2. y = 4 + 1 = 5
  3. x = 5 * 2 = 10
  4. y = 10 - 5 = 5

Answer: x = 10, y = 5

Key idea: = assigns the value on the right to the name on the left; identifiers are case-sensitive; constants are UPPER_CASE by convention and assigned once.

Check yourself

  1. What is the value of b after this code runs? ```python a = 7 b = a * 2 a = b - a b = b + a ```
    Show answer

    21. a = 7, b = 14, a = 14 - 7 = 7, b = 14 + 7 = 21.

  2. What is displayed when this code runs? ```python p = "left" q = "right" p = q q = p print(p, q) ```
    Show answer

    right right. After p = q, both hold "right"; q = p then copies "right" again. The original "left" is lost: right right. A temporary variable is needed.

  3. Which exception is raised when this code runs? ```python Total = 10 print(total) ```
    • TypeError
    • ValueError
    • NameError
    • IndexError
    Show answer

    NameError. Identifiers are case-sensitive: total was never created, so NameError.

  4. Which of these is a valid Python identifier?
    • 2nd_place
    • max_speed2
    • max-speed
    • for
    Show answer

    max_speed2. Identifiers may contain letters, digits and underscores but must not start with a digit, contain a hyphen or be a keyword.

  5. Why should a program use a named constant such as MAX_STUDENTS = 30 instead of typing 30 throughout?
    • Constants make the program run faster
    • Python refuses to use the same literal twice
    • Constants use less memory than variables
    • The value is explained by its name and only needs changing in one place
    Show answer

    The value is explained by its name and only needs changing in one place. Named constants improve readability and maintenance.

  6. What is displayed when this code runs? ```python x, y = 5, 9 x, y = y, x + y print(x, y) ```
    Show answer

    9 14. The right-hand side is worked out first using the old values: y = 9 and x + y = 14, then both are assigned: 9 14.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.