Python supports multiple programming paradigms, and Functional Programming (FP) is one of the most powerful. Rather than describing how to perform each computation step-by-step, FP lets you declare what transformation should happen and pass functions as values to drive that logic. Python’s support for first-class functions, lambdas, and built-in tools like map(), filter(), and reduce() makes building clean, composable data pipelines natural and expressive.
The Declarative Approach
Imperative programming focuses on how to solve a problem — you manage loops, indices, and mutable state. Declarative programming focuses on what you want — you express the logic of a computation without dictating its control flow.
Side-by-Side Comparison
Imperative (how to do it):
Declarative (what to do):
Both produce the same result, but the declarative version is shorter, more expressive, and has no explicit loop or intermediate list to manage.
Higher-Order Functions
A Higher-Order Function either accepts a function as an argument, returns a function as its result, or both.
Passing Functions as Arguments
process_numbers doesn’t know or care what operation to perform — it delegates that decision to the caller.
Returning Functions (The Strategy Pattern)
Real-World: Salary Policy Example
process_salary is generic. Different policies (tax, bonus) are plugged in at call time — a classic Strategy Pattern.
Built-In Higher-Order Functions
For complex multi-line logic, pass a named function directly:
filter() — Keep Elements That Match a Condition
reduce() — Collapse a Sequence to a Single Value
Functional Pipeline: Sum of Squares of Even Numbers
This classic example chains all three higher-order functions together:
As a single chained statement:
The Pythonic Equivalent
While the functional pipeline above is correct, Python often provides a more readable alternative using generator expressions and built-in functions:
Prefer generator expressions and built-in functions (sum(), max(), min(), all(), any()) for mathematical operations on collections. Use map()/filter()/reduce() when building explicit functional pipelines or integrating with callback-based APIs.