Creating an array of a set size

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

Creating an array of a set size

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates an array of 10 elements that all start at 0?
A scores = [0] * 10
B scores = [10] * 0
2. What does names = [''] * 5 create?
A An array of 5 zeros
B An array of 5 empty strings
3. How many elements does marks = [0] * 30 have?
A 1
B 30
4. Which version uses a constant for the array size?
A SIZE = 8 scores = [0] * SIZE
B scores = [0] * size()
5. After scores = [0] * 5, what does scores[2] = 17 do?
A Fills the third element with 17 and leaves the others at 0
B Adds 17 to the end and makes the array 6 elements long

Match the Pairs

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

Left columnRight column
1scores = [0] * 10 AThe * is inside the brackets, so it makes a list with one element (0 * 10 = 0), not 10 elements. It should be [0] * 10.
2names = [''] * 5 BSIZE = 10 scores = [0] * SIZE
3Use a constant for the size so the array length can be changed in one place Cscores[2] = 85
4Fill an element of a set-size array by its index DUses + instead of *. A list cannot be added to a number, so this causes an error. It should be [0] * 10 to repeat the 0 ten times.
5scores = [0] + 10 ECreates an array of 5 empty strings ready to be filled
6scores = [0 * 10] FCreates an array of 10 elements, all starting at 0

Definitions

scores = [0] * 10

Fill in the Blanks

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

1.
scores = [0] _ 10
2.
# Array for 5 student names names = [''] * _
3.
SIZE = 8 marks = [0] * ____
4.
SIZE = 5 scores = [0] * SIZE scores[_] = 75
5.
scores = [0] * 10 for i in range(___(scores)): scores[i] = i * 5

Spot the Error

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

Question 1
A scores = [0] x 10
B scores[0] = 85
C scores[1] = 92
D print(len(scores))
Question 2
A SIZE = 5
B names = [] * SIZE
C names[0] = 'Ava'
D print(names)
Question 3
A SIZE = 8
B marks = [0] + SIZE
C for i in range(SIZE):
D marks[i] = i * 10
Question 4
A temps = [0.0] * 7.0
B temps[6] = 31.5
C print(temps)
Question 5
A SIZE = 5
B scores = [0] * SIZE
C scores[5] = 90
D print(scores)

Answer Key

Creating an array of a set size
Section A: Quick Questions
Q1
A — scores = [0] * 10
Q2
B — An array of 5 empty strings
Q3
B — 30
Q4
A — SIZE = 8 scores = [0] * SIZE
Q5
A — Fills the third element with 17 and leaves the others at 0
Section B: Match the Pairs
Q1
scores = [0] * 10 → Creates an array of 10 elements, all starting at 0
Q2
names = [''] * 5 → Creates an array of 5 empty strings ready to be filled
Q3
Use a constant for the size so the array length can be changed in one place → SIZE = 10 scores = [0] * SIZE
Q4
Fill an element of a set-size array by its index → scores[2] = 85
Q5
scores = [0] + 10 → Uses + instead of *. A list cannot be added to a number, so this causes an error. It should be [0] * 10 to repeat the 0 ten times.
Q6
scores = [0 * 10] → The * is inside the brackets, so it makes a list with one element (0 * 10 = 0), not 10 elements. It should be [0] * 10.
Section C: Definitions
Q1
scores = [0] * 10 — Creates a one-dimensional array (list) of 10 elements that all start with the value 0. Change the number to set the size, or use '' for strings.
Section D: Fill in the Blanks
Q1
scores = [0] * 10
Q2
# Array for 5 student names names = [''] * 5
Q3
SIZE = 8 marks = [0] * SIZE
Q4
SIZE = 5 scores = [0] * SIZE scores[0] = 75
Q5
scores = [0] * 10 for i in range(len(scores)): scores[i] = i * 5
Section E: Spot the Error
Q1
A — x is not a Python operator, so this is a syntax error. Use * to repeat the element: scores = [0] * 10.
Q2
B — Repeating an empty list still gives an empty list with 0 elements. Put an empty string inside the brackets: names = [''] * SIZE.
Q3
B — You cannot add an integer to a list, so this causes a TypeError. Use * to repeat the starting value: marks = [0] * SIZE.
Q4
A — The array size must be a whole number. Multiplying a list by a float causes a TypeError. Use the integer 7.
Q5
C — An array of 5 elements has indexes 0 to 4. Index 5 is out of range and causes an IndexError. Use scores[4] = 90 for the last element.