← Back to subjects
0
Edexcel GCSE Computer Science (1CP2) · Programming
Mini-Lesson

Programming

This mini-lesson covers the core of writing programs (in Python-style code): data types, variables & constants, the three programming constructs sequence, selection & iteration, operators, arrays/lists, subprograms, string manipulation and file handling.

data & variables operators sequence · selection · iteration lists · subprograms strings · files turning an algorithm into working code

Work through each screen, answer the questions (some code-reading, some calculations) and collect ⭐ stars. Press Start when you're ready.

Programming · data types

Data types

Every value has a data type that tells the computer how to store and use it:

  • Integer — a whole number, e.g. 42, -7.
  • Real / float — a number with a decimal point, e.g. 3.14.
  • Boolean — only True or False.
  • Character — a single symbol, e.g. 'A'.
  • String — a sequence of characters, e.g. "hello".

Watch out: "42" (in quotes) is a string, not an integer — you can't do arithmetic on it until you convert it with something like int("42"). Choosing the right type is called casting when you convert.

Quick check

Which data type?

?A variable stores whether a user is logged in: it is either True or False. Which data type is best?
Programming · variables & constants

Variables & constants

  • A variable is a named store whose value can change while the program runs, e.g. score = score + 1.
  • A constant is a named value that must not change, e.g. VAT = 0.20 or PI = 3.14159. Using a constant makes code clearer and easier to update.
price = 50
VAT = 0.2   # constant
total = price + price * VAT# assignment uses =, and works right-hand-side first

Assignment: total = price + 10 means "work out the right-hand side, then store it in total". The = sign is assignment, not "equals" in the maths sense.

Programming · operators

Operators

  • Arithmetic: + − * /, plus ** (power), // (integer division / DIV) and % (remainder / MOD).
  • Comparison: == (equal to), != (not equal), <, >, <=, >=.
  • Logical: AND, OR, NOT — combine conditions.
Worked example — DIV and MOD

17 // 5 = 3 (how many whole 5s fit in 17)

17 % 5 = 2 (the remainder left over)

Common trap: = assigns a value; == tests whether two values are equal. Mixing them up is a classic bug.

Calculate

Your turn — remainder (MOD)

1What is the result of 23 % 4 (the remainder when 23 is divided by 4)?
Hint: 4 × 5 = 20, and 23 − 20 = 3.
Programming · the three constructs

Sequence, selection & iteration

Every program is built from three constructs:

  • Sequence — statements run in order, one after another.
  • Selection — choose a path using IF / ELIF / ELSE.
  • Iteration (loops) — repeat code. A FOR loop repeats a set number of times; a WHILE loop repeats while a condition is true (a condition-controlled loop).
total = 0
for i in range(1, 6):  # i = 1,2,3,4,5
  total = total + i
print(total)  # 15range(1, 6) counts 1 up to but NOT including 6.

FOR vs WHILE: use FOR when you know how many repeats; use WHILE when you repeat until something happens (e.g. until the user types "quit").

Trace it

Your turn — how many times?

2How many times does the body of this loop run: for i in range(0, 10)? (range(0, 10) gives 0 up to but not including 10.)
times
Hint: the values are 0,1,2,3,4,5,6,7,8,9 — count them.
Quick check

Read the code

?Given age = 17, what does this print?
if age >= 18: print("adult") else: print("minor")
Programming · arrays & lists

Arrays & lists

An array (a list in Python) stores many values under one name. Each value has a position number called its index, starting at 0.

"cat" "dog" "fox" "owl" index 0 index 1 index 2 index 3 animals[2] → "fox"
animals = ["cat","dog","fox","owl"] — the first item is index 0, so animals[2] is "fox".

Off-by-one trap: a list of 4 items has indexes 0 to 3. Asking for index 4 gives an out-of-range error.

Sort it

What data type is each value?

Tap a value, then tap its data type.

🔤 String

🔢 Integer

✅ Boolean

Calculate

Your turn — the last index

3A list scores holds 8 values. What is the index of the last item?
Hint: indexes start at 0, so 8 items run 0 to 7.
Programming · subprograms

Subprograms: functions & procedures

A subprogram is a named block of code you can call whenever you need it. This supports decomposition and avoids repeating code.

  • A function returns a value, e.g. area(w, h) returns w × h.
  • A procedure does a job but doesn't return a value (in Python both use def).
  • Values passed in are parameters/arguments; the answer sent back uses return.
def area(w, h):
  return w * h
print(area(4, 5))  # 20area is a function: it takes parameters w and h and returns a value.

Benefits: subprograms make code easier to read, test and reuse, and let a team work on different parts.

Programming · string manipulation

String manipulation

Strings can be measured and sliced:

  • Length: len("cat")3.
  • Index a character: "cat"[0]"c".
  • Slice/substring: "computer"[0:4]"comp" (positions 0,1,2,3).
  • Join (concatenate): "AB" + "CD""ABCD".
  • Change case: "hi".upper()"HI".

Slicing rule: [start:end] includes start but stops before end. So [0:4] gives 4 characters, not 5.

Quick check

Read the string code

?What does len("Edexcel") return?
Programming · file handling

File handling

Programs can read from and write to text files so data is saved permanently (not lost when the program closes). Typical steps:

  • Open the file, choosing a mode: read ("r"), write ("w", overwrites) or append ("a", adds to the end).
  • Read a line or write a line of text.
  • Close the file to save changes and free it.
f = open("scores.txt", "w")
f.write("Ada,20\n")
f.close()"w" overwrites the whole file; "a" would add to the end instead.

Careful: opening in "w" mode deletes the old contents. Use "a" (append) if you want to keep what's already there.

Quick check

Which file mode?

?You want to add a new high score to the end of a file without deleting the existing scores. Which mode should you open it in?
Match it

Match the description to the term

Tap a description on the left, then its matching term on the right.

Description
Term
Quick check

Which loop?

?You want to keep asking for a password until the user types the correct one — you don't know how many tries it will take. Which loop suits this best?
Quick check

Function or procedure?

?A subprogram works out and returns the average of a list so the answer can be stored in a variable. What is it?
Recap

The big ideas to know

Data types: integer · real · Boolean · character · string

Variables change; constants don't · = assigns, == compares

Operators: + − * / ** // % · comparison · AND/OR/NOT

Constructs: sequence · selection (IF) · iteration (FOR/WHILE)

Lists: index from 0 · subprograms: functions return, procedures don't

Strings: len, index, slice [start:end] · files: open/read/write/close, "w" overwrites, "a" appends

You've covered the whole of Edexcel Programming. Press Finish to see your score.

🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Programming for Edexcel GCSE Computer Science. 🎉

Your stars: 0 / 0

Next: test yourself in the Evaluate stage Confidence Quiz, then lock it in with Verify.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to all subjects