Lists – Slicing, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
Lists – Slicing in one minute
What students need to know
Slice from index 1 up to (not including) index 4. Step -1 traverses the list backwards, producing a reversed copy.
Start at index 1, stop before 4: 20(1) 30(2) 40(3). [:2] takes indices 0 and 1: 10, 20.
Mistakes students make
- Use colon : for slicing, not comma.
- Use square brackets for slicing, not parentheses.
- Step cannot be 0: causes ValueError.
- copy = nums doesn't copy: both point to the same list. Use nums[:] to copy.
PREVIOUS · MODULE 34Lists – Indexing
YOU ARE HERE · MODULE 35Lists – Slicing
NEXT · MODULE 36Lists – Methods (append, remove, pop, insert)
What students will learn in this module
Every question in Module 35
Quick questions
- [10,20,30,40,50][1:4] returns?A[20, 30, 40]B[10, 20, 30, 40]
- [10,20,30,40][:2] returns?A[10, 20]B[10, 20, 30]
- [10,20,30,40][2:] returns?A[30, 40]B[10, 20]
- Does list slicing modify the original?AYesBNo creates a new list
- [1,2,3,4,5][::-1] returns?A[1, 2, 3, 4, 5]B[5, 4, 3, 2, 1]
Key terms
Students type the meaning of:
list[start:stop]list[:stop]list[start:]list[::step]
Match the pairs
nums = [1,2,3,4,5] nums[1,3]Match it to what it does
nums = [1,2,3] nums[::0]Match it to what it does
nums = [1,2,3,4,5] nums[1:4]Match it to what it does
nums[::-1]Match it to what it doesn = [10,20,30,40,50] print(n[:3])Match it to what it does
n = [1,2,3,4,5] print(n[::2])Match it to what it does
Fill in the blanks
nums = [10, 20, 30, 40] print(nums[_:3])
nums = [10, 20, 30, 40] print(nums[2:_])
nums = [1, 2, 3, 4, 5] print(nums[::__])
nums = [1, 2, 3] copy = nums[_]
nums = [10, 20, 30, 40, 50] print(nums[:_])
Spot the error
Question 1
Anums = [10, 20, 30]Bpart = nums[0,2]Cprint('Slice:', part)Dprint('Done')
Question 2
Anums = [10, 20, 30, 40]Bpart = nums(1:3)Cprint('Slice:', part)Dprint('Done')
Question 3
Anums = [1, 2, 3, 4, 5]Bpart = nums[1:4:0]Cprint('Slice:', part)Dprint('Done')
Question 4
Anums = [1, 2, 3]Bcopy = numsCcopy[0] = 99D# expects nums unchanged
Question 5
Anums = [10, 20, 30, 40, 50][-1:-4]Blabel = 'Slice'Cprint('Slice:', nums)Dprint('Done')
