Functional Programming in Python: Concepts and Code Examples
This article introduces functional programming concepts in Python, explaining pure functions, higher‑order functions, and function composition, and demonstrates ten practical examples—including map, filter, reduce, list comprehensions, generators, partial application, operator usage, compose, zip, and recursion—to illustrate how to write concise, maintainable code.
Functional programming is a programming paradigm that emphasizes the use of pure functions (no side effects, no state mutation) and function composition to build programs. Python provides several functional features such as higher‑order functions, anonymous functions, and composition tools that help write concise and maintainable code.
1. Using map() to transform each element of an iterable:
data = [1, 2, 3, 4, 5]
result = map(lambda x: x * 2, data)
print(list(result)) # 输出 [2, 4, 6, 8, 10]2. Using filter() to select elements that satisfy a condition:
data = [1, 2, 3, 4, 5]
result = filter(lambda x: x % 2 == 0, data)
print(list(result)) # 输出 [2, 4]3. Using reduce() to accumulate values across an iterable:
from functools import reduce
data = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, data)
print(result) # 输出 15 (1 + 2 + 3 + 4 + 5)4. Using a list comprehension to generate a new list:
data = [1, 2, 3, 4, 5]
result = [x * 2 for x in data]
print(result) # 输出 [2, 4, 6, 8, 10]5. Using a generator expression to create a generator:
data = [1, 2, 3, 4, 5]
result = (x * 2 for x in data)
print(list(result)) # 输出 [2, 4, 6, 8, 10]6. Using functools.partial() for partial function application:
from functools import partial
def multiply(x, y):
return x * y
double = partial(multiply, y=2)
result = double(5)
print(result) # 输出 107. Replacing lambda expressions with functions from the operator module:
from operator import add, mul
result1 = add(2, 3) # 等同于 lambda x, y: x + y
result2 = mul(2, 3) # 等同于 lambda x, y: x * y
print(result1, result2) # 输出 5 68. Using functools.compose() to combine multiple functions into one:
from functools import compose
def add_one(x):
return x + 1
def multiply_by_two(x):
return x * 2
transform = compose(add_one, multiply_by_two)
result = transform(5)
print(result) # 输出 11 (5 * 2 + 1)9. Using zip() to pair elements from multiple iterables:
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
result = zip(names, ages)
print(list(result)) # 输出 [("Alice", 25), ("Bob", 30), ("Charlie", 35)]10. Solving problems recursively, exemplified by a factorial function:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
result = factorial(5)
print(result) # 输出 120 (5!)These examples demonstrate how functional programming techniques in Python—such as higher‑order functions, anonymous functions, composition, and other functional features—enable developers to write clearer, more maintainable code for data transformation, filtering, aggregation, and more.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.