Exercises
Sort Lists
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Sort the names ignoring case.
Python
names = ["banana", "Apple", "cherry"]
# print them sorted case-insensitively
Pass key=str.lower.
names = ["banana", "Apple", "cherry"]
print(sorted(names, key=str.lower))Exercise 2Passed
Sort the people by age, youngest first.
Python
people = [("Ada", 36), ("Grace", 45), ("Katherine", 28)]
# print them sorted by age
The key returns the part to sort on.
people = [("Ada", 36), ("Grace", 45), ("Katherine", 28)]
print(sorted(people, key=lambda person: person[1]))Exercise 3Passed
This sets numbers to None. Fix it so it prints the sorted list.
Python
numbers = [3, 1, 2]
numbers = numbers.sort()
print(numbers)sort() returns None. Either drop the assignment or use sorted().
numbers = [3, 1, 2]
numbers.sort()
print(numbers)