CS4Data Types, Variables, Constants and Input/Output
3.2.1 Data types
Data types
Each variable has a data type. The data type determines what kind of value the variable will hold and the set of operations that can be performed on it.
The type decides what you can do with a value. You can divide two integers, but not two strings. You can join two strings with +.
A string that looks like a number is still a string: "7" + "1" gives "71", but 7 + 1 gives 8.
Exams use AQA’s general names (integer, real, Boolean, character, string). When a question asks for the Python data type, give Python’s name.
A real number can be called a float. A whole-number value written with a decimal point, such as 2.0, is real, not integer.
AQA name
What it holds (AQA)
Examples
Python
Integer
A whole number
17, 0, -4
int
Real
Numbers that can include fractions (values after the decimal point)
3.5, -0.25, 2.0
float
Boolean
One of two values: True and False
True, False
bool
Character
A single character from a character set (for example ASCII)
'A', '7', '?'
No separate type: a string of length 1
String
A sequence of characters
'Ella', '07700 900123'
str
Exam questions use AQA’s general names. In Python code, use Python’s names
Exam tip‘It’s the type of data’ isn’t a description of a data type. Say it decides what kind of value a variable can hold and which operations can be done on it. Only 1 in 5 students managed this in 2025.
Exam tipReal is not a Python type. If a question asks for the Python data type of a number with a decimal point, the answer is float.
Data type
Determines what kind of value a variable will hold and the set of operations that can be performed on it.
Integer
A whole number.
Real (float)
A number that can include a fraction (values after the decimal point).
Boolean
A data type that can represent the two values True and False.
Character
A single character from a character set, such as ASCII.
String
A sequence of characters.
3.2.1 Data types
Choosing the most suitable data type
Pick the type that fits the values and what the program will do with them.
Numbers you never calculate with (phone numbers, bank sort codes, ID numbers that start with 0) are strings. An integer would lose the leading 0.
Money with pence is real. Storing £3.85 as an integer would lose the 85p.
A comparison such as score > 50 gives a Boolean, so its result can only take two values: True or False.
Every expression has a type, worked out from the value it gives.
Data
Most suitable type
Why
Number of pupils in a class
Integer
Always a whole number; may be used in sums
Price of a sandwich, £3.85
Real
Has pence after the decimal point
Is the door locked?
Boolean
Only two possible answers
A grade such as B
Character
Exactly one character
Phone number 07700 900123
String
Starts with 0 and has a space; no arithmetic is ever done on it
Postcode, e.g. LS6 3QW
String
Letters, digits and a space
Worked exampleState the data type of the value each Python expression gives: (a) 17 / 4 (b) 17 // 4 (c) "4" + "2" (d) 9 >= 10 (e) 8 / 2
(a) 17 / 4 = 4.25 → real (Python float)/ is real division
(b) 17 // 4 = 4 → integer// is integer division: it drops the remainder
(c) "4" + "2" = "42" → string+ joins two strings; nothing is added up
(d) 9 >= 10 = False → Booleana comparison is either True or False
(e) 8 / 2 = 4.0 → realin Python, / always gives a float, even when it divides exactly
(a) real (b) integer (c) string (d) Boolean (e) real
Exam tipIf asked why a value isn’t a string, don’t say ‘because it is converted to an integer’. Give the reason: the program does arithmetic with it, which needs a number.
3.2.2 Programming concepts
Variables, declaration and assignment
A variable is an identifier that represents a memory location that can store a value. This value can change during execution of a program.
Assignment: an assignment statement sets the value of a variable or constant. Pseudo-code uses ←; Python uses =.
Declaration creates a variable. In Python (and in AQA’s pseudo-code) a variable is declared the first time a value is assigned to it.
Read an assignment right to left: work out the right-hand side, then store the answer in the variable on the left.
Assigning a new value replaces the old one. The old value is lost.
For exam purposes, the first value assigned fixes the type: totalCost = 2.45 makes totalCost real, and it should hold real values from then on.
In Python, = assigns and == compares. In pseudo-code, ← assigns and = compares.
AQA pseudo-code
Python
What happens
score ← 0
score = 0
Declares score and sets it to 0
score ← score + 5
score = score + 5
Works out score + 5, then stores the result back in score
name ← 'Priya'
name = "Priya"
Stores a string. AQA’s pseudo-code uses single quotes; Python code uses double
A variable’s value can be replaced while the program runs. A constant’s can’t
Worked exampleWork out the final values of a and b.
a ← 4
b ← a * 3
a ← b - 2
b ← b + a
a = 4first assignment
b = 4 × 3 = 12uses the current value of a
a = 12 − 2 = 10a’s old value, 4, is replaced
b = 12 + 10 = 22uses the new value of a
a = 10, b = 22
Exam tip‘Assigning something to a variable’ is too vague to describe assignment. Say an assignment statement sets the value of a variable (or constant).
Variable
An identifier that represents a memory location that can store a value. This value can change during execution of a program.
Assignment
An assignment statement sets the value of a variable or constant.
Variable declaration
Creating a variable. In Python, a variable is declared when it is first given a value, e.g. age = 12.
3.2.2 Programming concepts
Constants and why named constants are used
A constant is an identifier that represents a memory location that can store a value. This value cannot change during execution of a program.
AQA pseudo-code: CONSTANT VAT_RATE ← 0.2. Constant names are always in capitals.
Python has no true constants. By convention a name in capitals with underscores marks a constant: VAT_RATE = 0.2. Never change it later in the program.
Why use a named constant?
1. The value is set once and can’t be changed by accident while the program runs.
2. If the value ever changes (a new tax rate), you edit one line, not every place the number appears, so you can’t miss one.
3. The name explains the number: VAT_RATE says what 0.2 is for, so the code is easier to read and maintain.
Why use a variable? For any value that changes as the program runs: inputs, totals, counters.
Worked exampleRewrite this line so the 40 is a named constant.
if mark >= 40:
print("Pass")
PASS_MARK = 40 at the top of the programcapitals mark it as a constant
if mark >= PASS_MARK:use the name wherever the value is needed
PASS_MARK = 40
mark = int(input("Mark: "))
if mark >= PASS_MARK:
print("Pass")
Exam tipFor ‘why use a named constant?’, give a benefit (change it in one place; it can’t be changed by mistake; the name makes the code clearer), not just ‘its value can’t change’.
Constant
An identifier that represents a memory location that can store a value. This value cannot change during execution of a program.
Named constant
A constant given a meaningful name, such as MAX_PLAYERS, used instead of typing the value each time.
3.2.2 Programming concepts
Meaningful identifiers
An identifier is a name for a variable, a constant or a subroutine.
A meaningful identifier says what it holds: ticketPrice, not t or x.
Why? The code is easier to read and understand, for you later and for other programmers, so it is easier to debug and maintain, and mistakes are less likely.
AQA’s Python style: variables in camelCase (numberOfTickets); constants in CAPITALS with underscores (MAX_SPEED).
Python’s rules: no spaces; only letters, digits and underscores; can’t start with a digit; can’t be a keyword such as if, for or while. Capitals matter: total and Total are different names.
Keep names short enough to write accurately: seatsLeft beats numberOfSeatsLeftInTheTheatre. Examiners see long names misspelt.
Single letters are fine where they are meaningful: i as a loop counter, x and y for coordinates.
Poor
Better
Why
n
name
says what is stored
p2
pricePerKg
says what the number means
ticket price
ticketPrice
a space splits it into two names
2ndScore
secondScore
can’t start with a digit
vat
VAT_RATE
a constant is written in capitals
Exam tipEvery code question has a design mark for meaningful variable names throughout. For two passwords, p1 and p2 are the least that count; a and b don’t.
Identifier
A name given to a variable, constant or subroutine.
Meaningful identifier
A name that describes what the variable, constant or subroutine holds or does.
camelCase
Writing a name with no spaces, starting each new word with a capital: numberOfItemsSold.
3.2.7 Input/output
Input from the keyboard and casting
Programs read data typed at the keyboard. In Python, input always gives a string.
input() returns a string even if the user types 25. To do arithmetic, convert it (this is called casting): int() for whole numbers, float() for numbers with a decimal point.
The text inside input("…") is a prompt for the user. It is optional, and examiners ignore it unless a question asks for it. Keep prompts short.
Without a cast, age + 1 on a string is a type error, and "3" * 2 gives "33", not 6.
Checking that the input is sensible (a range or a length) is validation, covered in the topic on robust programs.
AQA pseudo-code
Python
Gives
name ← USERINPUT
name = input("Name? ")
a string
qty ← STRING_TO_INT(USERINPUT)
qty = int(input("How many? "))
an integer
cost ← STRING_TO_REAL(USERINPUT)
cost = float(input("Cost? "))
a real (float)
Worked exampleThe user types 4 and then 5. What does this program output? What would it output with int() round each input()?
first = input()
second = input()
print(first + second)
first = "4", second = "5"input() returns strings
"4" + "5" = "45"+ joins strings
With int(input()): 4 + 5 = 9now they are integers, so + adds
45 without casting; 9 with int()
Exam tipMissing int() or float() round an input() that is used in a calculation is an error in code, and costs you full marks on a code question even if the rest is right.
Exam tipUse float() for money. int(input()) for a price such as 2.50 crashes the program.
Casting
Converting a value from one data type to another, e.g. int("25") gives the integer 25.
USERINPUT
AQA pseudo-code for reading a value typed at the keyboard.
3.2.7 Input/output
Output to the screen
Programs show data and messages on the screen with OUTPUT (pseudo-code) or print() (Python).
print(a, b) prints each item with a space between them, then moves to a new line. Pseudo-code: OUTPUT a, b.
Concatenation with + joins strings with no space. Numbers must be converted first: "Total: " + str(total).
f-strings put values inside text: f"{name} scored {score}". The f before the quotes is needed.
end="" stops print moving to a new line.
A comma in an assignment is not concatenation: msg = "Hi", name makes a pair of values, not one string. Use +.
Never write OUTPUT in a Python answer. It is pseudo-code, and it can’t gain the mark.
Worked exampleState exactly what this program outputs.
name = "Sam"
score = 18
print("Well done", name)
print("Score: " + str(score))
print(f"{name} scored {score} out of 20")
print("Done", end="")
print("!")
Well done Sama comma puts one space between items
Score: 18the space is inside the string; str() turns 18 into "18"
Sam scored 18 out of 20the f-string replaces {name} and {score}; the braces aren’t printed
Done!end="" keeps the next print on the same line
Well done Sam
Score: 18
Sam scored 18 out of 20
Done!
Exam tipWhen you state an output, write exactly what appears on screen: no quotation marks, and the text of an f-string without its braces.
Concatenation
Joining two strings into one with +.
f-string
A Python string starting with f in which {identifier} is replaced by the value, e.g. f"Hi {name}".
OUTPUT
AQA pseudo-code for showing a value on the screen. In Python, use print().
3.2.2 Programming concepts
Statement types and the three combining principles
Programs are built from a few statement types: variable declaration, constant declaration, assignment, iteration, selection and subroutine (procedure or function). They are combined in three ways.
Sequence, selection and iteration are basic to all high-level imperative programming languages.
Selection is covered in Operators and selection; iteration in Iteration; subroutines in Subroutines.
In a numbered program, the line that uses selection is the IF line, and the line that uses iteration is the FOR, WHILE or REPEAT line.
Combining principle
What it means
Pseudo-code
Python
Sequence
Statements run one after another, in order
one line after another
one line after another
Selection (choice)
A block of code runs only when a certain condition is satisfied
IF … THEN … ENDIF
if …:
Iteration (repetition)
A block of code is repeated
FOR, WHILE, REPEAT
for, while
The three combining principles as flowchart fragments
Worked exampleName the statement type or combining principle on each numbered line.
Line 2: variable declaration with assignmentfirst value given to tries
Lines 3–8: iterationWHILE repeats lines 4–7
Line 4: assignmenttries is given a new value
Lines 5–7: selection, nested inside the iterationline 6 runs only when the condition is true
Lines 1–2 run in sequence before the loop; the loop body also runs in sequence each time
Sequence
Statements carried out one after another, in the order written.
Selection
A selection statement allows a block of code to be executed only when a certain condition is satisfied.
Iteration
The repetition of a block of code.
3.2.2, 3.2.7
Writing a short program to a brief
Short code questions (2–6 marks) usually want input → process → output. Marks go to each correct piece.
1. Read the bullets in the question and tick each one off as you write it.
2. Input each value into a meaningful variable, cast to int or float if it is a number.
3. Use a named constant for any fixed value you’re given, if the question asks for one.
4. Process: the calculation, with brackets where the order matters.
5. Output exactly what is asked, with print.
6. Write in the form asked for: ‘in Python’ means Python, not pseudo-code.
Worked exampleWrite a Python program that asks for a temperature in degrees Celsius (it may have a decimal point) and outputs it in degrees Fahrenheit. F = C × 9 ÷ 5 + 32.
celsius = float(input("Temperature in C: "))it may have a decimal point, so float
fahrenheit = celsius * 9 / 5 + 32* and / happen before +
print(f"{celsius} C is {fahrenheit} F")one output line with both values
celsius = float(input("Temperature in C: "))
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius} C is {fahrenheit} F")
Exam tipAlways attempt code questions. There is usually a mark for one correct input line, and design marks (such as meaningful names) are given even if the program has errors.
Exam tipDon’t add extra features. Extra code adds chances for errors, and any error in code stops you getting full marks.