Exercises
Change Items
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Replace Grace with Alan, keeping the same list object.
Python
names = ["Ada", "Grace", "Katherine"]
# replace the middle name
print(names)Assign straight to the index.
names = ["Ada", "Grace", "Katherine"]
names[1] = "Alan"
print(names)Exercise 2Passed
Replace the two middle numbers with three letters, using a slice assignment.
Python
numbers = [1, 2, 3, 4, 5]
# replace positions 1 and 2 with 'a', 'b', 'c'
print(numbers)Assigning to a slice can change the length.
numbers = [1, 2, 3, 4, 5]
numbers[1:3] = ["a", "b", "c"]
print(numbers)Exercise 3Passed
Change a so that b sees the new contents too.
Python
a = [1, 2, 3]
b = a
# make both show [7, 8]
print(a)
print(b)Rebinding a would leave b behind. Assign to the whole slice instead.
a = [1, 2, 3]
b = a
a[:] = [7, 8]
print(a)
print(b)