What this book covers
There are over 130 recipes in this book. We can decompose them into four general areas:
Python Fundamentals
Chapter 1, Numbers, Strings, and Tuples, will look at the different kinds of numbers, how to work with strings, how to use tuples, and how to use the essential built-in types in Python. We will also show ways to exploit the full power of the Unicode character set.
Chapter 2, Statements and Syntax, will cover some basics of creating script files. Then we’ll move on to looking at some of the complex statements, including if, while, for, break, try, raise, and with.
Chapter 3, Function Definitions, will look at a number of function definition techniques. We’ll devote several recipes to type hints for a variety of types. We’ll also address an element of designing a testable script by using functions and a main-import-switch.
Chapter 4, Built-In Data Structures Part 1: Lists and Sets, begins an overview of the built-in data structures structures that are available and what problems they solve. This includes a number of recipes showing list and set operations, including list and set comprehensions.
Chapter 5, Built-In Data Structures Part 2: Dictionaries, continues examining the built-in data structures, looking at dictionaries in detail. This chapter will also look at some more advanced topics related to how Python handles references to objects. It also shows how to handle mutable objects as function parameter default values.
Chapter 6, User Inputs and Outputs, explains how to use the different features of the print() function. We’ll also look at the different functions used to provide user input. The use of f-strings for debugging and the argparse module for command-line input are featured.
Object-Oriented and Functional Design Approaches
Chapter 7, Basics of Classes and Objects, begins the coverage of object-oriented programming. It shows how to create classes and the type hints related to class definitions. This section has been expanded from previous editions to cover dataclasses. It shows how to extend built-in classes, and how to create context managers to manage resources.
Chapter 8, More Advanced Class Design, continues the exploration of object-oriented design and programming. This includes an exploration of the composition vs. inheritance question, and shows how to manage the “duck typing” principle of Python.
Chapter 9, Functional Programming Features, looks at Python’s functional programming features. This style of programming emphasizes function definitions and stateless, immutable objects. The recipes look at generator expressions, using the map(), filter(), and reduce() functions. We also look at ways to create partial functions and some examples of replacing stateful objects with data structures built from collections of immutable objects.
More Sophisticated Designs
Chapter 10, Working with Type Matching and Annotations, looks more closely at type hints and the match statement. This includes using Pydantic to create classes with more strict run-time type-checking. It also looks at introspection of annotated types.
Chapter 11, Input/Output, Physical Format, and Logical Layout, will work with paths and files in general. It will look at reading and writing data in a variety of file formats, including CSV, JSON (and YAML), XML, and HTML. The HTML section will emphasize using Beautiful Soup for extracting data.
Chapter 12, Graphics and Visualization with Jupyter Lab, will use Jupyter Lab to create notebooks that use Python for data analysis and visualization. This will show ways to ingest data into a notebook to create plots, and how to use Markdown to create useful documentation and reports from a notebook.
Chapter 13, Application Integration: Configuration, will start looking at ways that we can design larger applications. The recipes in this chapter address different ways to handle configuration files and how to manage logging.
Chapter 14, Application Integration: Combination, will continue looking at ways to create composite applications from smaller pieces. This will look at object-oriented design patterns and Command-Line Interface (CLI) applications. It will also look at using the subprocess module to run existing applications under Python’s control.
Completing a Project: Fit and Finish
Chapter 15, Testing, provides recipes for using the built-in doctest and unittest testing frameworks used in Python. Additionally, recipes will cover the pytest tool.
Chapter 16, Dependencies and Virtual Environments, covers tools used to manage virtual environments. The built-in venv, as well as conda and poetry will be covered. There are a lot of solutions to managing virtual environments, and we can’t cover all of them.
Chapter 17, Documentation and Style, covers additional tools that can help to create high-quality software. This includes a particular focus on sphinx for creating comprehensive, readable documentation. We’ll also look at tox to automate running tests.