Lists – Indexing

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

Lists – Indexing

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. colors = ['red','blue','green']\ncolors[0] returns?
A 'blue'
B 'red'
2. colors = ['red','blue','green']\ncolors[-1] returns?
A 'green'
B 'red'
3. Can you change a list item by index?
A No lists are immutable
B Yes lists are mutable
4. nums = [10,20,30]\nnums[3] does what?
A IndexError
B Returns 30
5. 'cat' in ['cat','dog'] returns?
A True
B False

Match the Pairs

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

Left columnRight column
1nums = [10, 20, 30] print(nums[3]) AValid: Lists are mutable
2nums = [1, 2, 3] print(nums(0)) BOutput: b
3nums = [5, 10, 15] nums[1] = 99 CError: Use [] not () for indexing
4nums[-1] DError: Index 3 out of range
5c = ['r','g','b'] print(c[0]) EOutput: r
6c = ['r','g','b'] print(c[-1]) FOk: Last item in list

Definitions

list[0]

Fill in the Blanks

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

1.
colors = ['red', 'blue'] print(colors[_])
2.
nums = [10, 20, 30] nums[_] = 99
3.
pets = ['cat', 'dog'] print('cat' __ pets)
4.
___ fruit in fruits: print(fruit)
5.
colors = ['red', 'blue', 'green'] print(colors[__])

Spot the Error

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

Question 1
A colors = ['red', 'blue', 'green']
B first = colors[0]
C last = colors[3]
D print(first, last)
Question 2
A colors = ['red', 'blue']
B first = colors(0)
C print('First:', first)
D print('Done')
Question 3
A colors = ['red', 'blue']
B first = colors['0']
C print('First:', first)
D print('Done')
Question 4
A nums = [10, 20, 30]
B nums(1) = 99
C print('Updated:', nums)
D print('Done')
Question 5
A colors = ['red', 'blue']
B for color in colors
C print(color)
D print('Done')

Answer Key

Lists – Indexing
Section A: Quick Questions
Q1
B — 'red'
Q2
A — 'green'
Q3
B — Yes lists are mutable
Q4
A — IndexError
Q5
A — True
Section B: Match the Pairs
Q1
nums = [10, 20, 30] print(nums[3]) → Error: Index 3 out of range
Q2
nums = [1, 2, 3] print(nums(0)) → Error: Use [] not () for indexing
Q3
nums = [5, 10, 15] nums[1] = 99 → Valid: Lists are mutable
Q4
nums[-1] → Ok: Last item in list
Q5
c = ['r','g','b'] print(c[0]) → Output: r
Q6
c = ['r','g','b'] print(c[-1]) → Output: b
Section C: Definitions
Q1
list[0] — Access the first item in the list.~~list[-1]
Section D: Fill in the Blanks
Q1
colors = ['red', 'blue'] print(colors[0])
Q2
nums = [10, 20, 30] nums[1] = 99
Q3
pets = ['cat', 'dog'] print('cat' in pets)
Q4
for fruit in fruits: print(fruit)
Q5
colors = ['red', 'blue', 'green'] print(colors[-1])
Section E: Spot the Error
Q1
C — List has indices 0,1,2: index 3 is out of range
Q2
B — Use square brackets [] for indexing, not parentheses ()
Q3
B — Index must be an integer, not a string
Q4
B — Use square brackets for assignment: nums[1] = 99
Q5
B — Missing colon after the for statement