GuruBytes•Study Guides

Python for A/L ICT: 7 Code Patterns You Must Know

Most A/L ICT programming questions are built from a handful of patterns: totals, maximums, counting, searching, strings, functions and files. Learn these seven and new questions start to look familiar.

An A/L ICT student traces a Python loop by hand in a notebook beside a laptop.

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".

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.

Frequently asked questions

Which version of Python is used for A/L ICT?

Python 3. Use print() with brackets, input() for input, and remember that input() always returns a string, so convert it with int() or float() when you need a number.

How should I practise Python for the exam?

Write programs by hand on paper as well as on a computer, because you will not have a computer in the exam. Trace past paper programs with a table and compare your output with the marking scheme.

Do I need to memorise Python code?

No. Understand the patterns in this guide instead. Most exam programs combine two or three of them, so if you can write each pattern from memory you can build the program the question asks for.

You've reached the end

Found this useful? Share it with a friend or a parent who needs it.

Share:WhatsAppFacebook
Up nextHow the A/L ICT Syllabus Evolved: From 2009 to the 2017 "New Syllabus"

Study A/L ICT with Teran Subasinghe

English medium A/L ICT classes at Sasip Nugegoda, Shakthi Bambalapitiya and online island-wide, with recordings, revision games and past paper discussions.

#python#programming#unit 9#trace tables#paper 2
Ask GuruBotQuestions about classes