Chapter 8: Functional Programming
Chapter 8: Functional Programming
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. Python supports functional programming concepts that make your code more predictable, testable, and maintainable.
Understanding Functional Programming
Functional programming emphasizes:
- Pure functions: Functions that always return the same output for the same input
- Immutability: Data that doesn't change after creation
- No side effects: Functions that don't modify external state
Depending on Global State (Avoid This)
This function depends on external state, making it harder to test and reason about.
Functional Approach (Better)
The function is now pure - it always returns the same output for the same input, with no side effects.
Avoiding Mutable State
Functional Approach with Explicit State
Modifying Mutable Data (Avoid This)
Functional Approach with Immutability
Applying Functions to Data Science Workflows
Functional programming shines in data science when transforming and analyzing data.
Using apply() with DataFrames
Creating Feature Engineering Functions
Map, Reduce, and Filter
Python provides three powerful functional programming tools: map(), reduce(), and filter().
Map: Apply a Function to Every Element
Map with Multiple Inputs
Map with Lambda
Reduce: Combine Elements into a Single Value
Reduce with operator Module
Filter: Select Elements That Match a Condition
Filter with Lambda
Combining Map and Filter
List Comprehensions
List comprehensions provide a concise, readable way to create lists. They're often more Pythonic than map/filter.
From For Loop to List Comprehension
Mapping with List Comprehensions
Filtering with List Comprehensions
Multiple Variables with zip
Conditional Expressions in Comprehensions
Flattening Lists
Nested List Comprehensions
Creating 2D Arrays
Dictionary Comprehensions
Similar to list comprehensions, but create dictionaries.
Basic Dictionary Comprehension
Transforming Dictionaries
Dictionary from Rows
Filtering Dictionary Comprehensions
Set Comprehensions
Python also supports set comprehensions:
Practical Functional Programming Examples
Data Transformation Pipeline
Text Processing
Data Analysis with Functional Approach
Key Takeaways
- Pure Functions: Functions with no side effects that always return the same output for the same input
- Immutability: Prefer creating new data over modifying existing data
- map(): Apply a function to every element in a sequence
- filter(): Select elements that match a condition
- reduce(): Combine elements into a single value
- List Comprehensions: Concise, readable syntax for creating lists
- Dictionary Comprehensions: Create dictionaries with comprehension syntax
- Composition: Combine simple functions to build complex transformations
Functional programming makes code more predictable, testable, and often more concise. While Python isn't a purely functional language, it provides excellent tools for functional-style programming when appropriate.
Quiz
Further Reading
📝 Test Your Knowledge: Chapter 8: Functional Programming
Take this quiz to reinforce what you've learned in this chapter.