Pylerno
English

Functions as values

Pass a small behavior to a reusable operation such as sorting.

On this page

The concept

Functions are values: they can be assigned to names, passed as arguments, and returned. sorted uses a key function to obtain the comparison value for each item. This allows the same sorting operation to order records by score, price, or name length.

lambda creates a small anonymous function consisting of one expression. Use def when a function needs several statements, a meaningful name, or reuse. map and filter return iterators; a comprehension is often clearer for a simple transformation or filter.

Worked example

python
students = [("Sara", 90), ("Dana", 75), ("Avan", 85)]
ranked = sorted(students, key=lambda student: student[1], reverse=True)
print(ranked)
numbers = [1, 2, 3, 4]
print(list(map(lambda number: number * 2, numbers)))
print(list(filter(lambda number: number % 2 == 0, numbers)))
print([number * 2 for number in numbers])
print(sorted(["Python", "is", "fun"], key=len))

Trace the result

  1. The sorting key selects element 1 of each student tuple, which is the score.
  2. map doubles every number, while filter keeps only even numbers.
  3. key=len orders words by their length rather than their text.
text
[('Sara', 90), ('Avan', 85), ('Dana', 75)]
[2, 4, 6, 8]
[2, 4]
[2, 4, 6, 8]
['is', 'fun', 'Python']
Line-by-line walkthrough

Line 1: students = [("Sara", 90), ("Dana", 75), ("Avan", 85)]

Evaluate [('Sara', 90), ('Dana', 75), ('Avan', 85)], then bind or update students with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 2: ranked = sorted(students, key=lambda student: student[1], reverse=True)

Evaluate sorted(students, key=lambda student: student[1], reverse=True), then bind or update ranked with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 3: print(ranked)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 4: numbers = [1, 2, 3, 4]

Evaluate [1, 2, 3, 4], then bind or update numbers with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 5: print(list(map(lambda number: number * 2, numbers)))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: print(list(filter(lambda number: number % 2 == 0, numbers)))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: print([number * 2 for number in numbers])

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print(sorted(["Python", "is", "fun"], key=len))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

key=function passes a function. key=function() calls it immediately and passes its result, which is usually a different and incorrect operation.

Check your understanding

What does this program print?

python
action = len
print(action("hi"))
Choose one answer

Search documentation

Share this page