← VCE Software Development U3O1

Linear search in Python

An algorithm that uses a loop to check each item in a list in turn. It stops when it finds the target or reaches the end of the list, often returning the index or -1.

Understanding Linear search in Python in VCE Software Development U3O1

What it does: Returning i inside the loop ends the function straight away and reports the index where the target was found. range() needs a number, so use len(scores) to get how many items there are. range(scores) would cause a TypeError. Jumping to the middle and halving is binary search. Linear search checks items one at a time from the start. -1 is not a valid index, so it clearly means 'not found'. 0 is the first index, so it would look like a match at the start of the list. == compares two values. A single = assigns a value and causes a SyntaxError inside an if statement.

Common mistakes: A single = assigns a value, so it causes a syntax error inside an if. Use == to compare: if nums[i] == target:. With != the function returns the index of the first item that is NOT the goal. Linear search must check for a match using ==.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 142–155) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which code correctly starts a linear search through the list scores?
for i in range(scores):for i in range(len(scores)):
Matches
if items[i] = target:
↓
Uses = (assignment) instead of == (comparison), which causes a syntax error. It should be ==.
Applying
Meanings
Term
linear search
Meaning
An algorithm that uses a loop to check each item in a list in turn. It stops when it finds the target or reaches the end of the list, often returning the index or -1.
Practice
def linear_search(items, target):
Testing
Gaps
i in range(len(items)):
Check Answer
Spots
2 for i in range(len(nums)):
3 if nums[i] = target:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Horned Lizard' badge — Bug Squasher, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which code correctly starts a linear search through the list scores?
A. for i in range(scores):
B. for i in range(len(scores)): ✓
range() needs a number, so use len(scores) to get how many items there are. range(scores) would cause a TypeError.
2. What does a linear search do?
A. Jumps to the middle of the list and halves the search area each time
B. Checks each item in order until it finds the target or reaches the end ✓
Jumping to the middle and halving is binary search. Linear search checks items one at a time from the start.
3. A linear search finds the target at index 2. What should the function do next?
A. return i straight away ✓
B. keep looping to the end of the list
Linear search stops as soon as it finds the target. Returning immediately ends the function and the loop.
4. Which value does a linear search commonly return when the target is NOT in the list?
A. -1 ✓
B. 0
-1 is not a valid index, so it clearly means 'not found'. 0 is the first index, so it would look like a match at the start of the list.
5. Which comparison checks whether the current item matches the target?
A. if names[i] == target: ✓
B. if names[i] = target:
== compares two values. A single = assigns a value and causes a SyntaxError inside an if statement.

Match the Pairs

for i in range(len(items)): → Loops through every index of the list so each item can be checked in turn
if items[i] == target: → Compares the current item with the value being searched for
When the target is found, the search stops and gives back its position (index) → return i
If the loop finishes without finding the target, the search returns -1 to show it is not in the list → return -1
if items[i] = target: → Uses = (assignment) instead of == (comparison), which causes a syntax error. It should be ==.
for i in range(len(items)): if items[i] == target: return i return -1 → return -1 is inside the loop, so the search gives up after checking only the first item. It should be after the loop, not inside it.

Key Term

linear search
An algorithm that uses a loop to check each item in a list in turn. It stops when it finds the target or reaches the end of the list, often returning the index or -1.

Fill in the Blanks

1. for i in range(len(items)): if items[i] == target: return i
Hint: Type the keyword that starts a loop so every item is checked in turn.
2. for i in range(len(items)): if items[i] == target: return i
Hint: Type the operator that compares two values to see if they are equal.
3. for i in range(len(items)): if items[i] == target: return i return -1
Hint: Type the value returned when the loop ends without finding the target.
4. for i in range(len(items)): if items[i] == target: break
Hint: Type the keyword that stops the loop early once the target is found.
5. for i in range(len(items)): if items[i] == target: return i
Hint: Type the built-in function that gives how many items are in the list.

Spot the Error

Question 1
A def find(nums, target):
B for i in range(len(nums)):
C if nums[i] = target:
D return i
E return -1
Line C — A single = assigns a value, so it causes a syntax error inside an if. Use == to compare: if nums[i] == target:
Question 2
A def find(ids, goal):
B for i in range(len(ids)):
C if ids[i] != goal:
D return i
E return -1
Line C — With != the function returns the index of the first item that is NOT the goal. Linear search must check for a match using ==.
Question 3
A def find(pets, name)
B for i in range(len(pets)):
C if pets[i] == name:
D return i
E return -1
Line A — A function definition must end with a colon. It should be def find(pets, name):
Question 4
A def find(pets, name):
B for i in range(pets):
C if pets[i] == name:
D return i
E return -1
Line B — range() needs a number, not a list. Use len(pets) to get the length: for i in range(len(pets)):
Question 5
A def search(nums, target):
B for i in range(len(nums)):
C if nums[i] == target:
D return target
E return -1
Line D — Linear search should return the index where the target was found. Returning target just gives back the value we already knew. It should be return i.

Student with a class code? Join your class →