Learning route
Follow the index. Handle the missing item.
Prerequisite: Can trace a while loop, use a Boolean condition and distinguish a counter from a total. Complete Lesson 02 first if needed.
- Read and update a list using zero-based indexing.
- Trace a linear search and count item comparisons.
- Return the first matching index or a clear not-found result.
- Test first, last, missing and duplicate-item cases.
Retrieve and explain
- List the values produced by
range(4). - Why does a while loop need an update?
- State the result of
not False. - In a trace table, why must you say whether a row is before or after an update?
A list stores an ordered sequence of values. An index is the position used to access an item. Python uses index 0 for the first item. For a non-empty list of length n, its last non-negative index is n − 1.
names = ["Asha", "Ben", "Cara", "Dev"]
print(names[0])
print(len(names))
names[1] = "Bilal"
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Before update | Asha | Ben | Cara | Dev |
| After update | Asha | Bilal | Cara | Dev |
The printed values are Asha and 4. names[4] is outside this list and raises IndexError. len(names) is a count, not the last index. Python also supports negative indices, but this lesson uses non-negative indices throughout.
for index in range(len(names)):
print(index, names[index])
Searching does not require sorting.
A linear search checks items in order until it finds the target or reaches the end. It works on an unsorted list. Binary search is a different algorithm and requires suitably sorted data; it is not the focus of this lesson.
Worked example
Find the first occurrence of target 27 in the unsorted list below. We keep a Boolean found and an index. Stop immediately when a match is found.
numbers = [14, 8, 27, 8, 31]
target = 27
index = 0
found = False
while index < len(numbers) and not found:
if numbers[index] == target:
found = True
else:
index = index + 1
if found:
print("Found at index", index)
else:
print("Not found")
| Index compared | Value | Match? | State after body |
|---|---|---|---|
| 0 | 14 | No | index = 1, found = False |
| 1 | 8 | No | index = 2, found = False |
| 2 | 27 | Yes | index = 2, found = True |
Output: Found at index 2. There are three item-to-target comparisons. On a match, index does not advance; this preserves the matching index for the output.
Protect the list boundary.
The loop checks index < len(numbers) before accessing numbers[index] inside its body. For an empty list the body runs zero times and the program outputs Not found.
Repair practice
The following code searches for an absent item. Find the boundary error and repair it.
items = [4, 7, 9]
target = 5
index = 0
while index <= len(items):
if items[index] == target:
print("Match")
index = index + 1
State the first invalid index, explain why it is invalid, and change one operator to avoid the crash. Then explain why this small repair still does not make a complete first-match search with a not-found message.
Repair feedback
The first invalid index is 3, because the valid non-negative indices are 0–2. Change <= to <. This prevents the out-of-range access. It still checks every item, can output multiple matches, and never prints a not-found message. A found flag, suitable stopping condition and final output decision are needed to meet the first-match requirement.
Independent practice
Independent build: club code lookup. Use codes = [104, 208, 315, 208, 422]. Read an integer target. Implement a linear search that stops at the first match. Output Found at index followed by the zero-based index, or Not found. Also output Comparisons: followed by the number of item-to-target comparisons.
- Use an index and a Boolean flag. Do not use
inor.index()for the search. - Increment a comparison counter only when comparing a list item with the target.
- Protect the list boundary before accessing an item.
- Test 104, 422, 999 and 208. Then temporarily replace the list with
[]and test again.
Explain why a human saying “the second item” corresponds to Python index 1.
# Proof Academy | Lesson 03 | Club code lookup
codes = [104, 208, 315, 208, 422]
target = int(input("Target code: "))
index = 0
found = False
comparisons = 0
# Your task: linear search; stop at the first match.
# Your task: output Found at index ... or Not found.
# Your task: output Comparisons: ...
Model program and marking guidance
# Proof Academy | Lesson 03 | Worked solution
codes = [104, 208, 315, 208, 422]
target = int(input("Target code: "))
index = 0
found = False
comparisons = 0
while index < len(codes) and not found:
comparisons = comparisons + 1
if codes[index] == target:
found = True
else:
index = index + 1
if found:
print("Found at index", index)
else:
print("Not found")
print("Comparisons:", comparisons)
- Reads an integer target and initialises index, found and comparison count. [1]
- Checks the list boundary before accessing an item. [1]
- Compares successive items to the target and advances the index after a non-match. [1]
- Stops at the first match, preserving its index. [1]
- Correct found/not-found output, including the empty-list case. [1]
- Counts and displays item-to-target comparisons accurately. [1]
- Records correct expected results for first, last, missing, duplicate and empty-list cases. [1]
- Runs those five cases and records actual results, repairing mismatches. [1]
Exam practice
Original Proof Academy practice · 6 marks. Use the first-match algorithm from the worked example with numbers = [9, 3, 9, 6].
- For target 9, state the returned index and the number of item comparisons. [2]
- For target 7, state the result and the number of item comparisons. [2]
- Explain why a linear search can be used on this unsorted list. [1]
- State the maximum number of item comparisons for a list of n items using this algorithm. [1]
Worked feedback
Target 9: index 0 (1), one comparison (1). Target 7: Not found (1), four comparisons (1). Linear search examines items sequentially without relying on their order (1). Maximum n comparisons (1), reached by an absent target or a first occurrence at the final index. An empty list needs zero item comparisons. Do not award a comparison mark for the number of loop-condition checks.
Homework and feedback
30 minutes: 5 minutes retrieval, 15 minutes adapting the search, 10 minutes testing.
Adapt the club lookup to count all occurrences of a target. Use codes = [104, 208, 315, 208, 422]. Output Matches: and the count. Test 208, 104 and 999, then an empty list. Explain why the algorithm must continue after finding a match.
Homework feedback
codes = [104, 208, 315, 208, 422]
target = int(input("Target code: "))
matches = 0
for index in range(len(codes)):
if codes[index] == target:
matches = matches + 1
print("Matches:", matches)
208 → 2; 104 → 1; 999 → 0; empty list → 0. Every item must be checked because another match may occur later. This changes the requirement from first-match lookup to counting all matches. Accept iteration directly over the values for this homework.
Sources and further practice
This chapter is written for Pearson Edexcel GCSE Computer Science 1CP2. Use the specification references in the chapter heading to locate the relevant requirements. Practice questions and point allocations on this page are original Proof Academy teaching material unless a question is explicitly identified as a sourced past-paper bridge.
- Pearson Edexcel 1CP2 specification
- PMT Paper 1 papers and mark schemes
- PMT Paper 2 papers and mark schemes
When using an original paper, open its matching mark scheme and supplied coding files where required. Use that paper's instructions and marks. A short classroom exercise is not a substitute for a full official task.
Trace lab · inline, no inner scroll
First-match search
Search [14, 8, 27, 8, 31]. Predict the printed result. The first 8 is index 1.
Keep your evidence
Export your notes before changing devices.
Notes stay in this browser. The Quiz tab records the chapter check, and Resources holds the long-answer questions.