functional programming
Functional Programming (FP) is a programming paradigm that emphasizes pure functions, immutable data, and function composition.
In Python, functional programming usually means writing functions that avoid changing global state and rely solely on their inputs and return values.
Although Python isn’t purely functional, you can still follow many functional principles using higher-order functions, lambda expressions, and built-in tools like map()
, filter()
and functools.reduce()
.
Key Concepts
- Pure Functions: Return the same result given the same inputs, without side effects.
- Immutability: Data doesn’t change in place; each operation creates new data.
- Higher-Order Functions: Functions that take other functions as arguments or return them as results.
Example in Python
from functools import reduce
numbers = [1, 2, 3, 4, 5]
doubled = map(lambda x: x * 2, numbers)
filtered = filter(lambda x: x % 2 == 0, doubled)
summed = reduce(lambda acc, x: acc + x, filtered, 0)
print(summed) # 12
Real-world usage often involves processing large data sets or composing pipelines of small, reusable functions, making your code easier to test and maintain.
Related Resources
Learning Path
Functional Programming With Python
Unlock the power of functional programming in Python! From lambda functions to built-in functions like map() and filter(), and advanced topics like closures, decorators, and recursion. This path will elevate your coding skills and make your code more efficient and readable.
For additional information on related topics, take a look at the following resources:
By Dan Bader • Updated Jan. 14, 2025