Exercises
List Access
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Print the first and the last name, without hard-coding the length.
Python
names = ["Ada", "Grace", "Katherine"]
# print the first, then the last
[0] is the first and [-1] is the last.
names = ["Ada", "Grace", "Katherine"]
print(names[0])
print(names[-1])Exercise 2Passed
Print the middle three numbers using a slice.
Python
numbers = [0, 1, 2, 3, 4, 5]
# print [1, 2, 3]
The start is included and the stop is not.
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4])Exercise 3Passed
Take a slice of the first two items, change its first item to 99, and show the original is untouched.
Python
original = [1, 2, 3]
# slice, change the slice, print the slice then the original
A slice is a new list, so changing it cannot affect the original.
original = [1, 2, 3]
piece = original[0:2]
piece[0] = 99
print(piece)
print(original)