The Quick Python Book
+ Python Programming
+ Data Science
Science & Technology
Parts
Feb, 2025
Feb, 2025
Feb, 2025
Feb, 2025
No matching items
Table of Contents
- Foreword xviii
- Preface xx
- Acknowledgments xxii
- About this book xxiv
- About the author xxix
- About the cover illustration xxx
Part 1. Starting out
1. About Python 3
- 1.1 Why should I use Python?
- 1.2 What Python does well
- Python is easy to use
- Python is expressive
- Python is readable
- Python is complete: “Batteries included”
- Python has a rich ecosystem of third-party libraries
- Python is cross-platform
- Python is free
- Python is easy to use
- 1.3 What Python is improving
- Python is getting faster
- Python doesn’t enforce variable types at compile time
- Python is improving mobile support
- Python is improving support for multiple processors
- Python is getting faster
2. Getting started
- 2.1 Paradox of choice: Which Python?
- Python versions
- Sources of Python
- Devices, platforms, and operating systems
- Environments and tools
- Python versions
- 2.2 Colaboratory: Jupyter notebooks in the cloud
- Getting the source notebooks
- Getting started with Colaboratory
- Colaboratory’s Python version
- Getting the source notebooks
- 2.3 Writing and running code in Colaboratory
- Hello, World
- Dealing with errors in Jupyter
- Hello, World
- 2.4 Using help and dir to explore Python
- 2.5 Using AI tools to write Python code
- Benefits of AI tools
- Negatives of using AI tools
- AI options
- Benefits of AI tools
3. The quick Python overview
- 3.1 Python synopsis
- 3.2 Built-in data types
- Numbers
- Lists
- Tuples
- Strings
- Dictionaries
- Sets, frozensets
- File objects
- Numbers
- 3.3 Type hints in Python
- 3.4 Control flow structures
- Boolean values and expressions
- The if-elif-else statement
- Structural pattern matching with
match
- The while loop
- The for loop
- Function definition
- Exceptions
- Context handling using the
withkeyword
- Boolean values and expressions
- 3.5 Module creation
- 3.6 Object-oriented programming
Part 2. The essentials
4. The absolute basics
- 4.1 Indentation and block structuring
- 4.2 Differentiating comments
- 4.3 Variables and assignments
- 4.4 Optional type hints in Python
- Why use type hints?
- Why not use type hints?
- Progressive typing
- Why use type hints?
- 4.5 Expressions
- 4.6 Strings
- 4.7 Numbers
- Built-in numeric functions
- Advanced numeric functions
- Numeric computation
- Complex numbers
- Advanced complex-number functions
- Built-in numeric functions
- 4.8 The None value
- 4.9 Getting input from the user
- 4.10 Built-in operators
- 4.11 Basic Python style
5. Lists, tuples, and sets
- 5.1 Lists are like arrays
- 5.2 List indices
- 5.3 Modifying lists
- 5.4 Sorting lists
- Custom sorting
- The
sorted()function
- Custom sorting
- 5.5 Other common list operations
- List membership with
in
- Concatenation with
+
- Initialization with
*
- Minimum/maximum with
min,max
- Search with
index
- Matches with
count
- Summary of list operations
- List membership with
- 5.6 Nested lists and deep copies
- 5.7 Tuples
- Tuple basics
- One-element tuples need a comma
- Packing and unpacking tuples
- Converting between lists and tuples
- Tuple basics
- 5.8 Sets
- Set operations
- Frozen sets
- Set operations
- 5.9 Lab: Examining a list
- Why solve it the old-fashioned way?
- Solving with AI code generation
- Solutions and discussion
- Why solve it the old-fashioned way?
6. Strings
- 6.1 Strings as sequences of characters
- 6.2 Basic string operations
- 6.3 Special characters and escape sequences
- Basic escape sequences
- Numeric and Unicode escape sequences
- Printing vs. evaluating strings with special chars
- Basic escape sequences
- 6.4 String methods
splitandjoin
- Converting strings to numbers
- Getting rid of whitespace
- String searching
- Modifying strings
- Modifying with list manipulations
- Useful methods and constants
- 6.5 Converting objects to strings
- 6.6 Using the
formatmethod- Positional parameters
- Named parameters
- Format specifiers
- Positional parameters
- 6.7 String interpolation with f-strings
- 6.8 Formatting strings with
%- Formatting sequences
- Named parameters and formatting sequences
- Formatting sequences
- 6.9 Bytes
- 6.10 Preprocessing text
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
7. Dictionaries
- 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
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
8. Control flow
- 8.1 if-elif-else statement
- 8.2 Structural pattern matching with
match
- 8.3 while loop
- 8.4 for loop
- The range function
- The range function
- 8.5 Controlling range with start and step
- 8.6 for loop and tuple unpacking
- 8.7
enumeratefunction
- 8.8
zipfunction
- 8.9 Comprehensions (list, set, dict)
- Generator expressions
- Generator expressions
- 8.10 Statements, blocks, indentation
- 8.11 Boolean values and expressions
- Python objects as Booleans
- Comparison and Boolean operators
- Python objects as Booleans
- 8.12 Writing a simple text analyzer
- 8.13 Refactoring
word_count- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
9. Functions
- 9.1 Basic function definitions
- 9.2 Function parameter options
- Positional parameters
- Arguments by name
- Variable arguments
- Mixing techniques
- Positional parameters
- 9.3 Mutable objects as arguments
- Mutable defaults
- Mutable defaults
- 9.4 Local, nonlocal, global variables
- 9.5 Assigning functions to variables
- 9.6 Lambda expressions
- 9.7 Generator functions
- 9.8 Decorators
- 9.9 Useful functions
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
10. Modules and scoping rules
- 10.1 What is a module?
- 10.2 A first module
- 10.3 The import statement
- 10.4 The module search path
- Where to place your own modules
- 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
- Built-in namespace
- Built-in namespace
- 10.8 Creating a module
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
11. Python programs
- 11.1 Creating a basic program
- Starting from CLI
- Command-line arguments
- Executing code only as main
- Redirecting input/output
argparsemodule
fileinputmodule
- Starting from CLI
- 11.2 Running scripts on different OS
- UNIX
- macOS
- Windows
- UNIX
- 11.3 Programs and modules
- 11.4 Distributing Python applications
- Wheels
- zipapp and pex
- py2exe and py2app
- Creating executables with freeze
- Wheels
- 11.5 Creating a program
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
12. Using the filesystem
- 12.1
osandos.pathvs.pathlib
- 12.2 Paths and pathnames
- Absolute and relative paths
- Current working directory
- Accessing directories with pathlib
- Manipulating pathnames
- Manipulating with pathlib
- Useful constants and functions
- Absolute and relative paths
- 12.3 Getting info about files
- With
scandir
- With
- 12.4 More filesystem operations
- With pathlib
- With pathlib
- 12.5 Processing files in a directory subtree
- 12.6 More file operations
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
13. Reading and writing files
- 13.1 Opening files and file objects
- 13.2 Closing files
- 13.3 Opening files in write/other modes
- 13.4 Functions to read/write text or binary data
- Using binary mode
- Using binary mode
- 13.5 Reading and writing with pathlib
- 13.6 Terminal I/O and redirection
- 13.7 Handling structured binary data with
struct
- 13.8 Pickling objects
- Reasons not to pickle
- Reasons not to pickle
- 13.9 Shelving objects
- 13.10 Final fixes to
wc- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
14. Exceptions
- 14.1 Introduction to exceptions
- Philosophy of errors and handling
- Formal definition
- Handling different types
- Philosophy of errors and handling
- 14.2 Exceptions in Python
- Types of exceptions
- Raising exceptions
- Catching and handling
- Defining new exceptions
- Exception groups
- Debugging with
assert
- Exception hierarchy
- Example: disk-writing program
- Example: exceptions in evaluation
- Where to use exceptions
- Types of exceptions
- 14.3 Context managers with
with
- 14.4 Adding exceptions
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
Part 3. Advanced language features
15. Classes and object-oriented programming
- 15.1 Defining classes
- Using a class instance as a structure or record
- Using a class instance as a structure or record
- 15.2 Instance variables
- 15.3 Methods
- 15.4 Class variables
- An oddity with class variables
- An oddity with class variables
- 15.5 Static methods and class methods
- Static methods
- Class methods
- Static 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
@propertyfor 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
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
16. Regular expressions
- 16.1 What is a regular expression?
- 16.2 Regular expressions with special characters
- 16.3 Regular expressions and raw strings
- Raw strings to the rescue
- Raw strings to the rescue
- 16.4 Extracting matched text from strings
- 16.5 Substituting text with regular expressions
- Using a function with sub
- Using a function with sub
- 16.6 Phone number normalizer
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
17. Data types as objects
- 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- How it works
- Implementing full list functionality
- How it works
- 17.8 Giving an object full list capability
- 17.9 Subclassing from built-in types
- Subclassing list
- Subclassing UserList
- Subclassing list
- 17.10 When to use special method attributes
- 17.11 Creating a string-only key-value dictionary
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
18. Packages
- 18.1 What is a package?
- 18.2 A first example: mathproj
- 18.3 Implementing the mathproj package
__init__.pyfiles in packages
- Basic use of the mathproj package
- Loading subpackages and submodules
- Import statements within packages
- 18.4 The
__all__attribute
- 18.5 Proper use of packages
- 18.6 Creating a package
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
19. Using Python libraries
- 19.1 “Batteries included”: The standard library
- Managing various data types
- Manipulating files and storage
- Accessing operating system services
- Using internet protocols and formats
- Development/debugging tools and runtime services
- Managing various data types
- 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
- Installing with
--userflag
- Virtual environments
- Other options
- Installing with
Part 4. Working with data
20. Basic file wrangling
- 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
- Compressing files
- Grooming files
- Compressing files
21. Processing data files
- 21.1 Welcome to ETL
- 21.2 Reading text files
- Text encoding: ASCII, Unicode, others
- Unstructured text
- Delimited flat files
- The csv module
- Reading CSV as list of dicts
- Text encoding: ASCII, Unicode, others
- 21.3 Excel files
- 21.4 Data cleaning
- Cleaning
- Sorting
- Problems and pitfalls
- Cleaning
- 21.5 Writing data files
- CSV and delimited files
- Writing Excel files
- Packaging data files
- CSV and delimited files
- 21.6 Weather observations
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
22. Data over the network
- 22.1 Fetching files
- FTP
- SFTP
- HTTP/HTTPS
- FTP
- 22.2 Fetching data via an API
- 22.3 Structured data formats
- JSON
- XML
- JSON
- 22.4 Scraping web data
- 22.5 Tracking the weather
- Solving with AI-generated code
- Solutions and discussion
- Solving with AI-generated code
23. Saving data
- 23.1 Relational databases
- The Python database API
- The Python database API
- 23.2 SQLite: Using sqlite3
- 23.3 Using MySQL, PostgreSQL, other relational DBs
- 23.4 Object-relational mapper (ORM)
- SQLAlchemy
- Using Alembic for schema changes
- SQLAlchemy
- 23.5 NoSQL databases
- 23.6 Key-value stores with Redis
- 23.7 Documents in MongoDB
- 23.8 Creating a database
24. Exploring data
- 24.1 Python tools for data exploration
- Advantages over spreadsheets
- Advantages over spreadsheets
- 24.2 Python and pandas
- Why use pandas
- Installing pandas
- Data frames
- Why use pandas
- 24.3 Data cleaning
- Loading/saving with pandas
- Cleaning with data frame
- Loading/saving with pandas
- 24.4 Data aggregation and manipulation
- Merging data frames
- Selecting data
- Grouping and aggregation
- Merging data frames
- 24.5 Plotting data
- 24.6 Why you might not want to use pandas
- Case study
Appendix
- A guide to Python’s documentation