Programming carries marks in both papers, and many students find it the most stressful part of the syllabus. Here is the good news about Python for A/L ICT: exam questions are built from a small number of patterns that keep appearing. Once you can write these seven from memory, most programs in past papers become combinations of things you already know.
All examples use Python 3, as in Unit 9: Programming.
Pattern 1: Running total and average
Read values until a stop value is entered, keeping a total and a count.
total = 0
count = 0
mark = int(input("Enter mark (-1 to stop): "))
while mark != -1:
total = total + mark
count = count + 1
mark = int(input("Enter mark (-1 to stop): "))
if count > 0:
print("Average:", total / count)
Notice the two input() lines: one before the loop and one at the end of the loop. This "read ahead" structure is a classic exam pattern. Also note the check count > 0, which avoids dividing by zero when no marks are entered.
Pattern 2: Finding the largest (or smallest) value
marks = [67, 45, 89, 72, 58]
highest = marks[0]
for m in marks:
if m > highest:
highest = m
print(highest)
Here is the trace table for this program. Every variable gets a value on every row:
| m | m > highest? | highest |
|---|---|---|
| – | – | 67 |
| 67 | False | 67 |
| 45 | False | 67 |
| 89 | True | 89 |
| 72 | False | 89 |
| 58 | False | 89 |
Output: 89. To find the smallest value, change > to <.
Tip: Start with the first element, highest = marks[0], rather than highest = 0. Starting at 0 fails when every value is negative.
Pattern 3: Counting items that meet a condition
marks = [67, 45, 89, 72, 58]
passed = 0
for m in marks:
if m >= 50:
passed = passed + 1
print(passed)
Output: 4. Questions often combine this with Pattern 1, for example "count the passes and find the average".
Pattern 4: Linear search
names = ["Nimal", "Sara", "Kavin", "Amaya"]
target = "Kavin"
found = False
for i in range(len(names)):
if names[i] == target:
print("Found at index", i)
found = True
break
if not found:
print("Not found")
Output: Found at index 2. Remember that list indexes start at 0. The found flag lets the program report "Not found" only after the loop has checked every item.
Pattern 5: Processing a string character by character
word = "Information"
vowels = 0
for ch in word.lower():
if ch in "aeiou":
vowels = vowels + 1
print(vowels)
Output: 5. Converting with lower() first means capital vowels are counted too. The same loop can count digits, spaces or a particular letter.
Pattern 6: Functions with parameters and return values
def grade(mark):
if mark >= 75:
return "A"
elif mark >= 65:
return "B"
elif mark >= 50:
return "C"
elif mark >= 35:
return "S"
else:
return "F"
print(grade(68))
Output: B. The order of the conditions matters: check the highest boundary first. If you checked mark >= 35 first, every pass would be graded S.
Watch out: return sends a value back to the caller; print only displays it. If a question asks a function to "return" a value, using print instead loses marks.
Pattern 7: Reading from and writing to a file
# marks.txt contains lines such as: Nimal,72
with open("marks.txt", "r") as f:
for line in f:
name, mark = line.strip().split(",")
print(name, int(mark))
with open("result.txt", "w") as out:
out.write("Processing complete\n")
strip() removes the newline at the end of each line, and split(",") separates the fields. Mode "r" reads, "w" writes a new file (replacing any old content) and "a" adds to the end.
Python for A/L ICT: putting the patterns together
When you meet a new programming question, ask: "Which patterns does this need?" A question about a class's results might need Pattern 7 to read the file, Pattern 1 for the average, Pattern 2 for the top mark and Pattern 6 to convert marks to grades. Write each part separately, then join them.
Exam note: In the exam you write code on paper. Keep indentation consistent (four spaces), use meaningful variable names and trace your program with a small example before moving on.
Practise until it feels automatic
Write each pattern from memory on paper, then trace it. After that, work through the programming questions in our past papers and identify the patterns in each one. itguru.lk students can also practise in BattlePython inside Nexus.


