Python in a Nutshell, 4th Edition

Book description

Python was recently ranked as today's most popular programming language on the TIOBE index, thanks to its broad applicability to design and prototyping to testing, deployment, and maintenance. With this updated fourth edition, you'll learn how to get the most out of Python, whether you're a professional programmer or someone who needs this language to solve problems in a particular field.

Carefully curated by recognized experts in Python, this new edition focuses on version 3.10, bringing this seminal work on the Python language fully up to date on five version releases, including preview coverage of upcoming 3.11 features.

This handy guide will help you:

  • Learn how Python represents data and program as objects
  • Understand the value and uses of type annotations
  • Examine which language features appeared in which recent versions
  • Discover how to use modern Python idiomatically
  • Learn ways to structure Python projects appropriately
  • Understand how to debug Python code

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. How To Use This Book
      1. Part I, Getting Started with Python
      2. Part II, Core Python Language and Built-ins
      3. Part III, Python Library and Extension Modules
      4. Part IV, Network and Web Programming
      5. Part V, Extending, Distributing, and Version Upgrade and Migration
    2. Conventions Used in This Book
      1. Reference Conventions
      2. Version Conventions
      3. Typographic Conventions
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Introduction to Python
    1. The Python Language
    2. The Python Standard Library and Extension Modules
    3. Python Implementations
      1. CPython
      2. PyPy
      3. Choosing Between CPython, PyPy, and Other Implementations
      4. Other Developments, Implementations, and Distributions
      5. Licensing and Price Issues
    4. Python Development and Versions
    5. Python Resources
      1. Documentation
      2. Community
    6. Installation
    7. Installing Python from Binaries
    8. Installing Python from Source Code
      1. Microsoft Windows
      2. Unix-Like Platforms
  3. 2. The Python Interpreter
    1. The python Program
      1. Environment Variables
      2. Command-Line Syntax and Options
      3. The Windows py Launcher
      4. The PyPy Interpreter
      5. Interactive Sessions
    2. Python Development Environments
      1. IDLE
      2. Other Python IDEs
      3. Free Text Editors with Python Support
      4. Tools for Checking Python Programs
    3. Running Python Programs
    4. Running Python in the Browser
      1. PyScript
      2. Jupyter
  4. 3. The Python Language
    1. Lexical Structure
      1. Lines and Indentation
      2. Character Sets
      3. Tokens
      4. Statements
    2. Data Types
      1. Numbers
      2. Sequences
      3. Sets
      4. Dictionaries
      5. None
      6. Ellipsis (...)
      7. Callables
      8. Boolean Values
    3. Variables and Other References
      1. Variables
      2. Assignment Statements
      3. del Statements
    4. Expressions and Operators
      1. Comparison Chaining
      2. Short-Circuiting Operators
      3. Assignment Expressions
    5. Numeric Operations
      1. Numeric Conversions
      2. Arithmetic Operations
      3. Bitwise Operations on Integers
    6. Sequence Operations
      1. Sequences in General
      2. Strings
      3. Tuples
      4. Lists
    7. Set Operations
      1. Set Membership
      2. Set Methods
    8. Dictionary Operations
      1. Dictionary Membership
      2. Indexing a Dictionary
      3. Dictionary Methods
    9. Control Flow Statements
      1. The if Statement
      2. The match Statement
      3. The while Statement
      4. The for Statement
      5. The break Statement
      6. The continue Statement
      7. The else Clause on Loop Statements
      8. The pass Statement
      9. The try and raise Statements
      10. The with Statement
    10. Functions
      1. Defining Functions: The def Statement
      2. Parameters
      3. Attributes of Function Objects
      4. Function Annotations
      5. The return Statement
      6. Calling Functions
      7. Namespaces
      8. lambda Expressions
      9. Generators
      10. Recursion
  5. 4. Object-Oriented Python
    1. Classes and Instances
      1. Python Classes
      2. The class Statement
      3. The Class Body
      4. Descriptors
      5. Instances
      6. Attribute Reference Basics
      7. Bound and Unbound Methods
      8. Inheritance
      9. The Built-in object Type
      10. Class-Level Methods
      11. Properties
      12. __slots__
      13. __getattribute__
      14. Per Instance Methods
      15. Inheritance from Built-in Types
    2. Special Methods
      1. General-Purpose Special Methods
      2. Special Methods for Containers
      3. Abstract Base Classes
      4. Special Methods for Numeric Objects
    3. Decorators
    4. Metaclasses
      1. Alternatives to Custom Metaclasses for Simple Class Customization
      2. How Python Determines a Class’s Metaclass
      3. How a Metaclass Creates a Class
      4. Data Classes
      5. Enumerated Types (Enums)
  6. 5. Type Annotations
    1. History
    2. Type-Checking Utilities
      1. mypy
      2. Other Type Checkers
    3. Type Annotation Syntax
    4. The typing Module
      1. Types
      2. Type Expression Parameters
      3. Abstract Base Classes
      4. Protocols
      5. Utilities and Decorators
      6. Defining Custom Types
    5. Using Type Annotations at Runtime
    6. How to Add Type Annotations to Your Code
      1. Adding Type Annotations to New Code
      2. Adding Type Annotations to Existing Code (Gradual Typing)
      3. Using .pyi Stub Files
    7. Summary
  7. 6. Exceptions
    1. The try Statement
      1. try/except
      2. try/finally
      3. try/except/finally
    2. The raise Statement
    3. The with Statement and Context Managers
    4. Generators and Exceptions
    5. Exception Propagation
    6. Exception Objects
      1. The Hierarchy of Standard Exceptions
      2. Standard Exception Classes
    7. Custom Exception Classes
      1. Custom Exceptions and Multiple Inheritance
      2. Other Exceptions Used in the Standard Library
    8. ExceptionGroup and except*
    9. Error-Checking Strategies
      1. LBYL Versus EAFP
      2. Handling Errors in Large Programs
      3. Logging Errors
    10. The assert Statement
  8. 7. Modules and Packages
    1. Module Objects
      1. The import Statement
      2. The from Statement
    2. Module Loading
      1. Built-in Modules
      2. Searching the Filesystem for a Module
      3. The Main Program
      4. Reloading Modules
      5. Circular Imports
      6. Custom Importers
    3. Packages
      1. Special Attributes of Package Objects
      2. Absolute Versus Relative Imports
    4. Distribution Utilities (distutils) and setuptools
    5. Python Environments
      1. Enter the Virtual Environment
      2. What Is a Virtual Environment?
      3. Creating and Deleting Virtual Environments
      4. Working with Virtual Environments
      5. Managing Dependency Requirements
      6. Other Environment Management Solutions
      7. Best Practices with Virtualenvs
  9. 8. Core Built-ins and Standard Library Modules
    1. Built-in Types
    2. Built-in Functions
    3. The sys Module
    4. The copy Module
    5. The collections Module
      1. ChainMap
      2. Counter
      3. OrderedDict
      4. defaultdict
      5. deque
    6. The functools Module
    7. The heapq Module
      1. The Decorate–Sort–Undecorate Idiom
    8. The argparse Module
    9. The itertools Module
  10. 9. Strings and Things
    1. Methods of String Objects
    2. The string Module
    3. String Formatting
      1. Formatted String Literals (F-Strings)
      2. Formatting Using format Calls
      3. Value Conversion
      4. Value Formatting: The Format Specifier
      5. Nested Format Specifications
      6. Formatting of User-Coded Classes
      7. Legacy String Formatting with %
      8. Format Specifier Syntax
    4. Text Wrapping and Filling
    5. The pprint Module
    6. The reprlib Module
    7. Unicode
      1. The codecs Module
      2. The unicodedata Module
  11. 10. Regular Expressions
    1. Regular Expressions and the re Module
      1. REs and bytes Versus str
      2. Pattern String Syntax
      3. Common Regular Expression Idioms
      4. Sets of Characters
      5. Alternatives
      6. Groups
    2. Optional Flags
    3. Match Versus Search
    4. Anchoring at String Start and End
    5. Regular Expression Objects
    6. Match Objects
    7. Functions of the re Module
    8. REs and the := Operator
    9. The Third-Party regex Module
  12. 11. File and Text Operations
    1. The io Module
      1. Creating a File Object with open
      2. Attributes and Methods of File Objects
      3. Iteration on File Objects
      4. File-Like Objects and Polymorphism
    2. The tempfile Module
    3. Auxiliary Modules for File I/O
      1. The fileinput Module
      2. The struct Module
    4. In-Memory Files: io.StringIO and io.BytesIO
    5. Archived and Compressed Files
      1. The tarfile Module
      2. The zipfile Module
    6. The os Module
      1. Filesystem Operations
      2. The os.path Module
      3. OSError Exceptions
    7. The errno Module
    8. The pathlib Module
    9. The stat Module
    10. The filecmp Module
    11. The fnmatch Module
    12. The glob Module
    13. The shutil Module
    14. Text Input and Output
      1. Standard Output and Standard Error
      2. The print Function
      3. Standard Input
      4. The getpass Module
    15. Richer-Text I/O
      1. The readline Module
      2. Console I/O
    16. Internationalization
      1. The locale Module
      2. The gettext Module
      3. More Internationalization Resources
  13. 12. Persistence and Databases
    1. Serialization
      1. The csv Module
      2. The json Module
      3. The pickle Module
      4. The shelve Module
    2. DBM Modules
      1. The dbm Package
      2. Examples of DBM-Like File Use
    3. The Python Database API (DBAPI)
      1. Exception Classes
      2. Thread Safety
      3. Parameter Style
      4. Factory Functions
      5. Type Description Attributes
      6. The connect Function
      7. Connection Objects
      8. Cursor Objects
      9. DBAPI-Compliant Modules
      10. SQLite
  14. 13. Time Operations
    1. The time Module
    2. The datetime Module
      1. The date Class
      2. The time Class
      3. The datetime Class
      4. The timedelta Class
      5. The tzinfo Abstract Class
      6. The timezone Class
    3. The zoneinfo Module
    4. The dateutil Module
    5. The sched Module
    6. The calendar Module
  15. 14. Customizing Execution
    1. Per-Site Customization
    2. Termination Functions
    3. Dynamic Execution and exec
      1. Avoiding exec
      2. Expressions
      3. compile and Code Objects
      4. Never exec or eval Untrusted Code
    4. Internal Types
      1. Type Objects
      2. The Code Object Type
      3. The Frame Type
    5. Garbage Collection
      1. The gc Module
      2. The weakref Module
  16. 15. Concurrency: Threads and Processes
    1. Threads in Python
    2. The threading Module
      1. Thread Objects
      2. Thread Synchronization Objects
      3. Thread Local Storage
    3. The queue Module
    4. The multiprocessing Module
      1. Differences Between multiprocessing and threading
      2. Sharing State: Classes Value, Array, and Manager
      3. Process Pools
    5. The concurrent.futures Module
    6. Threaded Program Architecture
    7. Process Environment
    8. Running Other Programs
      1. Using the Subprocess Module
      2. Running Other Programs with the os Module
    9. The mmap Module
      1. Methods of mmap Objects
      2. Using mmap Objects for IPC
  17. 16. Numeric Processing
    1. Floating-Point Values
    2. The math and cmath Modules
    3. The statistics Module
    4. The operator Module
    5. Random and Pseudorandom Numbers
      1. The random Module
      2. Crypto-Quality Random Numbers: The secrets Module
    6. The fractions Module
    7. The decimal Module
    8. Array Processing
      1. The array Module
      2. Extensions for Numeric Array Computation
  18. 17. Testing, Debugging, and Optimizing
    1. Testing
      1. Unit Testing and System Testing
      2. The doctest Module
      3. The unittest Module
      4. Testing with nose2
      5. Testing with pytest
    2. Debugging
      1. Before You Debug
      2. The inspect Module
      3. The traceback Module
      4. The pdb Module
      5. Other Debugging Modules
    3. The warnings Module
      1. Classes
      2. Objects
      3. Filters
      4. Functions
    4. Optimization
      1. Developing a Fast-Enough Python Application
      2. Benchmarking
      3. Large-Scale Optimization
      4. Profiling
      5. Small-Scale Optimization
  19. 18. Networking Basics
    1. The Berkeley Socket Interface
      1. Socket Addresses
      2. Client/Server Computing
      3. The socket Module
      4. Socket Objects
      5. A Connectionless Socket Client
      6. A Connectionless Socket Server
      7. A Connection-Oriented Socket Client
      8. A Connection-Oriented Socket Server
    2. Transport Layer Security
    3. SSLContext
  20. 19. Client-Side Network Protocol Modules
    1. Email Protocols
      1. The poplib Module
      2. The smtplib Module
    2. HTTP and URL Clients
      1. URL Access
      2. The urllib Package
      3. The Third-Party requests Package
    3. Other Network Protocols
  21. 20. Serving HTTP
    1. http.server
    2. WSGI
      1. WSGI Servers
      2. ASGI
    3. Python Web Frameworks
      1. “Full-Stack” Versus “Lightweight” Frameworks
      2. A Few Popular Full-Stack Frameworks
      3. Considerations When Using Lightweight Frameworks
      4. A Few Popular Lightweight Frameworks
  22. 21. Email, MIME, and Other Network Encodings
    1. MIME and Email Format Handling
      1. Functions in the email Package
      2. The email.message Module
      3. The email.Generator Module
      4. Creating Messages
      5. The email.encoders Module
      6. The email.utils Module
      7. Example Uses of the email Package
    2. Encoding Binary Data as ASCII Text
      1. The base64 Module
      2. The quopri Module
      3. The uu Module
  23. 22. Structured Text: HTML
    1. The html.entities Module
    2. The BeautifulSoup Third-Party Package
      1. The BeautifulSoup Class
      2. The Navigable Classes of bs4
      3. bs4 find… Methods (aka Search Methods)
      4. bs4 CSS Selectors
      5. An HTML Parsing Example with BeautifulSoup
    3. Generating HTML
      1. Editing and Creating HTML with bs4
      2. Building HTML with bs4
      3. Templating
      4. The jinja2 Package
  24. 23. Structured Text: XML
    1. ElementTree
      1. The Element Class
      2. The ElementTree Class
      3. Functions in the ElementTree Module
    2. Parsing XML with ElementTree.parse
      1. Selecting Elements from an ElementTree
      2. Editing an ElementTree
    3. Building an ElementTree from Scratch
    4. Parsing XML Iteratively
  25. 24. Packaging Programs and Extensions
    1. What We Don’t Cover in This Chapter
    2. A Brief History of Python Packaging
    3. Online Material
  26. 25. Extending and Embedding Classic Python
    1. Online Material
  27. 26. v3.7 to v3.n Migration
    1. Significant Changes in Python Through 3.11
    2. Planning a Python Version Upgrade
      1. Choosing a Target Version
      2. Scoping the Work
      3. Applying the Code Changes
      4. Upgrade Automation Using pyupgrade
      5. Multiversion Testing
      6. Use a Controlled Deployment Process
      7. How Often Should You Upgrade?
    3. Summary
  28. Appendix. New Features and Changes in Python 3.7 Through 3.11
    1. Python 3.7
    2. Python 3.8
    3. Python 3.9
    4. Python 3.10
    5. Python 3.11
  29. Index
  30. About the Authors

Product information

  • Title: Python in a Nutshell, 4th Edition
  • Author(s): Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire
  • Release date: January 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098113551