Looping through an array by index

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Looping through an array by index

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop visits every index of the list scores?
A for i in range(scores):
B for i in range(len(scores)):
2. A list arr has 5 elements. Which values does i take in: for i in range(len(arr)):
A 0, 1, 2, 3, 4
B 1, 2, 3, 4, 5
3. Which code adds up every element of arr into total?
A for i in range(len(arr)): total = total + i
B for i in range(len(arr)): total = total + arr[i]
4. Which line finds the largest element so far inside the loop?
A if arr[i] > largest:
B if i > largest:
5. Which code doubles every element of arr in place?
A for i in range(len(arr)): arr[i] = arr[i] * 2
B for item in arr: item = item * 2

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for i in range(len(arr)): ALoops i through every index of arr, from 0 to the last position
2arr[i] = arr[i] * 2 Brange() needs a number, but arr is a list. It should be range(len(arr)) so i covers every index.
3Totalling array elements: start a total at 0, then add arr[i] on every pass Clargest = arr[0] for i in range(len(arr)): if arr[i] > largest: largest = arr[i]
4Finding the largest element: start with the first element and replace it whenever arr[i] is bigger DAdds the index i instead of the element arr[i], so the total is of positions, not values. It should be total = total + arr[i].
5for i in range(len(arr)): total = total + i Etotal = 0 for i in range(len(arr)): total = total + arr[i]
6for i in range(arr): arr[i] = arr[i] + 1 FChanges the element at position i in place by doubling it

Definitions

for i in range(len(arr)):

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for i in _____(len(arr)):
2.
total = 0 for i in range(___(arr)): total = total + arr[i]
3.
largest = arr[0] for i in range(len(arr)): if arr[i] _ largest: largest = arr[i]
4.
for i in range(len(arr)): arr[_] = arr[i] * 2
5.
total = 0 for i in range(len(arr)): total __ arr[i]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A scores = [12, 8, 15, 9]
B total = 1
C for i in range(len(scores)):
D total = total + scores[i]
E print(total)
Question 2
A marks = [56, 91, 73, 84]
B biggest = marks(0)
C for i in range(len(marks)):
D if marks[i] > biggest:
E biggest = marks[i]
Question 3
A prices = [10, 20, 30]
B for i in range(prices):
C prices[i] = prices[i] * 2
D print(prices)
Question 4
A temps = [21, 25, 19, 30]
B total = 0
C for i in range(len(temps)):
D total = total + i
E average = total / len(temps)
Question 5
A nums = [4, 9, 2, 7]
B for i in range(len(nums)):
C nums = nums[i] + 1
D print(nums)

Answer Key

Looping through an array by index
Section A: Quick Questions
Q1
B — for i in range(len(scores)):
Q2
A — 0, 1, 2, 3, 4
Q3
B — for i in range(len(arr)): total = total + arr[i]
Q4
A — if arr[i] > largest:
Q5
A — for i in range(len(arr)): arr[i] = arr[i] * 2
Section B: Match the Pairs
Q1
for i in range(len(arr)): → Loops i through every index of arr, from 0 to the last position
Q2
arr[i] = arr[i] * 2 → Changes the element at position i in place by doubling it
Q3
Totalling array elements: start a total at 0, then add arr[i] on every pass → total = 0 for i in range(len(arr)): total = total + arr[i]
Q4
Finding the largest element: start with the first element and replace it whenever arr[i] is bigger → largest = arr[0] for i in range(len(arr)): if arr[i] > largest: largest = arr[i]
Q5
for i in range(len(arr)): total = total + i → Adds the index i instead of the element arr[i], so the total is of positions, not values. It should be total = total + arr[i].
Q6
for i in range(arr): arr[i] = arr[i] + 1 → range() needs a number, but arr is a list. It should be range(len(arr)) so i covers every index.
Section C: Definitions
Q1
for i in range(len(arr)): — 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.
Section D: Fill in the Blanks
Q1
for i in range(len(arr)):
Q2
total = 0 for i in range(len(arr)): total = total + arr[i]
Q3
largest = arr[0] for i in range(len(arr)): if arr[i] > largest: largest = arr[i]
Q4
for i in range(len(arr)): arr[i] = arr[i] * 2
Q5
total = 0 for i in range(len(arr)): total += arr[i]
Section E: Spot the Error
Q1
B — A running total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be total = 0.
Q2
B — 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].
Q3
B — range() needs a whole number, but prices is a list. Use len(prices) to get the number of elements: for i in range(len(prices)):
Q4
D — i is the index (position), not the element. Adding i totals 0, 1, 2, 3 instead of the temperatures. It should be total = total + temps[i].
Q5
C — This replaces the whole array with a single number. To change one element in place, assign to nums[i]. It should be nums[i] = nums[i] + 1.