66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.
Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.
Variables and constants store data in a program; assignment gives them values; input and output let the program talk to the user. These ideas appear in almost every code question, and you may be asked to define the terms, spot them in code or write short programs that use them.
Key facts
Variable: named memory location whose value can change while the program runs
Constant: named value that cannot change while the program runs (const in ERL)
Assignment: variable = expression (the right side is worked out first)
= assigns a value; == compares two values
input() always returns a string: cast with int() or float() for numbers
print() outputs to the screen; use str() to join numbers to text
Use meaningful identifiers, such as totalCost, not x
Notes
Variables and constants
A variable is a named location in memory that stores a value. The value can change while the program is running.
A constant is a named value that cannot change while the program is running. In ERL it is declared with const, for example const VAT = 0.2.
Why use a constant: its value cannot be changed by mistake while the program runs, and if the value ever needs updating (for example a new VAT rate) the programmer changes it in just one place in the code.
Use meaningful names (identifiers), such as totalCost rather than x, so the code is easy to understand.
Assignment
diagram
Assignment gives a variable a value using =, for example score = 0.
The right-hand side is worked out first, then the result is stored in the variable on the left. So score = score + 10 adds 10 to the current score.
Assignment: work out the right-hand side, then store it in the variable on the left
Do not confuse = (assignment) with == (comparing two values).
Input and output
Input: name = input("Enter your name") shows the message and stores what the user types.
Input is always a string, so cast it to use it as a number: age = int(input("Enter your age")).
Output: print("Hello " + name) displays text on the screen. Use str() to join a number to a string.
Say that the value stays the same while the program runs, so it cannot be changed by accident.
Say that it only needs changing in one place in the code if it is ever updated.
Example. A program uses the value 0.2 for VAT in several places. Explain why VAT should be declared as a constant rather than a variable. [2]
Show the model answerHide the model answer
The value does not need to change while the program runs, so declaring it as a constant stops it being changed by accident (1). If the VAT rate changes, it only needs to be updated in one place in the code (1).
State the values after a sequence of assignments
2 to 3 marksGrade 5
Work through one line at a time, keeping a note of each variable's current value.
Always use the value a variable has at that moment, not its starting value.
If asked what the code does, describe the overall effect.
Example. State the final values of a and b, and describe what the code does.
a = 4
b = 7
a = a + b
b = a - b
a = a - b
[3]Show the model answerHide the model answer
a = 7 (1), b = 4 (1). It swaps the values of a and b (1). (a becomes 11, then b = 11 − 7 = 4, then a = 11 − 4 = 7.)
Write a short input, process, output program
3 marksGrade 6
Input the data and cast it to the right type.
Do the calculation and store the result in a variable.
Output the result.
Example. Write a program that asks the user for a temperature in degrees Celsius and outputs it in degrees Fahrenheit, using F = C × 1.8 + 32. [3]
Show the model answerHide the model answer
c = float(input("Enter temperature in Celsius"))
f = c * 1.8 + 32
print(f)
Input stored in a variable and cast to a real number (1); correct calculation stored in a variable (1); outputs the result (1).
Don’t lose marks
Saying a constant 'can never be changed': the programmer can edit it in the code; it just cannot change while the program runs.
Writing assignments backwards, such as 10 = score.
Doing arithmetic on input without casting it: "5" + "3" gives "53", not 8.