Book description
“Using this book has propelled my efficiency in solving leetcode challenges.” - Discord user, zizu Python isn't all about object-oriented programming. Discover a valuable way of thinking about code design through a function-first approach – and learn when you need to use it. Now with detailed exercises at the end of every chapter! Purchase of the print or Kindle book includes a free eBook in PDF format.
Key Features
- Learn how, when, and why to adopt functional elements in your projects
- Explore the Python modules essential to functional programming, like itertools and functools
- Cover examples relevant to mathematical, statistical, and data analysis domains
Book Description
Not enough developers understand the benefits of functional programming, or even what it is. Author Steven Lott demystifies the approach, teaching you how to improve the way you code in Python and make gains in memory use and performance. If you’re a leetcoder preparing for coding interviews, this book is for you.
Starting from the fundamentals, this book shows you how to apply functional thinking and techniques in a range of scenarios, with Python 3.10+ examples focused on mathematical and statistical algorithms, data cleaning, and exploratory data analysis. You'll learn how to use generator expressions, list comprehensions, and decorators to your advantage. You don't have to abandon object-oriented design completely, though – you'll also see how Python's native object orientation is used in conjunction with functional programming techniques.
By the end of this book, you'll be well-versed in the essential functional programming features of Python and understand why and when functional thinking helps. You'll also have all the tools you need to pursue any additional functional topics that are not part of the Python language.
What you will learn
- Use Python's libraries to avoid the complexities of state-changing classes
- Leverage built-in higher-order functions to avoid rewriting common algorithms
- Write generator functions to create lazy processing
- Design and implement decorators for functional composition
- Make use of Python type annotations to describe parameters and results of functions
- Apply functional programming to concurrency and web services
- Explore the PyMonad library for stateful simulations
Who this book is for
The functional paradigm is very useful for programmers working in data science or preparing for technical interviews, but any Python developer who wants to create more reliable, succinct, and expressive code will have much to learn from this book. No prior knowledge of functional programming is required to get started, though Python programming knowledge is assumed. A running Python environment is essential.
Table of contents
- Preface
- Chapter 1: Understanding Functional Programming
-
Chapter 2: Introducing Essential Functional Concepts
- 2.1 Functions as first-class objects
- 2.2 Immutable data
- 2.3 Strict and non-strict evaluation
- 2.4 Lazy and eager evaluation
- 2.5 Recursion instead of an explicit loop state
- 2.6 Functional type systems
- 2.7 Familiar territory
- 2.8 Learning some advanced concepts
- 2.9 Summary
- 2.10 Exercises
- Join our community Discord space
-
Chapter 3: Functions, Iterators, and Generators
- 3.1 Writing pure functions
- 3.2 Functions as first-class objects
- 3.3 Using strings
- 3.4 Using tuples and named tuples
- 3.5 Using generator expressions
- 3.6 Cleaning raw data with generator functions
- 3.7 Applying generators to built-in collections
- 3.8 Summary
- 3.9 Exercises
- Join our community Discord space
-
Chapter 4: Working with Collections
- 4.1 An overview of function varieties
- 4.2 Working with iterables
- 4.3 Using any() and all() as reductions
- 4.4 Using len() and sum() on collections
- 4.5 Using zip() to structure and flatten sequences
- 4.6 Using sorted() and reversed() to change the order
- 4.7 Using enumerate() to include a sequence number
- 4.8 Summary
- 4.9 Exercises
- Join our community Discord space
-
Chapter 5: Higher-Order Functions
- 5.1 Using max() and min() to find extrema
- 5.2 Using the map() function to apply a function to a collection
- 5.3 Using the filter() function to pass or reject data
- 5.4 The iter() function with a sentinel value
- 5.5 Using sorted() to put data in order
- 5.6 Overview of writing higher-order functions
- 5.7 Writing higher-order mappings and filters
- 5.8 Building higher-order functions with callables
- 5.9 Review of some design patterns
- 5.10 Summary
- 5.11 Exercises
- Join our community Discord space
- Chapter 6: Recursions and Reductions
-
Chapter 7: Complex Stateless Objects
- 7.1 Using tuples to collect data
- 7.2 Using NamedTuple to collect data
- 7.3 Using frozen dataclasses to collect data
- 7.4 Complicated object initialization and property computations
- 7.5 Using pyrsistent to collect data
- 7.6 Avoiding stateful classes by using families of tuples
- 7.7 Polymorphism and type pattern matching
- 7.8 Summary
- 7.9 Exercises
- Join our community Discord space
- Chapter 8: The Itertools Module
- Chapter 9: Itertools for Combinatorics – Permutations and Combinations
-
Chapter 10: The Functools Module
- 10.1 Function tools
- 10.2 Memoizing previous results with cache
- 10.3 Defining classes with total ordering
- 10.4 Applying partial arguments with partial()
- 10.5 Reducing sets of data with the reduce() function
- 10.6 Handling multiple types with singledispatch
- 10.7 Summary
- 10.8 Exercises
- Join our community Discord space
- Chapter 11: The Toolz Package
- Chapter 12: Decorator Design Techniques
- Chapter 13: The PyMonad Library
- Chapter 14: The Multiprocessing, Threading, and Concurrent.Futures Modules
- Chapter 15: A Functional Approach to Web Services
- Other Books You Might Enjoy
- Index
Product information
- Title: Functional Python Programming - Third Edition
- Author(s):
- Release date: December 2022
- Publisher(s): Packt Publishing
- ISBN: 9781803232577
You might also like
book
Advanced Python Programming - Second Edition
Write fast, robust, and highly reusable applications using Python's internal optimization, state-of-the-art performance-benchmarking tools, and cutting-edge …
book
Python Programming with Design Patterns
Improve Your Python Code with Modern Object-Oriented Design Patterns To write clean, efficient, maintainable code, developers …
book
Pythonic Programming
Make your good Python code even better by following proven and effective pythonic programming tips. Avoid …
book
Learn Python Programming - Third Edition
Get up and running with Python 3.9 through concise tutorials and practical projects in this fully …