Intermediate

Variables, Constants and Data Types

AicademyAicademy
·GCSE Computer Science·AQA 8525·9 min
3.2.1 Data types·3.2.2 Programming concepts·3.2.3 Arithmetic operations·3.2.4 Relational operations·3.2.5 Boolean operations·3.2.7 Input/output·3.2.8 String handling operations·3.2.9 Random number generation

Data Types

Every value stored in a program has a data type that tells the computer how to interpret the bits. Using the wrong data type causes programs to produce incorrect results or fail entirely.

AQA requires knowledge of five data types:

Data typeWhat it storesExample valuesPython name
IntegerWhole numbers, positive or negative-3, 0, 42int
RealNumbers with a decimal point3.14, -0.5, 100.0float
BooleanLogical true or false onlyTRUE, FALSEbool
CharacterA single symbol'A', '7', '!'str (length 1)
StringA sequence of zero or more characters"hello", "AQA", ""str

Data type names vary by programming language. AQA pseudocode uses the terms above; Python uses int, float, bool, and str. Both sets of names appear in exam questions — know both.

Why data type matters: storing age as a string instead of an integer means arithmetic on it fails. Storing a price as an integer instead of a real loses the pence. Choosing the correct type is part of correct program design, not a cosmetic detail.

Variables and Constants

A variable is a named location in memory that stores a value which can change during the program's execution. A constant is a named location whose value is fixed at the start and never changed.

score ← 0           # variable — changes as the program runs
MAX_SCORE ← 100     # constant — set once, never modified
score = 0
MAX_SCORE = 100     # Python has no enforced constant keyword; use ALL_CAPS by convention

Why use named constants?

Without a constantWith a constant
IF score > 100 written in 12 placesIF score > MAX_SCORE written in 12 places
Changing the maximum requires 12 editsChanging the maximum requires 1 edit
Easy to introduce inconsistency with a typoGuaranteed consistency everywhere

Meaningful identifier names are required by the spec. x and t1 are poor; studentScore and maxAttempts make code self-documenting. Examiners expect well-named identifiers in pseudocode answers — vague names like a or temp lose marks when the name is part of the mark scheme.

Assignment uses in AQA pseudocode and = in Python. The value on the right is evaluated first, then stored in the variable on the left: count ← count + 1 adds 1 to the current value and stores the result back.

Arithmetic Operators

Six arithmetic operators are required for the specification:

OperatorMeaningAQA pseudocodePythonExampleResult
AdditionAdd two values++7 + 310
SubtractionSubtract right from left--7 - 34
MultiplicationMultiply two values**7 * 321
Real divisionDivide, keeping decimal//7 / 23.5
Integer divisionQuotient only, no remainderDIV//7 DIV 23
ModuloRemainder only, no quotientMOD%7 MOD 21

DIV and MOD are frequently tested. Two particularly useful patterns:

  • n MOD 2 = 0n is even; n MOD 2 = 1n is odd
  • n DIV 10 removes the rightmost digit: 157 DIV 10 = 15
  • n MOD 10 extracts the rightmost digit: 157 MOD 10 = 7

Worked example — A cinema has 150 seats in rows of 12. How many complete rows, and how many seats remain?

seats ← 150
rowSize ← 12
fullRows ← seats DIV rowSize    # 12
leftOver ← seats MOD rowSize    # 6
OUTPUT STR(fullRows) & " full rows, " & STR(leftOver) & " seats left"

Random number generation is also in §3.2.9: RANDOM_INT(a, b) in AQA pseudocode (Python: random.randint(a, b)) returns a random integer between a and b inclusive. Understanding the pseudo-random mechanism behind it is explicitly not required.

Relational and Boolean Operators

Relational operators compare two values and always produce a Boolean result:

Operator meaningAQA pseudocodePythonExample
Equal to===5 = 3 → FALSE
Not equal to!=5 ≠ 3 → TRUE
Less than<<5 < 3 → FALSE
Greater than>>5 > 3 → TRUE
Less than or equal to<=5 ≤ 5 → TRUE
Greater than or equal to>=4 ≥ 5 → FALSE

Boolean operators combine relational expressions into compound conditions:

OperatorMeaningResult
ANDBoth conditions must be TRUEage ≥ 18 AND hasID = TRUE
ORAt least one condition must be TRUEcolour = "red" OR colour = "blue"
NOTInverts the Boolean valueNOT gameOver is TRUE when gameOver is FALSE

Worked example — Award a merit if a score is between 60 and 79 inclusive:

IF score ≥ 60 AND score ≤ 79 THEN
    OUTPUT "Merit"
ENDIF

Relational operators bind before Boolean operators: score > 60 AND score < 80 evaluates as (score > 60) AND (score < 80). Writing score > 60 AND 80 is a logic error — it does not work as intended.

Worth saving these ideas?

Turn what you've read into instant revision cards. Free to get started.

Make flashcards

Input, Output and Type Conversion

Programs receive data from users with INPUT / USERINPUT and display results with OUTPUT / print().

OUTPUT "Enter your name: "
name ← USERINPUT
OUTPUT "Hello, " & name
name = input("Enter your name: ")
print("Hello, " + name)

input() in Python always returns a string, even when the user types a number. Arithmetic on an unconverted string causes a TypeError. Always convert before arithmetic.

Type conversion functions:

ConversionAQA pseudocodePython
String → integerINT(x)int(x)
String → realREAL(x)float(x)
Integer → stringSTR(x)str(x)
Real → stringSTR(x)str(x)

Worked example — Read two numbers and output their sum:

a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)

Without int(), "3" + "5" gives "35" (string concatenation), not 8 (addition). This is one of the most common runtime errors beginners make.

String Handling Operations

Strings are sequences of characters. AQA requires six string operations:

OperationMeaningAQA pseudocodePython
LengthNumber of charactersLEN(str)len(str)
PositionIndex of first occurrence of a characterPOSITION(str, char)str.index(char)
SubstringExtract a portionSUBSTRING(str, start, length)str[start : start+length]
ConcatenationJoin two stringsstr1 & str2str1 + str2
Char to codeASCII value of a characterASC(char)ord(char)
Code to charCharacter from an ASCII valueCHR(code)chr(code)

String indices start at 0 in AQA pseudocode — the first character is at position 0. This matches Python.

Worked example — Extract the area code from a postcode:

postcode ← "SW1A 2AA"
area ← SUBSTRING(postcode, 0, 3)
OUTPUT area        # SW1

Worked example — Check whether two characters are adjacent in the alphabet:

diff ← ASC("C") - ASC("B")    # 1 — adjacent
OUTPUT diff
diff ← ASC("E") - ASC("B")    # 3 — not adjacent
OUTPUT diff

ASC("A") returns 65, ASC("Z") returns 90, ASC("a") returns 97. Upper and lower case have different codes.

Common Exam Mistakes

1. Treating input() output as a number without converting

input() in Python always returns a string. int(input(...)) converts it immediately. Forgetting this causes TypeError on arithmetic, or silent concatenation instead of addition.

2. Using / when integer division is required

7 / 2 gives 3.5. If the question requires a whole number result (e.g. how many complete groups), use DIV (// in Python) to get 3. Using real division and hoping the decimal part is zero is unreliable.

3. Confusing = assignment with = comparison in pseudocode

In AQA pseudocode, is assignment and = is comparison. Writing score = 5 inside an IF condition means "is score equal to 5?" — this is correct. Writing score = 5 as a standalone statement to assign a value is wrong; it should be score ← 5.

4. Boolean operator precedence errors

score > 60 AND 80 does not check whether score is between 60 and 80. It evaluates (score > 60) AND 80, which is a type error. The full condition must be: score > 60 AND score < 80.

5. Off-by-one in SUBSTRING

SUBSTRING("Hello", 0, 3) extracts 3 characters starting at index 0: "Hel". The second argument is the length, not the end index. Expecting "Hell" (4 characters) is an off-by-one error.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Next

Selection and Iteration

Related lessons

7 Slides

Lesson

Boolean Logic: Gates and Truth Tables

GCSE Computer Science · AQA 8525

10 days ago

7 Slides

Lesson

Selection and Iteration

GCSE Computer Science · AQA 8525

5 days ago

7 Slides

Lesson

Subroutines: Procedures and Functions

GCSE Computer Science · AQA 8525

5 days ago