Books & Videos

Table of Contents

  1. Chapter 1 The Basics: Getting Started Quickly

    1. Breaking with Tradition

    2. Jump Right In

    3. Understanding IDLE’s Windows

    4. What Happens Next...

    5. Press F5 to Run Your Code

    6. Code Runs Immediately

    7. Executing Code, One Statement at a Time

    8. Functions + Modules = The Standard Library

    9. Batteries Included

    10. Data Structures Come Built-in

    11. Invoking Methods Obtains Results

    12. Deciding When to Run Blocks of Code

    13. What Happened to My Curly Braces?

    14. What “else” Can You Have with “if”?

    15. Suites Can Contain Embedded Suites

    16. What We Already Know

    17. Extending Our Program to Do More

    18. What’s the Best Approach to Solving This Problem?

    19. Returning to the Python Shell

    20. Experimenting at the Shell

    21. Iterating Over a Sequence of Objects

    22. Iterating a Specific Number of Times

    23. Applying the Outcome of Task #1 to Our Code

    24. Indent Suites with Format...Indent Region

    25. Arranging to Pause Execution

    26. Importation Confusion

    27. Generating Random Integers with Python

    28. Asking the Interpreter for Help

    29. Reviewing Our Experiments

    30. Updating What We Already Know

    31. A Few Lines of Code Do a Lot

    32. Coding a Serious Business Application

    33. Python Code Is Easy to Read

    34. Is Indentation Driving You Crazy?

    35. Asking the Interpreter for Help on a Function

    36. Experimenting with Ranges

    37. Don’t Forget to Try the Beer Song Code

    38. Chapter 1’s Programs

  2. Chapter 2 List Data: Working With Ordered Data

    1. Numbers, Strings... and Objects

    2. “Everything is an Object”

    3. Meet the Four Built-in Data Structures

    4. Ordered Collections are Mutable/Immutable

    5. An Unordered Data Structure: Dictionary

    6. A Data Structure Which Avoids Duplicates: Set

    7. A List is an Ordered Collection of Objects

    8. Creating Lists Literally

    9. Putting Lists to Work

    10. Use Your Editor When Working on More Than a Few Lines of Code

    11. “Growing” A List At Run-Time

    12. Checking For Membership with “in”

    13. It’s Time to Update Our Code

    14. Removing Objects From a List

    15. Popping Objects Off a List

    16. Extending a List with Objects

    17. Inserting an Object Into a List

    18. What About Using Square Brackets?

    19. What Happened to “plist”?

    20. What Happened to “plist”, Continued?

    21. Lists: What We Know

    22. What Looks Like A Copy, Isn’t

    23. How To Copy A Data Structure

    24. Square Brackets Are Everywhere

    25. Lists Extend The Square Bracket Notation

    26. Lists Understand Start, Stop, And Step

    27. List Slices In Action

    28. Starting, And Stopping With Lists

    29. Stepping With Lists

    30. Putting Slices To Work On Lists

    31. Putting Slices To Work On Lists, Continued

    32. Which is better? It depends...

    33. Slicing A List Is Non-destructive

    34. Python’s “for” Loop Understands Lists

    35. Python’s “for” Loop Understands Slices

    36. Marvin’s Slices In Detail

    37. Lists: Updating What We Know

    38. What’s Wrong With Lists?

    39. When Not To Use Lists

    40. Chapter 2’s Programs

    41. Chapter 2’s Programs, Continued

  3. Chapter 3 Structured Data: Working With Structured Data

    1. A Dictionary Stores Key/Value Pairs

    2. Make Dictionaries Easy to Read

    3. How to Spot a Dictionary in Code

    4. Insertion Order is NOT Maintained

    5. Value Lookup With Square Brackets

    6. Working With Dictionaries At Run-Time

    7. Recap: Displaying Found Vowels (Lists)

    8. How Can A Dictionary Help Here?

    9. Selecting A Frequency Count Data Structure

    10. Updating A Frequency Counter

    11. Updating A Frequency Counter, v2.0

    12. Iterating Over A Dictionary

    13. Iterating Over Keys And Values

    14. Dictionaries: What We Already Know

    15. Iterating Over A Dictionary With “items”

    16. Just How Dynamic Are Dictionaries?

    17. Avoiding KeyErrors At Run-Time

    18. Checking For Membership With “in”

    19. Ensuring Initialization Before Use

    20. Substituting “not in” For “in”

    21. Putting The “setdefault” Method To Work

    22. Aren’t Dictionaries (And Lists) Enough?

    23. Sets Don’t Allow Duplicates

    24. Creating Sets Efficiently

    25. Taking Advantage Of Set Methods

    26. Union Works By Combining Sets

    27. Difference Tells You What’s Not Shared

    28. Intersection Reports On Commonality

    29. Sets: What You Already Know

    30. Making The Case For Tuples

    31. Tuples Are Immutable

    32. Watch Out For Single-Object Tuples

    33. Combining The Built-in Data Structures

    34. Storing A Table Of Data

    35. A Dictionary Containing a Dictionary

    36. A Dictionary of Dictionaries (a.k.a. A Table)

    37. Pretty Printing Complex Data Structures

    38. Visualizing Complex Data Structures

    39. Accessing A Complex Data Structure’s Data

    40. Data Is As Complex As You Make It

    41. Chapter 3’s Programs (1 of 2)

    42. Chapter 3’s Programs (2 of 2)

  4. Chapter 4 Code Reuse: Functions & Modules

    1. Reusing Code With Functions

    2. Introducing Functions

    3. What About Type Information?

    4. Naming A Chunk Of Code With “def”

    5. Invoking Your Function

    6. Use IDLE’s Editor To Make Changes

    7. What’s The Deal With All Those Strings?

    8. Follow Best Practice As Per The PEPs

    9. Functions Can Accept Arguments

    10. Functions Return A Result

    11. Returning One Value

    12. Returning More Than One Value

    13. Recalling The Built-in Data Structures

    14. Use Annotations To Improve Your Docs

    15. Why Use Function Annotations?

    16. Functions: What We Know Already

    17. Making A Generically Useful Function

    18. Creating Another Function, 1 of 3

    19. Creating Another Function, 2 of 3

    20. Creating Another Function, 3 of 3

    21. Specifying Default Values For Arguments

    22. Positional vs. Keyword Assignment

    23. Updating What We Know About Functions

    24. Functions Beget Modules

    25. How Are Modules Found?

    26. Running Python From The Command-Line

    27. Not Found Modules Produce ImportErrors

    28. ImportErrors Occur No Matter The Platform

    29. Getting A Module Into Site-packages

    30. Creating The Required Setup Files

    31. Creating The Distribution File

    32. Distribution Files On UNIX-like OSes

    33. Installing Packages With “pip”

    34. Modules: What We Know Already

    35. Demonstrating Call-By-Value Semantics

    36. Demonstrating Call-By-Reference Semantics

    37. Can I Test For PEP 8 Compliance?

    38. Getting Ready To Check PEP 8 Compliance

    39. Install The Testing Developer Tools

    40. How PEP 8 Compliant Is Our Code?

    41. Understanding The Failure Messages

    42. Confirming PEP 8 Compliance

    43. Chapter 4’s Programs

  5. Chapter 5 Building a Webapp: Getting Real

    1. Python: What You Already Know

    2. Let’s Build Something

    3. What Do We Want Our Webapp To Do?

    4. What Happens On The Web Server?

    5. Let’s Install Flask

    6. How Does Flask Work?

    7. Running Your Flask Webapp For The First Time

    8. Here’s What Happened (Line-By-Line)

    9. Creating A Flask Webapp Object

    10. Decorating A Function With A URL

    11. Running Your Webapp’s Behavior(s)

    12. Exposing Functionality To The Web

    13. Recall What We’re Trying To Build

    14. Building The HTML Form

    15. Templates Relate To Web Pages

    16. Rendering Templates From Flask

    17. Displaying The Webapp’s HTML Form

    18. Preparing To Run The Template Code

    19. We’re Ready For A Test-Run

    20. Understanding HTTP Status Codes

    21. Handling Posted Data

    22. Refining The Edit/Stop/Start/Test Cycle

    23. Accessing HTML Form Data With Flask

    24. Using Request Data In Your Webapp

    25. Producing The Results As HTML

    26. Calculating The Data We Need

    27. Adding A Finishing Touch

    28. Redirect To Avoid Unwanted Errors

    29. Functions Can Have Multiple URLs

    30. Updating What We Know

    31. Preparing Your Webapp For The Cloud

    32. Exploiting Dunder Name Dunder Main

    33. Chapter 5’s Programs

  6. Chapter 6 Storing & Manipulating Data: Where To Put Your Data

    1. Doing Something With Your Webapp’s Data

    2. Python Supports Open, Process, Close

    3. Reading Data From An Existing File

    4. A Better Open, Process, Close: “with”

    5. A Quick Review

    6. View The Log Through Your Webapp

    7. Examine The Raw Data With View Source

    8. It’s Time To Escape (Your Data)

    9. Viewing The Entire Log In Your Webapp

    10. Learning More About The Request-object

    11. Logging Specific Web Request Attributes

    12. Log A Single Line Of Delimited Data

    13. One Final Change To Our Logging Code

    14. From Raw Data To Readable Output

    15. Does This Remind You Of Anything?

    16. Use A Dict Of Dicts... Or Something Else?

    17. What’s Joined Together Can Be Split Apart

    18. When Should The Conversion Occur?

    19. Processing Data: What We Already Know

    20. Generate Readable Output With HTML

    21. Embed Display Logic In Your Template

    22. Producing Readable Output With Jinja2

    23. The Current State Of Our Webapp Code

    24. Asking Questions Of Your Data

    25. Chapter 6’s Code

  7. Chapter 7 Using a Database: Putting Python’s DB-API To Use

    1. Database-Enabling Your Webapp

    2. Task 1: Install The MySQL Server

    3. Introducing Python’s DB-API

    4. Task 2: Install A MySQL Database Driver For Python

    5. Install MySQL-Connector/Python

    6. Task 3: Create Our Webapp’s Database & Tables

    7. Decide On A Structure For Your Log Data

    8. Confirm Your Table Is Ready For Data

    9. Task 4: Create Code To Work With Our Webapp’s Database And Tables

    10. Storing Data Is Only Half The Battle

    11. How Best To Reuse Your Database Code?

    12. Consider What You’re Trying To Reuse

    13. What About That Import?

    14. Consider What You’re Trying To Do

    15. You’ve Seen This Pattern Before

    16. The Bad News Isn’t Really All That Bad

    17. Chapter 7’s Code

  8. Chapter 8 A Little Bit of Class: Abstracting Behavior and State

    1. Hooking Into The “with” Statement

    2. An Object-Oriented Primer

    3. Creating Objects From Classes

    4. Objects Share Behavior But Not State

    5. Doing More With CountFromBy

    6. It’s Worth Repeating Ourselves: Objects Share Behavior But Not State

    7. Invoking A Method: Understand The Details

    8. Method Invocation: What Actually Happens

    9. Adding A Method To A Class

    10. Are You Serious About “self”?

    11. The Importance Of “self”

    12. Coping With Scoping

    13. Prefix Your Attribute Names With “self”

    14. Initialise (Attribute) Values Before Use

    15. Dunder Init Initializes Attributes

    16. Initializing Attributes With Dunder Init

    17. Understanding CountFromBy’s Representation

    18. Defining CountFromBy’s Representation

    19. Providing Sensible Defaults For CountFromBy

    20. Classes: What We Know

    21. Chapter 8’s Code

  9. Chapter 9 The Context Management Protocol: Hooking Into Python’s With Statement

    1. What’s The Best Way To Share Our Webapp’s Database Code?

    2. Consider What You’re Trying To Do, Revisited

    3. Managing Context With Methods

    4. You’ve Already Seen A Context Manager In Action

    5. Create A New Context Manager Class

    6. Initialise The Class With The Database Config

    7. Perform Set-up With Dunder Enter

    8. Perform Tear-down With Dunder Exit

    9. Reconsidering Your Webapp Code, 1 of 2

    10. Reconsidering Your Webapp Code, 2 of 2

    11. Recalling The “log_request” Function

    12. Amending The “log_request” Function

    13. Recalling The “view_the_log” Function

    14. It’s Not Just The Code That Changes

    15. Amending The “view_the_log” Function

    16. All That Remains...

    17. Answering The Data Questions

    18. Chapter 9’s Code, 1 of 2

    19. Chapter 9’s Code, 2 of 2

  10. Chapter 10 Function Decorators: Wrapping Functions

    1. Your Webapp is Working well, but...

    2. The Web is Stateless

    3. Your Web Server (Not Your Computer) Runs Your Code

    4. It’s Time for a Bit of a Session

    5. Flask’s Session Technology Adds State

    6. Dictionary Lookup Retrieves State

    7. Managing Logins with Sessions

    8. Let’s do Login

    9. Let’s do Logout & Status Checking

    10. Why not Check for False?

    11. Can we now Restrict Access to URLs?

    12. Copy-and-Paste is Rarely a good Idea

    13. Creating a function helps, but...

    14. You’ve been Using Decorators all Along

    15. Pass a Function to a Function

    16. Invoking a Passed Function

    17. Functions can be Nested inside Functions

    18. Return a Function from a Function

    19. Accepting a list of Arguments

    20. Processing a list of Arguments

    21. Accepting a dictionary of Arguments

    22. Processing a dictionary of Arguments

    23. Accepting any Number and type of Function Arguments

    24. A Recipe for Creating a Function Decorator

    25. Recap: We need to Restrict Access to Certain URLs

    26. Creating a Function Decorator

    27. The Final Step: Handling Arguments

    28. Your decorator in all its Glory

    29. Putting your decorator to Work

    30. The Beauty of Decorators

    31. Creating more Decorators

    32. Back to Restricting Access to /viewlog

    33. What’s Next?

    34. Chapter 10’s Code, 1 of 2

    35. Chapter 10’s Code, 2 of 2

  11. Chapter 11 Exception Handling: What to Do When Things Go Wrong

    1. Databases Aren’t Always Available

    2. Web Attacks Are A Real Pain

    3. Input-Output Is (Sometimes) Slow

    4. Your Function Calls Can Fail

    5. Considering The Identified Problems

    6. Always Try To Execute Error-prone Code

    7. Catching An Error Is Not Enough

    8. Try Once, But Except Many Times

    9. A Lot Of Things Can Go Wrong

    10. The Catch-All Exception-Handler

    11. Haven’t We Just Lost Something?

    12. Learning About Exceptions From “sys”

    13. The Catch-All Exception-Handler, Revisited

    14. Getting Back To Our Webapp Code

    15. Silently Handling Exceptions

    16. Handling Other Database Errors

    17. Does “More Errors” Mean “More Excepts”?

    18. Avoid Tightly Coupled Code

    19. The DBcm Module, Revisited

    20. Creating Custom Exceptions

    21. What Else Can Go Wrong With “DBcm”?

    22. Creating More Custom Exceptions

    23. Are Your Database Credentials Correct?

    24. Handling SQLError Is Different

    25. Be Careful With Code Positioning

    26. Raising SQLError

    27. A Quick Recap: Adding Robustness

    28. How To Deal With Wait? It Depends...

    29. Chapter 11’s Code, 1 of 3

    30. Chapter 11’s Code, 2 of 3

    31. Chapter 11’s Code, 3 of 3

  12. Chapter 12 11¾ A Little Bit of Threading: Dealing With Waiting

    1. Waiting: What To Do?

    2. How Are You Querying Your Database?

    3. Database Inserts And Selects Are Different

    4. Doing More Than One Thing At Once

    5. Don’t Get Bummed Out: Use Threads

    6. First Things First: Don’t Panic

    7. Don’t Get Bummed Out: Flask Can Help

    8. Is Your Webapp Robust Now?

    9. Chapter 11¾’s Code, 1 of 2

    10. Chapter 11¾’s Code, 2 of 2

  13. Chapter 13 Advanced Iteration: Looping Like Crazy

    1. Bahamas Buzzers Have Places To Go

    2. Reading CSV Data As Lists

    3. Reading CSV Data As Dictionaries

    4. Let’s Back Up A Little Bit

    5. Stripping, Then Splitting, Your Raw Data

    6. Be Careful When Chaining Method Calls

    7. Transforming Data Into The Format You Need

    8. Transforming Into A Dictionary Of Lists

    9. Let’s Do The Basic Conversions

    10. Did You Spot The Pattern In Your Code?

    11. Spotting The Pattern With Lists

    12. Converting Patterns Into Comprehensions

    13. Take A Closer Look At The Comprehension

    14. What’s The Big Deal?

    15. Specifying A Dictionary Comprehension

    16. Extend Comprehensions With Filters

    17. Recall What You Set Out To Do

    18. Deal With Complexity The Python Way

    19. Extract A Single Destination’s Flight Times

    20. Extract Flight Times For All Destinations

    21. That Feeling You Get...

    22. The Set Comprehension In Action

    23. How To Spot A Comprehension

    24. What About “Tuple Comprehensions”?

    25. Parentheses Around Code == Generator

    26. Using A Listcomp To Process URLs

    27. Using A Generator To Process URLs

    28. Using A Generator: What Just Happened?

    29. Define What Your Function Needs To Do

    30. Yield To The Power Of Generator Functions

    31. Tracing Your Generator Function, 1 of 2

    32. Tracing Your Generator Function, 2 of 2

    33. Concluding Remarks

    34. One Final Question

    35. Chapter 12’s Code

    36. It’s Time To Go...

  14. Appendix Installation: Installing Python

    1. Install Python 3 on Windows

    2. Check Python 3 on Windows

    3. Add to Python 3 on Windows

    4. Install Python 3 on Mac OS X (macOS)

    5. Check & Configure Python 3 on Mac OS X

    6. Install Python 3 on Linux

  15. Appendix Pythonanywhere: Deploying Your Webapp

    1. Step 0: A Little Prep

    2. Step 1: Sign-up For PythonAnywhere

    3. Step 2: Upload Your Files To The Cloud

    4. Step 3: Extract And Install Your Code

    5. Step 4: Create A Starter Webapp, 1 of 2

    6. Step 4: Create A Starter Webapp, 2 of 2

    7. Step 5: Configure Your Webapp

    8. Step 5: Take Your Cloud-based Webapp For A Spin!

  16. Appendix Top Ten Things We Didn’t Cover: There’s Always More To Learn

    1. 1. What About Python 2?

    2. 2. Virtual Programming Environments

    3. 3. More On Object Orientation

    4. 4. Formats For Strings And The Like

    5. 5. Getting Things Sorted

    6. 6. More From The Standard Library

    7. 7. Running Your Code Concurrently

    8. 8. GUIs With Tkinter (And Fun With Turtles)

    9. 9. It’s Not Over ‘Til It’s Tested

    10. 10. Debug, Debug, Debug

  17. Appendix Top Ten Projects Not Covered: Even More Tools, Libraries, And Modules

    1. 1. Alternatives To >>>

    2. 2. Alternatives To IDLE

    3. 3. Jupyter Notebook: The Web-Based IDE

    4. 4. Doing Data Science

    5. 5. Web Development Technologies

    6. 6. Working With Web Data

    7. 7. More Data Sources

    8. 8. Programming Tools

    9. 9. Kivy: Our Pick For “Coolest Project Ever”

    10. 10. Alternative Implementations

  18. Appendix Getting Involved: The Python Community

    1. BDFL: Benevolent Dictator For Life

    2. A Tolerant Community: Respect For Diversity

    3. The Python Podcasts

    4. The Zen Of Python

    5. Which Book Should I Read Next?

    6. Our Favorite Python Books

  19. Appendix Back Cover Content: TOC images

    1. What will you learn from this book?

    2. Why does this book look so different?

    3. Chapter 1 TOC Image

    4. Chapter 2 TOC Image

    5. Chapter 3 TOC Image

    6. Chapter 4 TOC Image

    7. Chapter 5 TOC Image

    8. Chapter 6 TOC Image

    9. Chapter 7 TOC Image

    10. Chapter 8 TOC Image

    11. Chapter 9 TOC Image

    12. Chapter 10 TOC Image

    13. Chapter 11 TOC Image

    14. Chapter 11¾ TOC Image

    15. Chapter 12 TOC Image