×

python · level 3 · unit 2 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A function is a value you can pass around.

Write a function's name without brackets and you get the function itself, not its answer. You can store it in a variable, put it in a dictionary, or hand it to another function to call later.

sorted, max and min take a . They call it on every item and compare the answers, so sorted(words, key=len) puts the shortest words first.

For a one-off key, write a : lambda film: film[1] is a tiny function with no name that returns its one expression. Keep lambdas short. Anything bigger deserves a proper def.

films = [("Up", 2009), ("Jaws", 1975), ("Coco", 2017)]
oldest = min(films, key=lambda film: film[1])
by_title = sorted(films, key=lambda film: film[0])
print(oldest)
print(by_title)
shout = str.upper
print(shout("encore"))

key=len hands over the function, while key=len() would try to call it straight away.