liveBook · Manning

What you will learn in The Quick Python Book, Fourth Edition

by Naomi Ceder

Chapter by chapter

Chapter 1: About Python

The introduction sets the book’s scope and explains why coding knowledge matters when using AI-generated code.

1.1 Why should I use Python? · 1.2 What Python does well · 1.2.1 Python is easy to use · 1.2.2 Python is expressive · 1.2.3 Python is readable · 1.2.4 Python is complete: “Batteries included” · 1.2.5 Python has a rich ecosystem of third-party libraries · 1.2.6 Python is cross-platform · 1.2.7 Python is free · 1.3 What Python is improving · 1.3.1 Python is getting faster · 1.3.2 Python doesn’t enforce variable types at compile time · 1.3.3 Python is improving mobile support · 1.3.4 Python is improving support for multiple processors · 1.4 Summary

What you learn in chapter 1 →

Chapter 2: Getting started

The chapter introduces Google Colaboratory and the book’s GitHub code as a quick way to start running Python examples and lab exercises.

2.1 Paradox of choice: Which Python? · 2.1.1 Python versions · 2.1.2 Sources of Python · 2.1.3 Devices, platforms, and operating systems · 2.1.4 Environments and tools · 2.2 Colaboratory: Jupyter notebooks in the cloud · 2.2.1 Getting the source notebooks · 2.2.2 Getting started with Colaboratory · 2.2.3 Colaboratory’s Python version · 2.3 Writing and running code in Colaboratory · 2.3.1 Hello, World · 2.3.2 Dealing with errors in Jupyter · 2.4 Using help and dir to explore Python · 2.5 Using AI tools to write Python code · 2.5.1 Benefits of AI tools · 2.5.2 Negatives of using AI tools · 2.5.3 AI options · 2.6 Summary

What you learn in chapter 2 →

Chapter 3: The quick Python overview

This chapter introduction frames the coming Python overview as a first impression rather than a detailed lesson, with specifics covered in later chapters.

3.1 Python synopsis · 3.2 Built-in data types · 3.2.1 Numbers · 3.2.2 Lists · 3.2.3 Tuples · 3.2.4 Strings · 3.2.5 Dictionaries · 3.2.6 Sets, frozensets · 3.2.7 File objects · 3.3 Type hints in Python · 3.4 Control flow structures · 3.4.1 Boolean values and expressions · 3.4.2 The if-elif-else statement · 3.4.3 Structural pattern matching with match · 3.4.4 The while loop · 3.4.5 The for loop · 3.4.6 Function definition · 3.4.7 Exceptions · 3.4.8 Context handling using the with keyword · 3.5 Module creation · 3.6 Object-oriented programming · 3.7 Summary

What you learn in chapter 3 →

Chapter 4: The absolute basics

This chapter introduction previews the Python basics covered in the chapter.

4.1 Indentation and block structuring · 4.2 Differentiating comments · 4.3 Variables and assignments · 4.4 Optional type hints in Python · 4.4.1 Why use type hints? · 4.4.2 Why not use type hints? · 4.4.3 Progressive typing · 4.5 Expressions · 4.6 Strings · 4.7 Numbers · 4.7.1 Built-in numeric functions · 4.7.2 Advanced numeric functions · 4.7.3 Numeric computation · 4.7.4 Complex numbers · 4.7.5 Advanced complex-number functions · 4.8 The None value · 4.9 Getting input from the user · 4.10 Built-in operators · 4.11 Basic Python style · 4.12 Summary

What you learn in chapter 4 →

Chapter 5: Lists, tuples, and sets

Introduces lists and tuples as Python sequence types and contrasts them with sets, where membership matters more than position.

5.1 Lists are like arrays · 5.2 List indices · 5.3 Modifying lists · 5.4 Sorting lists · 5.4.1 Custom sorting · 5.4.2 The sorted () function · 5.5 Other common list operations · 5.5.1 List membership with the in operator · 5.5.2 List concatenation with the + operator · 5.5.3 List initialization with the * operator · 5.5.4 List minimum or maximum with min and max · 5.5.5 List search with index · 5.5.6 List matches with count · 5.5.7 Summary of list operations · 5.6 Nested lists and deep copies · 5.7 Tuples · 5.7.1 Tuple basics · 5.7.2 One-element tuples need a comma · 5.7.3 Packing and unpacking tuples · 5.7.4 Converting between lists and tuples · 5.8 Sets · 5.8.1 Set operations · 5.8.2 Frozen sets · 5.9 Lab: Examining a list · 5.9.1 Why solve it the old-fashioned way? · 5.9.2 Solving the problem with AI code generation · 5.9.3 Solutions and discussion · 5.10 Summary

What you learn in chapter 5 →

Chapter 6: Strings

Introduces the chapter’s focus on handling and formatting text in Python.

6.1 Strings as sequences of characters · 6.2 Basic string operations · 6.3 Special characters and escape sequences · 6.3.1 Basic escape sequences · 6.3.2 Numeric (octal and hexadecimal) and Unicode escape sequences · 6.3.3 Printing vs. evaluating strings with special characters · 6.4 String methods · 6.4.1 The split and join string methods · 6.4.2 Converting strings to numbers · 6.4.3 Getting rid of extra whitespace · 6.4.4 String searching · 6.4.5 Modifying strings · 6.4.6 Modifying strings with list manipulations · 6.4.7 Useful methods and constants · 6.5 Converting objects to strings · 6.6 Using the format method · 6.6.1 The format method and positional parameters · 6.6.2 The format method and named parameters · 6.6.3 Format specifiers · 6.7 String interpolation with f-strings · 6.8 Formatting strings with % · 6.8.1 Using formatting sequences · 6.8.2 Named parameters and formatting sequences · 6.9 Bytes · 6.10 Preprocessing text · 6.10.1 Solving the problem with AI-generated code · 6.10.2 Solutions and discussion · 6.11 Summary

What you learn in chapter 6 →

Chapter 7: Dictionaries

Introduces dictionaries as Python’s useful associative arrays or maps, implemented using hash tables.

7.1 What is a dictionary? · 7.2 Other dictionary operations · 7.3 Word counting · 7.4 What can be used as a key? · 7.5 Sparse matrices · 7.6 Dictionaries as caches · 7.7 Efficiency of dictionaries · 7.8 Word counting · 7.8.1 Solving the problem with AI-generated code · 7.8.2 Solutions and discussion · 7.9 Summary

What you learn in chapter 7 →

Chapter 8: Control flow

Introduces the chapter’s coverage of Python control flow, including conditionals, loops, comprehensions, indentation, and Boolean expressions.

8.1 The if-elif-else statement · 8.2 Structural pattern matching with match · 8.3 The while loop · 8.4 The for loop · 8.4.1 The range function · 8.5 Controlling range with starting and stepping values · 8.6 The for loop and tuple unpacking · 8.7 The enumerate function · 8.8 The zip function · 8.9 List, set, and dictionary comprehensions · 8.9.1 Generator expressions · 8.10 Statements, blocks, and indentation · 8.11 Boolean values and expressions · 8.11.1 Most Python objects can be used as Booleans · 8.11.2 Comparison and Boolean operators · 8.12 Writing a simple program to analyze a text file · 8.13 Refactoring word_count · 8.13.1 Solving the problem with AI-generated code · 8.13.2 Solutions and discussion · 8.14 Summary

What you learn in chapter 8 →

Chapter 9: Functions

This introduction previews the chapter’s function topics and states that readers are expected to know basic function concepts from another language.

9.1 Basic function definitions · 9.2 Function parameter options · 9.2.1 Positional parameters · 9.2.2 Passing arguments by parameter name · 9.2.3 Variable numbers of arguments · 9.2.4 Mixing argument-passing techniques · 9.3 Mutable objects as arguments · 9.3.1 Mutable objects as default values · 9.4 Local, nonlocal, and global variables · 9.5 Assigning functions to variables · 9.6 lambda expressions · 9.7 Generator functions · 9.8 Decorators · 9.9 Useful functions · 9.9.1 Solving the problem with AI-generated code · 9.9.2 Solutions and discussion · 9.10 Summary

What you learn in chapter 9 →

Chapter 10: Modules and scoping rules

This introduction explains why modules are useful for organizing larger or reusable Python code.

10.1 What is a module? · 10.2 A first module · 10.3 The import statement · 10.4 The module search path · 10.4.1 Where to place your own modules · 10.5 Private names in modules · 10.6 Library and third-party modules · 10.7 Python scoping rules and namespaces · 10.7.1 The built-in namespace · 10.8 Creating a module · 10.8.1 Solving the problem with AI-generated code · 10.8.2 Solutions and discussion · 10.9 Summary

What you learn in chapter 10 →

Chapter 11: Python programs

This introduction explains why command-line Python scripts are worth learning after interactive work in Colaboratory, especially for processing many files.

11.1 Creating a very basic program · 11.1.1 Starting a script from a command line · 11.1.2 Command-line arguments · 11.1.3 Executing code only as main script · 11.1.4 Redirecting the input and output of a script · 11.1.5 The argparse module · 11.1.6 Using the fileinput module · 11.2 Running scripts in different operating systems · 11.2.1 Making a script directly executable on UNIX · 11.2.2 Scripts on macOS · 11.2.3 Script execution options in Windows · 11.3 Programs and modules · 11.4 Distributing Python applications · 11.4.1 Wheels packages · 11.4.2 zipapp and pex · 11.4.3 py2exe and py2app · 11.4.4 Creating executable programs with freeze · 11.5 Creating a program · 11.5.1 Solving the problem with AI-generated code · 11.5.2 Solutions and discussion · 11.6 Summary

What you learn in chapter 11 →

Chapter 12: Using the filesystem

The introduction distinguishes filesystem work from file I/O and explains why portable file references matter when working across operating systems.

12.1 os and os.path vs. pathlib · 12.2 Paths and pathnames · 12.2.1 Absolute and relative paths · 12.2.2 The current working directory · 12.2.3 Accessing directories with pathlib · 12.2.4 Manipulating pathnames · 12.2.5 Manipulating pathnames with pathlib · 12.2.6 Useful constants and functions · 12.3 Getting information about files · 12.3.1 Getting information about files with scandir · 12.4 More filesystem operations · 12.4.1 More filesystem operations with pathlib · 12.5 Processing all files in a directory subtree · 12.6 More file operations · 12.6.1 Solving the problem with AI-generated code · 12.6.2 Solutions and discussion · 12.7 Summary

What you learn in chapter 12 →

Chapter 13: Reading and writing files

This introduction explains why developers work with text and binary files and identifies Python modules used for particular kinds of binary storage.

13.1 Opening files and file objects · 13.2 Closing files · 13.3 Opening files in write or other modes · 13.4 Functions to read and write text or binary data · 13.4.1 Using binary mode · 13.5 Reading and writing with pathlib · 13.6 Terminal input/output and redirection · 13.7 Handling structured binary data with the struct module · 13.8 Pickling objects to files · 13.8.1 Reasons not to pickle · 13.9 Shelving objects · 13.10 Final fixes to wc · 13.10.1 Solving the problem with AI-generated code · 13.10.2 Solutions and discussion · 13.11 Summary

What you learn in chapter 13 →

Chapter 14: Exceptions

The chapter introduces exceptions as Python’s way to handle unusual circumstances during program execution.

14.1 Introduction to exceptions · 14.1.1 General philosophy of errors and exception handling · 14.1.2 A more formal definition of exceptions · 14.1.3 Handling different types of exceptions · 14.2 Exceptions in Python · 14.2.1 Types of Python exceptions · 14.2.2 Raising exceptions · 14.2.3 Catching and handling exceptions · 14.2.4 Defining new exceptions · 14.2.5 Exception groups · 14.2.6 Debugging programs with the assert statement · 14.2.7 The exception inheritance hierarchy · 14.2.8 Example: A disk-writing program in Python · 14.2.9 Example: Exceptions in normal evaluation · 14.2.10 Where to use exceptions · 14.3 Context managers using the with keyword · 14.4 Adding exceptions · 14.4.1 Solving the problem with AI-generated code · 14.4.2 Solutions and discussion · 14.5 Summary

What you learn in chapter 14 →

Chapter 15: Classes and object-oriented programming

The chapter introduces Python classes as constructs that can hold both data and code, while leaving the details to later sections.

15.1 Defining classes · 15.1.1 Using a class instance as a structure or record · 15.2 Instance variables · 15.3 Methods · 15.4 Class variables · 15.4.1 An oddity with class variables · 15.5 Static methods and class methods · 15.5.1 Static methods · 15.5.2 Class methods · 15.6 Inheritance · 15.7 Inheritance with class and instance variables · 15.8 Recap: Basics of Python classes · 15.9 Private variables and private methods · 15.10 Using @property for more flexible instance variables · 15.11 Scoping rules and namespaces for class instances · 15.12 Destructors and memory management · 15.13 Multiple inheritance · 15.14 HTML classes · 15.14.1 Solving the problem with AI-generated code · 15.14.2 Solutions and discussion · 15.15 Summary

What you learn in chapter 15 →

Chapter 16: Regular expressions

The introduction explains why regular expressions deserve a place in a Python book despite being an advanced feature.

16.1 What is a regular expression? · 16.2 Regular expressions with special characters · 16.3 Regular expressions and raw strings · 16.3.1 Raw strings to the rescue · 16.4 Extracting matched text from strings · 16.5 Substituting text with regular expressions · 16.5.1 Using a function with sub · 16.6 Phone number normalizer · 16.6.1 Solving the problem with AI-generated code · 16.6.2 Solutions and discussion · 16.7 Summary

What you learn in chapter 16 →

Chapter 17: Data types as objects

The introduction explains that Python’s dynamic typing makes types relevant at runtime, not just the objects they describe.

17.1 Types are objects too · 17.2 Using types · 17.3 Types and user-defined classes · 17.4 Duck typing · 17.5 What is a special method attribute? · 17.6 Making an object behave like a list · 17.7 The __getitem__ special method attribute · 17.7.1 it works · 17.7.2 Implementing full list functionality · 17.8 Giving an object full list capability · 17.9 Subclassing from built-in types · 17.9.1 Subclassing list · 17.9.2 Subclassing UserList · 17.10 When to use special method attributes · 17.11 Creating a string-only key-value dictionary · 17.11.1 Solving the problem with AI-generated code · 17.11.2 Solutions and discussion · 17.12 Summary

What you learn in chapter 17 →

Chapter 18: Packages

This introduction explains why packages help organize related modules and distinguishes that meaning of package from a distributable file.

18.1 What is a package? · 18.2 A first example: mathproj · 18.3 Implementing the mathproj package · 18.3.1 __init__.py files in packages · 18.3.2 Basic use of the mathproj package · 18.3.3 Loading subpackages and submodules · 18.3.4 import statements within packages · 18.4 The __all__ attribute · 18.5 Proper use of packages · 18.6 Creating a package · 18.6.1 Solving the problem with AI-generated code · 18.6.2 Solutions and discussion · 18.7 Summary

What you learn in chapter 18 →

Chapter 19: Using Python libraries

The introduction explains Python’s “batteries included” philosophy and introduces the chapter’s survey of libraries.

19.1 “Batteries included”: The standard library · 19.1.1 Managing various data types · 19.1.2 Manipulating files and storage · 19.1.3 Accessing operating system services · 19.1.4 Using internet protocols and formats · 19.1.5 Development and debugging tools and runtime services · 19.2 Moving beyond the standard library · 19.3 Adding more Python libraries · 19.4 The Python Package Index · 19.5 Installing Python libraries using pip and venv · 19.5.1 Installing with the --user flag · 19.5.2 Virtual environments · 19.5.3 Other options · 19.6 Summary

What you learn in chapter 19 →

Chapter 20: Basic file wrangling

The introduction frames file wrangling as the planned, automated management of growing collections of files that cannot be discarded immediately.

20.1 The problem: The never-ending flow of data files · 20.2 Scenario: The product feed from hell · 20.3 More organization · 20.4 Saving storage space: Compression and grooming · 20.4.1 Compressing files · 20.4.2 Grooming files · 20.5 Summary

What you learn in chapter 20 →

Chapter 21: Processing data files

The introduction explains why processing text files matters: data may be unstructured or delimited into records, and datasets may be large and dirty.

21.1 Welcome to ETL · 21.2 Reading text files · 21.2.1 Text encoding: ASCII, Unicode, and others · 21.2.2 Unstructured text · 21.2.3 Delimited flat files · 21.2.4 The csv module · 21.2.5 Reading a csv file as a list of dictionaries · 21.3 Excel files · 21.4 Data cleaning · 21.4.1 Cleaning · 21.4.2 Sorting · 21.4.3 Data cleaning problems and pitfalls · 21.5 Writing data files · 21.5.1 CSV and other delimited files · 21.5.2 Writing Excel files · 21.5.3 Packaging data files · 21.6 Weather observations · 21.6.1 Solving the problem with AI-generated code · 21.6.2 Solutions and discussion · 21.7 Summary

What you learn in chapter 21 →

Chapter 22: Data over the network

Introduces the chapter’s coverage of moving and obtaining data over a network, including file transfers, APIs, structured formats, and web scraping.

22.1 Fetching files · 22.1.1 Using Python to fetch files from an FTP server · 22.1.2 Fetching files with SFTP · 22.1.3 Retrieving files over HTTP/HTTPS · 22.2 Fetching data via an API · 22.3 Structured data formats · 22.3.1 JSON data · 22.3.2 XML data · 22.4 Scraping web data · 22.5 Tracking the weather · 22.5.1 Solving the problem with AI-generated code · 22.5.2 Solutions and discussion · 22.6 Summary

What you learn in chapter 22 →

Chapter 23: Saving data

The chapter introduction explains why cleaned data may need a database and identifies relational and NoSQL databases as useful storage options.

23.1 Relational databases · 23.1.1 The Python database API · 23.2 SQLite: Using the sqlite3 database · 23.3 Using MySQL, PostgreSQL, and other relational databases · 23.4 Making database handling easier with an object relational mapper · 23.4.1 SQLAlchemy · 23.4.2 Using Alembic for database schema changes · 23.5 NoSQL databases · 23.6 Key-value stores with Redis · 23.7 Documents in MongoDB · 23.8 Creating a database · 23.9 Summary

What you learn in chapter 23 →

Chapter 24: Exploring data

The introduction previews a shift from getting and cleaning data to manipulating and exploring it with Python.

24.1 Python tools for data exploration · 24.1.1 Python’s advantages for exploring data · 24.1.2 Python can be better than a spreadsheet · 24.2 Python and pandas · 24.2.1 Why you might want to use pandas · 24.2.2 Installing pandas · 24.2.3 Data frames · 24.3 Data cleaning · 24.3.1 Loading and saving data with pandas · 24.3.2 Data cleaning with a data frame · 24.4 Data aggregation and manipulation · 24.4.1 Merging data frames · 24.4.2 Selecting data · 24.4.3 Grouping and aggregation · 24.5 Plotting data · 24.6 Why you might not want to use pandas · 24.7 Summary

What you learn in chapter 24 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.