Lists – Indexing, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
Lists – Indexing in one minute
What students need to know
Unlike strings, list items can be changed by assigning to their index. -1 always gives the last element of a list.
Index 0 is always the first item. Indices go from 0 to len-1 — [3] doesn't exist in a 3-item list.
Mistakes students make
- List has indices 0,1,2: index 3 is out of range.
- Use square brackets [] for indexing, not parentheses ().
- Index must be an integer, not a string.
- Use square brackets for assignment: nums[1] = 99.
PREVIOUS · MODULE 33Lists – Creation
YOU ARE HERE · MODULE 34Lists – Indexing
NEXT · MODULE 35Lists – Slicing
What students will learn in this module
Every question in Module 34
Quick questions
- colors = ['red','blue','green']\ncolors[0] returns?A'blue'B'red'
- colors = ['red','blue','green']\ncolors[-1] returns?A'green'B'red'
- Can you change a list item by index?ANo lists are immutableBYes lists are mutable
- nums = [10,20,30]\nnums[3] does what?AIndexErrorBReturns 30
- 'cat' in ['cat','dog'] returns?ATrueBFalse
Key terms
Students type the meaning of:
list[0]list[-1]list[i] = valueIndexError
Match the pairs
nums = [10, 20, 30] print(nums[3])Match it to what it does
nums = [1, 2, 3] print(nums(0))Match it to what it does
nums = [5, 10, 15] nums[1] = 99Match it to what it does
nums[-1]Match it to what it doesc = ['r','g','b'] print(c[0])Match it to what it does
c = ['r','g','b'] print(c[-1])Match it to what it does
Fill in the blanks
colors = ['red', 'blue'] print(colors[_])
nums = [10, 20, 30] nums[_] = 99
pets = ['cat', 'dog'] print('cat' __ pets)___ fruit in fruits: print(fruit)colors = ['red', 'blue', 'green'] print(colors[__])
Spot the error
Question 1
Acolors = ['red', 'blue', 'green']Bfirst = colors[0]Clast = colors[3]Dprint(first, last)
Question 2
Acolors = ['red', 'blue']Bfirst = colors(0)Cprint('First:', first)Dprint('Done')
Question 3
Acolors = ['red', 'blue']Bfirst = colors['0']Cprint('First:', first)Dprint('Done')
Question 4
Anums = [10, 20, 30]Bnums(1) = 99Cprint('Updated:', nums)Dprint('Done')
Question 5
Acolors = ['red', 'blue']Bfor color in colorsC print(color)Dprint('Done')
