Exercises
List Methods
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Use the list as a stack: push a and b, then pop and print the top item.
Python
stack = []
# push two items, then pop and print one
print(stack)append pushes, pop takes the last one off.
stack = []
stack.append("a")
stack.append("b")
print(stack.pop())
print(stack)Exercise 2Passed
Print the sum, the smallest and the largest of the numbers.
Python
numbers = [4, 1, 3]
# print sum, then min, then max
These are builtins, not methods.
numbers = [4, 1, 3]
print(sum(numbers))
print(min(numbers))
print(max(numbers))Exercise 3Passed
Print the list reversed without changing the original.
Python
original = [1, 2, 3]
# print the reversed version, then the untouched original
reverse() changes in place; reversed() does not.
original = [1, 2, 3]
print(list(reversed(original)))
print(original)