Show the difference between Python's list.sort() method and the sorted() function, including the key argument.
sort() is a method that belongs to the list, and it sorts that list where it stands. sorted() is a function that takes any iterable and hands you back a new sorted list, leaving your original collection exactly as it was.
numbers = [3, 1, 2]
result = numbers.sort()
print(result) # None - sort returns nothing
print(numbers) # [1, 2, 3] - but the list is sorted
copy = sorted([3, 1, 2], reverse=True)
print(copy) # [3, 2, 1]
print([3, 1, 2]) # [3, 1, 2] - the original is untouched
# The key argument chooses what to sort on.
words = ['banana', 'fig', 'apple']
print(sorted(words, key=len)) # ['fig', 'apple', 'banana']
The None that sort() returns is the detail that catches people out, because it means that the whole statement result = numbers.sort() does nothing that you can use. If you find that pattern in your own code, then you wanted sorted() instead.
The key argument is where most of the real work happens. It takes a function that is applied to each element to produce the value that is used for the comparison, and the elements that end up in the result are the original ones. So sorting words by their length gives you the words in order of length, while they are still spelled normally. The reverse=True argument flips the order without needing a reversed key function.
Two more things are worth knowing. Python's sort is stable, which means that items which compare equal keep their original relative order. That matters when you sort by one field of a record and you want the other fields to stay in a predictable order. And a key function is called exactly once for each element, so that is the right place to put an expensive calculation. Writing a function that recomputes a value on every comparison is a well-known performance mistake.