Edexcel International Advanced Level Computer Science, point 17.1.1
Unit 4 is a practical Python exam (3 hours, onscreen) with more complex problems than Unit 2. The skill here is choosing the right operator or construct for the job, and combining them correctly. The facts are in 7.1.1 (flow control) and 7.1.3 (operators), so use those pages for the detail. Check the Programming Language Subset (PLS) for what is allowed.
Choosing operators
| Need | Use | Example |
|---|---|---|
| Whole-number division and remainder | // and % |
minutes // 60, n % 10 |
| Powers and roots | ** |
x ** 2, x ** 0.5 |
| Comparing | ==, !=, <, >, <=, >= |
age >= 18 |
| Combining conditions | and, or, not |
a > 0 and b > 0 |
| A range check | chained comparison | 1 <= n <= 10 |
| Testing and changing bits | &, |, ^, <<, >> |
flags & 0b100 |
| Membership | in, not in |
ch in "aeiou" |
| Shorthand updates | +=, -=, *= |
total += price |
Pitfalls: = assigns and == compares. Brackets make and/or mixtures clear. / always gives a float, so use // when you want an integer. Short-circuit evaluation (a and b doesn't evaluate b if a is false) can guard a risky test: if items and items[0] > 5.
Choosing flow control
| Situation | Construct |
|---|---|
| A fixed number of repeats, or every item in a collection | for |
| Repeat until something changes (unknown number of times) | while |
| A loop that must run at least once | while True: with break |
| Choose between actions | if / elif / else |
| Many exclusive values of one variable | if/elif chain, a dictionary, or match |
| Handling errors from input and files | try / except / finally |
| Leave early when the answer is known | return or break |
| Skip an item and go on | continue |
| Repeat the same steps in different places | A subprogram |
Prefer a dictionary over a long elif chain for a lookup:
DAYS = {1: "Mon", 2: "Tue", 3: "Wed"}
print(DAYS.get(2, "?")) # Tue
Worked example
Task: a program reads marks (0 to 100) until the user enters "done", rejects invalid input, and then reports how many passed (50 or more), the highest mark, and the percentage who passed.
PASS_MARK = 50
marks = []
while True: # runs at least once
text = input("Mark (or done): ").strip().lower()
if text == "done":
break # leave the loop
try:
mark = int(text)
except ValueError: # handle bad input
print("Enter a whole number")
continue # skip to the next input
if 0 <= mark <= 100: # chained comparison
marks.append(mark)
else:
print("Must be 0 to 100")
# guard: avoid dividing by zero
if marks:
passed = sum(1 for m in marks if m >= PASS_MARK)
print("Highest:", max(marks))
print("Passed:", passed)
print(f"Pass rate: {passed * 100 // len(marks)}%")
else:
print("No marks entered")
Constructs and operators used:
| Feature | Where |
|---|---|
while True and break |
The input loop |
try/except and continue |
Handling non-numbers |
| Chained comparison | 0 <= mark <= 100 |
| Selection | The range check, and the if marks: guard |
| A count-style loop (a generator) | sum(1 for m in marks if ...) |
// and * |
The integer percentage |
Test it: inputs 70, abc, 30, 150, done give marks [70, 30], highest 70, passed 1, pass rate 50%.
Exam tip: justify your choice when asked ("a while loop, because the number of inputs isn't known"). Test boundary values (0, 100, 50). Watch for division by zero and empty lists.
Check yourself
Which loop would you use to read input until the user types "quit"?
A while loop (condition-controlled), because the number of repeats isn't known.
How do you write "n is between 1 and 10 inclusive" in Python?
1 <= n <= 10.
What does continue do in a loop?
It skips the rest of the current pass and goes straight to the next one.
Why check if marks: before dividing by len(marks)?
An empty list has length 0, so dividing would cause a ZeroDivisionError.