Looping through an array by index
Loops i through every index of arr, so arr[i] can be read or changed and you know each element's position. Used for totals, finding the largest value and editing in place.
Understanding Looping through an array by index in VCE Software Development U3O1
What it does: The total starts at 0 and each element, read by its index, is added in turn, giving the sum of the array. largest holds the biggest value seen so far and is updated whenever arr[i] beats it, so it ends as the maximum. range() needs a number. len(scores) gives the number of elements, so range(len(scores)) produces the indexes 0 to len-1. arr[i] is the element at position i. Using i alone would add up the index numbers, not the values. Assigning to arr[i] changes the list itself. Changing the loop variable item only changes a copy of the value, so the list stays the same.
Common mistakes: A running total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be total = 0. Array elements are read with square brackets, not round ones. Round brackets try to call marks as a function. It should be biggest = marks[0].
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 72–96) and word search (Modules 72–113).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
for i in range(len(arr)):Fill in the Blanks
Spot the Error
Student with a class code? Join your class →