Academy Computer Science Programming fundamentals

Paper 2 · J277/02 · unit 2.2

Programming fundamentals

Sequence, selection, iteration, types, strings, arrays, files, functions, SQL, random. Exercises you can do in Python or on paper. Quiz is open.

Everything on this course is open. Skip, jump, retry. Nothing is locked and there is no required order.

The vocabulary of Paper 2. Write it in Python on this page if you want — the exam may show ERL. Same ideas. Open 2.2.3 first if you already have loops.

2.2.1 Programming fundamentals

Variables and constants

A variable is a named store whose value can change. A constant is named but must not change (ERL: const VAT = 0.2). Named constants stop magic numbers spreading.

Assignment copies a value: score = 0. The right-hand side is worked out first.

Input / output

name = input("Name?")
print("Hi " + name)

Operators

  • Arithmetic: + - * / and modulo MOD (% in Python) — remainder; DIV or // — integer division
  • Comparison: == != < > <= >=
  • Boolean: AND OR NOT

The three constructs

Every program is built from:

  1. Sequence — one statement after another
  2. Selectionif / else if / else
  3. Iteration — loops

Count-controlled: you know how many times (for i in range(10) / for i = 0 to 9).
Condition-controlled: until a condition fails (while lives > 0).

Nesting: a loop inside a loop, or an if inside a for. OCR added an explicit “use of nesting” note — they will ask.

for row = 1 to 3
  for col = 1 to 3
    print(row, col)
  next col
next row

That prints nine pairs. Trace it once.

2.2.2 Data types

Type Holds Example
Integer Whole numbers -3, 0, 42
Real / float Decimals 3.14
Boolean True or False True
Character One symbol 'A'
String Text "Lewis"

Casting converts: int("7"), str(7), float("1.5"). Concatenating a number onto a string without casting is a classic Paper 2 trap.

Why types matter: 3 + 2 is 5; "3" + "2" is "32".

2.2.3 Additional programming techniques

Strings

Length, position, slicing, upper/lower, joining.

Python: s[0], s[1:4], len(s), s.upper(), s + t.

ERL-style: left(s, 3), right(s, 2), substring(s, 2, 3) — learn the idea: start and length.

Arrays (1D and 2D) and records

A 1D array is a numbered list of the same type. Index usually from 0 in Python; ERL questions will say.

A 2D array is a grid: grid[row][col]. Nested loops walk them.

A record groups different types under field names: pupil.name, pupil.year. In Python you might use a dict or a small class — the idea is the field names.

Files

Open, read, write, close. Always think: what if the file is missing? (That’s 2.3.)

file = open("scores.txt", "r")
line = file.readline()
file.close()

Write mode "w" overwrites. Append "a" adds.

Functions and procedures

  • Procedure: a named block of code — does a job, may have parameters, typically no return
  • Function: returns a value

Parameters are the inputs in the definition; arguments are the values you pass. Scope: a variable created inside a function is local — invisible outside.

function add(a, b)
  return a + b
endfunction

Passing an array still needs care: in Python the list is mutable; in ERL, follow the question.

SQL

A tiny language for tables.

SELECT name, year FROM pupils WHERE year = 9
  • SELECT columns (* means all)
  • FROM table
  • WHERE filter
  • Sometimes ORDER BY

SQL injection is 1.4 — here you only need to read and write simple queries.

Random numbers

random(1, 6) in ERL, random.randint(1, 6) in Python. Games and simulations. Seed is beyond GCSE unless they mention it.


The programming lesson repeats these in longer Python katas. Use either page; neither is locked behind the other.

Practice · optional · answers on this page

Exercises

Do as many or as few as you like, in any order. Hints and a model answer sit under each task.

2.2.1 · e1 python

Fizz-ish

Write a program that prints numbers 1 to 20. If a number is a multiple of 3, print "Tyneside" instead. Multiple of 5: print "Academy". Both: print "Tyneside Academy". Use a count-controlled loop and nested/compound conditions.
Hint
n % 15 == 0 first, or check both flags. range(1, 21).
Show a model answer
for n in range(1, 21): if n % 15 == 0: print("Tyneside Academy") elif n % 3 == 0: print("Tyneside") elif n % 5 == 0: print("Academy") else: print(n)
2.2.1 · e2 paper

Nested loop trace

How many times is print called? for i = 1 to 4 for j = 1 to 3 print(i * j) next j next i
Hint
Outer 4, inner 3, independent.
Show a model answer
12 times (4 × 3).
2.2.2 · e3 paper

Type the result

Give the type and value if it runs, or say error: 1. 7 + 2 2. "7" + "2" 3. int("7") + 2 4. "score: " + 7
Hint
Casting. Python will error on 4 unless you str(7).
Show a model answer
1. integer 9 2. string "72" 3. integer 9 4. error in Python (must cast 7 to str) — exam ERL may want concat with a conversion
2.2.3 · e4 python

1D then 2D

1. A list of 5 scores. Print the highest without using max() — a loop. 2. A 3×3 grid of "." . Set the centre to "X" and print each row.
Hint
Keep a running best. grid[1][1] is the centre of a 0-indexed 3×3.
Show a model answer
scores = [4, 9, 1, 9, 2] best = scores[0] for s in scores: if s > best: best = s print(best) grid = [["."] * 3 for _ in range(3)] grid[1][1] = "X" for row in grid: print("".join(row))
2.2.3 · e5 python

Function + file

Write a function average(nums) that returns the mean of a list of integers (empty list → return 0). Then read integers from a file numbers.txt (one per line) and print the average.
Hint
Guard empty. Strip lines. int().
Show a model answer
def average(nums): if len(nums) == 0: return 0 total = 0 for n in nums: total = total + n return total / len(nums) nums = [] f = open("numbers.txt", "r") for line in f: line = line.strip() if line != "": nums.append(int(line)) f.close() print(average(nums))
2.2.3 · e6 paper

SQL

Table games: title, genre, year 1. All titles of genre "arcade". 2. All columns for games from 2024.
Hint
SELECT ... FROM ... WHERE ...
Show a model answer
1. SELECT title FROM games WHERE genre = "arcade" 2. SELECT * FROM games WHERE year = 2024

Check yourself · not a gateway

Quiz

Mark it, reveal it, or skip it. A low score does not close anything. Try again as often as you want.

1 2.2.1 A count-controlled loop is used when…
2 2.2.1 Nesting means…
3 2.2.2 Which data type is appropriate for “has the user paid?”
4 2.2.3 A function differs from a procedure because a function…
5 2.2.3 Opening a file in write mode ("w") typically…
6 2.2.3 SELECT name FROM pupils WHERE year = 8 returns…
7 2.2.2 The expression "3" + "4" gives the integer 7 in Python.
8 2.2.3 Which are true of a local variable inside a function? (Select all that apply)