You are previewing Head First Python, 2nd Edition.
O'Reilly logo
Head First Python, 2nd Edition

Book Description

Are you keen to add Python as a programming skill? Learn quickly and have some fun at the same time with the second edition of Head First Python. This updated book takes you beyond typical how-to manuals with engaging images, puzzles, stories, and quizzes that are proven to stimulate learning and retention. You’ll not only learn how Python differs from other programming languages, you’ll also learn how to be a great Python programmer.

Table of Contents

  1. Cover Page
  2. 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 Intepreter For Help On A Function
    36. Experimenting With Ranges
    37. Don’t Forget To Try The Beer Song Code
    38. Chapter 1’s Programs
  3. 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. Lists: What We Know
    21. What Looks Like A Copy, Isn’t
    22. How To Copy A Data Structure
    23. Square Brackets Are Everywhere
    24. Lists Extend The Square Bracket Notation
    25. Lists Understand Start, Stop, And Step
    26. List Slices In Action
    27. Starting, And Stopping With Lists
    28. Stepping With Lists
    29. Putting Slices To Work On Lists
    30. Which is better? It depends...
    31. Slicing A List Is Non-destructive
    32. Python’s “for” Loop Understands Lists
    33. Python’s “for” Loop Understands Slices
    34. Marvin’s Slices In Detail
    35. Lists: Updating What We Know
    36. What’s Wrong With Lists?
    37. When Not To Use Lists
    38. Chapter 2’s Programs
  4. 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 Initialisation 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)
  5. 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
  6. 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 Ser ver?
    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 Functionalit y 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 Templated 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
  7. 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
  8. 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
    5. Check & Configure Python 3 on Mac OS X
    6. Install Python 3 on Linux
  9. Appendix B: 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!