🎯 Syllabus & Goals 3 min
Cambridge 8.1.1 · Variables and constants 8.1.2 · Basic data types Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Define a variable and a constant, and state one difference between them.
- Declare variables and constants in Cambridge pseudocode and create them in Python.
- Choose the correct data type —
INTEGER,REAL,CHAR,STRINGorBOOLEAN— for a given item of data.
Textbook: Chapter 8, §8.1.1–8.1.2 (pp. 299–304)
Recap / Warm-Up 5 min
Unit 7 was about designing algorithms and dry-running them with trace tables. Unit 8 turns those algorithms into real programs. A computer follows a program exactly as written, so every detail matters.
The textbook names five basic constructs used in every program: data use (variables, constants, arrays), sequence, selection, iteration and operator use. This lesson starts with the first: data use.
Quick starter
A trace table has a column for each variable. In a trace table, what does a new entry in a column tell you?
Reveal the answer
It shows that the value stored in that variable has changed at that step of the algorithm.
🧠 Key Concept 14 min
1 · Named data stores
A program keeps its data in memory. Each item is given a name (an identifier) so the program can find it again. There are two kinds of named store.
StepsToday as a variable — it grows every time you walk. The number of metres in a kilometre (MetresPerKm ← 1000) is a constant: it never changes, but naming it makes the code easier to read than a bare 1000.2 · Declaring variables and constants
It is good practice to declare each variable and constant before it is used. In Cambridge pseudocode a variable is declared with its data type; a constant is given its value and its type is taken from that value.
Cambridge pseudocode
// variables: explicit type DECLARE Score : INTEGER DECLARE Price : REAL // constants: value fixed here CONSTANT MaxScore ← 100 CONSTANT VatRate ← 0.2
The same in Python
# Python has no DECLARE: a variable # is created when first assigned score = 0 price = 0.0 # capitals mark a constant (by convention) MAX_SCORE = 100 VAT_RATE = 0.2
3 · The five basic data types
Every item of data has a type. The type decides how the data is stored, which operations work on it (maths for numbers, joining for text) and allows some automatic validation.
| Pseudocode type | What it holds | Example value | Python type |
|---|---|---|---|
INTEGER | A positive or negative whole number, usable in maths | 25, -4 | int |
REAL | A positive or negative number with a fractional part | 25.0, 3.142 | float |
CHAR | A single character | 'F' | str of length 1 |
STRING | Zero or more characters (letters, digits, symbols) | "Emma", "" | str |
BOOLEAN | Only TRUE or FALSE | TRUE | bool (True/False) |
ItemName as STRING, Price as REAL, QuantityInStock as INTEGER, SizeCode ('S', 'M', 'L') as CHAR and InStock as BOOLEAN. The right types let it add up a basket and flag sold-out items.Worked Example 12 min
Example 1 · Choosing identifiers and data types
A mobile phone company stores these details for each customer. Choose a meaningful identifier and a data type for each.
- Customer's full name →
CustomerName : STRINGit is text of varying length. - Texts sent this month →
NumberOfTexts : INTEGERa count is always a whole number, and it will be added to. - Monthly cost in dollars →
MonthlyCost : REALmoney has a fractional part, e.g. 12.50. - Plan code, one letter A–D →
PlanCode : CHARexactly one character. - On a contract or not →
IsContract : BOOLEANonly two possible answers.
Cambridge pseudocode
DECLARE CustomerName : STRING DECLARE NumberOfTexts : INTEGER DECLARE MonthlyCost : REAL DECLARE PlanCode : CHAR DECLARE IsContract : BOOLEAN CustomerName ← "Sam Taylor" NumberOfTexts ← 120 MonthlyCost ← 12.50 PlanCode ← 'B' IsContract ← TRUE
The same in Python
# One variable of each IGCSE data type number_of_texts = 120 # INTEGER monthly_cost = 12.50 # REAL plan_code = "B" # CHAR (Python stores it as a 1-character str) customer_name = "Sam Taylor" # STRING is_contract = True # BOOLEAN print(type(number_of_texts)) print(type(monthly_cost)) print(type(plan_code)) print(type(customer_name)) print(type(is_contract))
Output
<class 'int'> <class 'float'> <class 'str'> <class 'str'> <class 'bool'>
How the marks are earned: Cambridge usually gives one mark per row for a sensible identifier and the correct type together. Note the quotes: a CHAR uses single quotes ('B'), a STRING uses double quotes.
Example 2 · A constant in a calculation, with a trace table
A pizza shop works out the area of a round pizza. The value of pi never changes, so it is a constant.
Cambridge pseudocode
// Area of a circular pizza CONSTANT Pi ← 3.142 DECLARE Radius : REAL DECLARE Area : REAL Radius ← 15.0 Area ← Pi * Radius * Radius OUTPUT "Area of the pizza is ", Area
The same in Python
# Area of a circular pizza PI = 3.142 # constant: capitals warn "do not change" radius = 15.0 # variable holding a REAL (float) area = PI * radius * radius print("Area of the pizza is", area)
Output
Area of the pizza is 706.9499999999999
Piis set to 3.142 once.a constant is fixed before any processing happens.Radiusis assigned 15.0.a variable, so a different pizza size could be used later.Area← 3.142 × 15.0 × 15.0 = 3.142 × 225 = 706.95.multiplication is done left to right; the result is REAL.
| Pi | Radius | Area | OUTPUT |
|---|---|---|---|
| 3.142 | |||
| 15.0 | |||
| 706.95 | |||
| Area of the pizza is 706.95 |
Why types matter — one line, two answers
print(12 + 3) # two integers: arithmetic print("12" + "3") # two strings: joined, not added
15 123
Try It Yourself 12 min
Goal: State the best data type for: (a) the number of pupils in a class, (b) a pupil's height in metres, (c) whether a pupil has paid for a trip, (d) a pupil's grade letter, (e) a pupil's email address.
Goal: Write the pseudocode declarations (and the Python assignments) for a cinema booking: film title, number of seats, price per seat, a constant booking fee of $1.50, and whether the seats are in the VIP row.
Goal: Write a program in pseudocode and Python that finds the volume of a box (length × width × height) using REAL variables. Add a constant for the price of packing foam per cubic centimetre and output the foam cost.
Hint
Declare four REAL variables and one constant. Work out Volume first, then Cost ← Volume * FoamPrice. Test with 10 × 5 × 2.
📝 Exam Practice 10 min
Define the terms variable and constant.
Mark scheme
- Variable: a named data store / memory location whose value can change during execution (1).
- Constant: a named data store / memory location whose value cannot change during execution (1).
A warehouse program stores data about each item. Complete the table with the most suitable data type for each item of data.
| Data | Data type |
|---|---|
| Item description | |
| Number in stock | |
| Weight in kilograms | |
| Size code (S, M or L) | |
| Whether the item is on order |
Mark scheme
- Item description — STRING (1)
- Number in stock — INTEGER (1)
- Weight in kilograms — REAL (1)
- Size code — CHAR (1)
- Whether on order — BOOLEAN (1)
Explain why a programmer might use a constant, rather than typing the value each time, for the VAT rate in a shop's billing program.
Mark scheme
- The value only needs to be changed in one place (1)…
- …so if the VAT rate changes, every calculation is updated at once (1).
- It cannot be changed accidentally while the program runs (1).
- A meaningful name (e.g.
VatRate) makes the code easier to read / maintain than a bare number (1).
Write pseudocode to declare a variable to hold a student's name, a variable to hold their test mark out of 50, and a constant for the pass mark of 25.
Mark scheme
DECLARE StudentName : STRING(1)DECLARE TestMark : INTEGER(1)CONSTANT PassMark ← 25(1)
🗝️ Recap & Key Terms 3 min
Programs keep data in named stores. A variable can change; a constant cannot. Declare each one before use. Pick the type from what the data is used for: count with INTEGER, measure with REAL, label with CHAR or STRING, decide with BOOLEAN.
- Variable
- A named data store that contains a value that may change during the execution of a program.
- Constant
- A named data store that contains a value that does not change during the execution of a program.
- Declare
- To define the name and data type of a variable, or the name and value of a constant, before it is used.
- Integer
- A positive or negative whole number that can be used with mathematical operators.
- Real
- A positive or negative number with a fractional part, usable with mathematical operators.
- Char / String
- A single character / a sequence of zero or more characters (letters, digits or symbols).
- Boolean
- A data type that can hold only one of two values: TRUE or FALSE.
Homework 1 min
Task (≤ 15 min): Describe two similarities and two differences between a variable and a constant. [4]
Model answer
- Similarity: both are named data stores / memory locations (1).
- Similarity: both have a data type / should have meaningful identifiers / should be declared (1).
- Difference: a variable's value can change during execution; a constant's value cannot (1).
- Difference: a constant is given its value when it is declared; a variable is assigned (and re-assigned) while the program runs (1).