Phase: 4. Functions | Estimated time: 2 hours | Milestone Project: No
- Module 031 (Functions: Basics)
- Module 034 (Lambda Functions)
By the end of this module, you will be able to:
- Use
map()to transform iterables with a function - Use
filter()to select elements with a predicate - Use
functools.reduce()to accumulate values - Treat functions as first-class objects (pass as arguments, return them)
- Use
sorted()with a customkeyfunction
Higher-order functions (functions that take or return other functions) are the foundation of functional programming in Python. They allow concise, declarative data processing without explicit loops.
In Python, functions are objects. You can assign them to variables, pass them as arguments, and return them from other functions:
def square(x):
return x * x
f = square # assign to variable
print(f(5)) # call through variable
def apply(func, values):
return [func(v) for v in values]
print(apply(square, [1, 2, 3])) # [1, 4, 9]nums = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, nums))
# [2, 4, 6, 8]nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
# [2, 4, 6]from functools import reduce
total = reduce(lambda a, b: a + b, [1, 2, 3, 4])
# 10words = ["python", "java", "c", "javascript"]
sorted(words, key=len)
# ['c', 'java', 'python', 'javascript']def make_multiplier(n):
"""Return a function that multiplies by n."""
def multiplier(x):
return x * n
return multiplier
double = make_multiplier(2)
print(double(5)) # 10- Forgetting to convert
map()/filter()to a list: They return iterators, not lists. - Using
reduce()when a built-in exists:sum(),min(),max()are clearer. - Overusing lambdas: Named functions are more readable for complex logic.
- Modifying the original list:
map()andfilter()create new iterables.
- Create a list of numbers and use
map()with a lambda to cube each. - Use
filter()to keep only positive numbers from[-3, -1, 0, 2, 5]. - Use
reduce()to find the maximum in a list. - Write a function
make_power(exp)that returns a function raising its argument toexp.
map(func, iterable)— transforms each element.filter(predicate, iterable)— keeps elements where predicate is True.functools.reduce(func, iterable)— accumulates values.- Functions are first-class: pass them, return them, assign them.
sorted(iterable, key=func)— sort with custom key.
Continue to Module 037: Decorators: Basics.