One-dimensional arrays

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

One-dimensional arrays

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement best describes a one-dimensional array?
A A single row of items of the same data type, found by index
B A table of rows and columns holding mixed data types
2. scores = [12, 18, 9, 15] What is scores[0]?
A 18
B 12
3. Which list is a valid array of one data type?
A temps = [21.5, 19.0, 23.4]
B temps = [21.5, "cold", True]
4. An array called names has 5 items. What is the last valid index?
A 5
B 4
5. Which loop visits every index of the array marks?
A for i in range(len(marks)):
B for i in range(len(marks) + 1):

Match the Pairs

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

Left columnRight column
1scores = [12, 15, 9, 20] AThe elements are different data types (integer, string, float). An array must hold one data type only.
2for i in range(len(scores)): Bnames = ["Ava", "Liam", "Mia"]
3Each item in an array is found by its index number, and the first index is 0 Cprint(scores[0])
4Every element in an array must be the same data type, such as all strings DIndex 3 does not exist. The array has 3 items, so the valid indexes are 0, 1 and 2.
5temps = [21, "warm", 19.5] ECreates a one-dimensional array of four integers using a Python list
6nums = [4, 8, 6] print(nums[3]) FLoops through every index of the array, from 0 up to the last index

Definitions

one-dimensional array

Fill in the Blanks

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

1.
scores = [12, 18, 15, 20] print(scores[_])
2.
scores = [12, 18, 15, 20] for i in _____(len(scores)): print(scores[i])
3.
scores = [12, 18, 15, 20] size = ___(scores)
4.
scores = [12, 18, 15, 20] scores[_] = 99
5.
scores = [12, 18, 15, 20] print(scores[len(scores) - _])

Spot the Error

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

Question 1
A scores = [12, 15, 9, 20]
B print(scores(0))
C print(scores[3])
Question 2
A temps = [21; 25; 19; 30]
B for i in range(4):
C print(temps[i])
Question 3
A marks = [70, 85, 90]
B for i in range(4):
C print(marks[i])
Question 4
A prices = [4, 8, 6, 10]
B total = 0
C for i in range(4):
D total = total + price[i]
E print(total)
Question 5
A ages = [14, 'fifteen', 13, 16]
B print(ages[1])
C print(len(ages))

Answer Key

One-dimensional arrays
Section A: Quick Questions
Q1
A — A single row of items of the same data type, found by index
Q2
B — 12
Q3
A — temps = [21.5, 19.0, 23.4]
Q4
B — 4
Q5
A — for i in range(len(marks)):
Section B: Match the Pairs
Q1
scores = [12, 15, 9, 20] → Creates a one-dimensional array of four integers using a Python list
Q2
for i in range(len(scores)): → Loops through every index of the array, from 0 up to the last index
Q3
Each item in an array is found by its index number, and the first index is 0 → print(scores[0])
Q4
Every element in an array must be the same data type, such as all strings → names = ["Ava", "Liam", "Mia"]
Q5
temps = [21, "warm", 19.5] → The elements are different data types (integer, string, float). An array must hold one data type only.
Q6
nums = [4, 8, 6] print(nums[3]) → Index 3 does not exist. The array has 3 items, so the valid indexes are 0, 1 and 2.
Section C: Definitions
Q1
one-dimensional array — A single row of items of the same data type, stored under one name. Each item is found by its index number, starting at 0. In Python, a list is used as an array.
Section D: Fill in the Blanks
Q1
scores = [12, 18, 15, 20] print(scores[0])
Q2
scores = [12, 18, 15, 20] for i in range(len(scores)): print(scores[i])
Q3
scores = [12, 18, 15, 20] size = len(scores)
Q4
scores = [12, 18, 15, 20] scores[2] = 99
Q5
scores = [12, 18, 15, 20] print(scores[len(scores) - 1])
Section E: Spot the Error
Q1
B — An array (list) element is found with square brackets, not round brackets. It should be print(scores[0]).
Q2
A — The items in a Python list are separated by commas, not semicolons. It should be temps = [21, 25, 19, 30].
Q3
B — The array has 3 elements, so the valid indexes are 0, 1 and 2. range(4) would reach index 3 and cause an IndexError. It should be range(3).
Q4
D — The array is called prices, not price, so this line would cause a NameError. It should be total = total + prices[i].
Q5
A — Every element of an array must have the same data type. 'fifteen' is a string mixed in with integers. It should be the integer 15.