Functional Programming

10. Functional Programming#

Functional programming is a style of writing programs as transformations from inputs to outputs.

  • Pure functions and side effects

  • Immutable data and returned values

  • Functions as values

  • Comprehensions, lambdas, and higher-order functions

  • Decorators, recursion, context managers, and functools

Python is not a purely functional language, but it includes functional tools that help you write concise, reusable, and testable code.

Video

This overview introduces the idea that functions are values that can be assigned, passed, and returned.

Learning Goals

By the end of this chapter, you will be able to:

  1. Identify pure functions and avoid unnecessary side effects

  2. Use immutability by returning new values instead of mutating existing objects

  3. Treat functions as first-class values that can be passed to and returned from other functions

  4. Use lambda functions with map(), filter(), and sorted()

  5. Build decorators, recursive functions, context managers, and selected functools utilities

Chapter Flow

Chapter Overview Slides