Functional Programming
Master functional programming patterns, pure functions, composition, and advanced FP architectures
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What Functional Programming Actually Means
Functional programming is based on core principles:
Principle
What It Means
Real-World Analogy
Pure Functions
Same input → same output, no side effects
A calculator: 2+2 always equals 4
Immutability
Data never changes; create new copies
Editing a photo creates a new file
First-Class Functions
Functions can be passed around like data
Giving someone a recipe card to follow
Higher-Order Functions
Functions that work with other functions
A chef who teaches other chefs techniques
Declarative Style
Describe what, not how
"Make me a sandwich" vs step-by-step instructions
- Functions are first-class citizens — can be stored, passed, returned
- No mutating state — create new data instead of modifying
- Pure functions — no side effects, predictable output
- Immutability — treat data as unchangeable
- Declarative style — describe what you want, not how
Pure Functions
A pure function has no side effects, doesn't modify external state, and always produces the same output for the same input.
Immutability in Python
FP encourages transforming data instead of mutating it. Use immutable types and return new values.
Higher-Order Functions
Functions that take functions as arguments or return functions. Core to functional programming.
map(), filter(), reduce() — Core FP Tools
The fundamental functional transformations for working with sequences.
Lambda Functions
Anonymous functions perfect for functional pipelines and quick transformations.
Function Composition
Building complex operations by chaining simple pure functions.
functools.partial — Pre-Configuring Functions
Create specialized versions of functions by "freezing" some arguments.
Generator-Based Functional Programming
Generators provide lazy evaluation and memory-efficient functional pipelines.
Advanced: Currying
Transform multi-argument functions into chains of single-argument functions.
Decorators — Functional Power Tools
Decorators are higher-order functions that transform and enhance functions.
Functional Error Handling
Handle errors with values instead of exceptions, enabling safer composition.
Immutable Data Structures
Use frozen dataclasses and immutable collections for safer functional code.
Real-World Functional Pipeline
A complete functional architecture for data processing.
Summary
You've learned comprehensive functional programming in Python:
- Pure functions and immutability principles
- Higher-order functions and function composition
- map(), filter(), reduce() and lambda functions
- functools utilities (partial, lru_cache, reduce)
- Generator-based lazy evaluation
- Advanced patterns: currying, decorators, monads
- Functional error handling with Maybe/Result
- Immutable data structures
- Real-world functional pipelines
Functional programming helps you build cleaner, more predictable, and easier-to-maintain code. These patterns are essential for data engineering, ML pipelines, ETL systems, and modern backend architectures.
📋 Quick Reference — Functional Programming
Tool / Syntax
What it does
map(fn, iterable)
Apply fn to every element
filter(fn, iterable)
Keep elements where fn returns True
functools.reduce(fn, iterable)
Accumulate into a single value
lambda x: x * 2
Anonymous inline function
functools.partial(fn, arg)
Pre-fill some function arguments
You can now write purely functional Python using map, filter, reduce, and composition — essential for data pipelines and ML workflows.
Up next: Metaprogramming — write code that generates and inspects other code at runtime.
Practice quiz
What defines a pure function?
- It prints output
- It uses global state
- Same input always gives the same output with no side effects
- It modifies its arguments
Answer: Same input always gives the same output with no side effects. A pure function always returns the same output for the same input and has no side effects, making it predictable and testable.
Which of these Python types is IMMUTABLE?
- tuple
- list
- dict
- set
Answer: tuple. Tuples (like int, float, str, frozenset) are immutable; lists, dicts, and sets are mutable.
What does map(lambda x: x * 2, [1, 2, 3]) produce when wrapped in list()?
map applies the function to each element, doubling them to give [2, 4, 6].
What does filter(lambda x: x % 2 == 0, nums) keep?
- Odd numbers
- Even numbers (where the function returns True)
- All numbers
- Nothing
Answer: Even numbers (where the function returns True). filter keeps only elements for which the function returns True; here that means the even numbers.
What does reduce(lambda a, b: a + b, [1, 2, 3, 4, 5]) return?
- 15
- 120
Answer: 15. reduce combines the list into a single value by repeated addition: 1+2+3+4+5 = 15.
What is a higher-order function?
- A function with many lines
- A recursive function
- A function that takes or returns other functions
- A function defined at module top level
Answer: A function that takes or returns other functions. A higher-order function takes functions as arguments and/or returns functions — core to functional programming.
What does a lambda expression create?
- A class
- An anonymous (unnamed) inline function
- A generator
- A decorator
Answer: An anonymous (unnamed) inline function. lambda creates a small anonymous function, handy for sorting keys, callbacks, and inline transformations.
What advantage do generators give in functional pipelines?
- They sort data
- They run in parallel automatically
- They validate types
- Lazy evaluation — memory-efficient, one item at a time
Answer: Lazy evaluation — memory-efficient, one item at a time. Generators yield items lazily, so a chained generator pipeline processes data one element at a time without building large lists in memory.
What does functools.partial do?
- Splits a function in half
- Creates a specialized function by pre-filling (freezing) some arguments
- Runs a function partially
- Caches results
Answer: Creates a specialized function by pre-filling (freezing) some arguments. partial pre-fills some arguments to produce a new, specialized callable, e.g. square = partial(power, exp=2).
What is currying?
- Caching function results
- Running functions concurrently
- Transforming f(a, b, c) into a chain f(a)(b)(c) of single-argument calls
- Adding type hints
Answer: Transforming f(a, b, c) into a chain f(a)(b)(c) of single-argument calls. Currying turns a multi-argument function into a sequence of single-argument functions, called like multiply(2)(3)(4).
Continue this course
- Previous: Collections
- Next: Metaprogramming