Head First Python, 3rd Edition

Book description

What will you learn from this book?

Want to learn the Python language without slogging your way through how-to manuals? With Head First Python, you'll quickly grasp Python's fundamentals by working with built-in data structures and functions. You'll build your very own web app, which—once it's ready for prime time—runs in the cloud. You'll learn how to wrangle data with Python, scrape data from the web, feed data to pandas, and interact with databases. This third edition is a complete learning experience that will help you become a bona fide Python programmer in no time.

What's so special about this book?

If you've read a Head First book, you know what to expect: a visually rich format designed for the way your brain works. If you haven't, you're in for a treat. With this book, you'll learn Python through a multisensory experience that engages your mind—rather than a text-heavy approach that puts you to sleep.

Publisher resources

View/Submit Errata

Table of contents

  1. Other books in O’Reilly’s Head First series
  2. Author of Head First Python
  3. Table of Contents (the real thing)
  4. How to use this Book: Intro
    1. Who is this book for?
      1. Who should probably back away from this book?
    2. We know what you’re thinking
    3. We know what your brain is thinking
    4. Metacognition: thinking about thinking
    5. Here’s what WE did:
    6. Read Me
    7. Let’s install the latest Python
      1. Installing on Windows
      2. Installing on macOS
      3. Installing on Linux
    8. Python on its own is not enough
      1. Install the latest Jupyter Notebook backend
      2. Install the latest release of VS Code
    9. Configure VS Code to your taste
    10. Add two required extensions to VS Code
    11. VS Code’s Python support is state-of-the-art
    12. The Technical Review Team
    13. Acknowledgments
  5. 1. Why Python?: Similar but Different
    1. Getting ready to run some code
    2. Preparing for your first Jupyter experience
    3. Let’s pop some code into your notebook editor
    4. Press Shift+Enter to run your code
      1. So… Python code really is easy to read… and run
    5. What if you want more than one card?
    6. Take a closer look at the card drawing code
    7. The Big 4: list, tuple, dictionary, and set
    8. Model your deck of cards with a set
    9. The print dir combo mambo
    10. Getting help with dir’s output
    11. Populate the set with cards
    12. This feels like a deck of cards now
    13. What exactly is “card”?
    14. Need to find something?
    15. Let’s pause and take stock
    16. Python ships with a rich standard library
    17. With Python you’ll only write the code you need
      1. Python’s package ecosystem is to die for
    18. Just when you thought you were done…
  6. 2. Diving in: Let’s Make a Splash
    1. How is the Coach working right now?
    2. The Coach needs a more capable stopwatch
    3. Cubicle Conversation
    4. The file and the spreadsheet are “related”
    5. Our first task: Extract the filename’s data
      1. Everything is an object in Python
    6. A string is an object with attributes
      1. Take a moment to appreciate what you’re looking at here
    7. Extract the swimmer’s data from the filename
    8. Don’t try to guess what a method does…
    9. Splitting (aka, breaking apart) a string
    10. There’s still some work to do
    11. Read error messages from the bottom up
    12. Be careful when combining method calls
    13. Cubicle Conversation
    14. Let’s try another string method
    15. All that remains is to create some variables
      1. Multiple assignment (aka unpacking)
    16. Task #1 is done!
    17. Task #2: Process the data in the file
  7. 3. Lists of Numbers: Processing List Data
    1. Task #2: Process the data in the file
    2. Grab a copy of the Coach’s data
    3. The open BIF works with files
    4. Cubicle Conversation
    5. Using with to open (and close) a file
    6. Variables are created dynamically, as needed
    7. The file’s data is what you really want
    8. We have the swimmer’s data from the file
    9. Let’s take stock of our progress so far
      1. Your new best friend, Python’s colon
    10. What needs to happen next feels familiar
    11. The previous chapter is paying dividends
    12. Converting a time string into a time value
    13. Convert the times to hundredths of seconds
    14. To hundredths of seconds with Python
    15. A quick review of Python’s for loop
    16. The gloves are off… for loops vs. while loops
    17. You’re cruising now and making great progress!
    18. Let’s keep a copy of the conversions
    19. Creating a new, empty list
    20. Displaying a list of your list’s methods
    21. It’s time to calculate the average
    22. Convert the average to a swim time string
    23. It’s time to bring everything together
    24. Task #2 (finally) gets over the line!
  8. 4. List of Files: Functions, Modules & Files
    1. Cubicle Conversation
    2. You already have most of the code you need
    3. How to create a function in Python
    4. Save your code as often as you wish
      1. Add the code you want to share to the function
    5. Simply copying code is not enough
    6. Be sure to copy all the code you need
      1. Update and save your code before continuing…
    7. Use modules to share code
    8. Bask in the glory of your returned data
    9. Functions return a tuple when required
    10. Let’s get a list of the Coach’s filenames
    11. It’s time for a bit of detective work…
    12. What can you do to lists?
    13. Is the issue with your data or your code?
    14. Cubicle Conversation
    15. Decisions, decisions, decisions
    16. Let’s look for the colon “in” the string
    17. Did you end up with 60 processed files?
    18. The Coach’s code is taking shape…
  9. 5. Formatted String Literals: Make Charts from Data
    1. Cubicle Conversation
    2. Create simple bar charts with HTML and SVG
      1. Let’s match up your HTML and SVG to the output you see on screen:
    3. Getting from a simple chart to a Coach chart
    4. Build the strings your HTML needs in code
    5. String concatenation doesn’t scale
    6. f-strings are a very popular Python feature
    7. Generating SVG is easy with f-strings!
    8. The data is all there, or is it?
    9. Make sure you return all the data you need
    10. You have numbers now, but are they usable?
    11. Scaling numeric values so they fit
    12. All that’s left is the end of your webpage
    13. Writing to files, like reading, is painless
    14. It’s time to display your handiwork
    15. All that’s left are two aesthetic tweaks…
    16. Cubicle Conversation
    17. It’s time for another custom function
    18. Let’s add another function to your module
    19. What’s with that hundredths value?
    20. Rounding is not what you want (in this case)
    21. One more minor formatting tweak
    22. Things are progressing well…
  10. 6. Getting Organized: Data Structure Choices
    1. Get to know the data you’ll be working with
    2. Let’s extract a list of swimmers’ names
    3. The list-set-list duplicate removing trick
    4. The Coach now has a list of names
    5. A small change makes a “big” difference
    6. Every tuple is unique
    7. Perform super fast lookups with dictionaries
    8. Dictionaries are key/value lookup stores
    9. Anatomy of building a dictionary
    10. Dictionaries are optimized for speedy lookup
    11. Display the entire dictionary
    12. The pprint module prett y-prints your data
    13. Your dictionary-of-lists is easily processed
    14. This is really stating to come together
  11. 7. Building a Webapp: Web Development
    1. Let’s build the Coach’s webapp with Flask
    2. Install Flask from PyPI
    3. Prepare your folder to host your webapp
    4. The Flask MVP
    5. You have options when working with your code
      1. How does your browser and your Flask-based webapp communicate?
    6. Building your webapp, bit by bit…
      1. Spoiler Alert!
    7. What’s the deal with that NameError?
    8. Cubicle Conversation
    9. Flask includes built-in session support
    10. Flask’s session technology is a dictionary
      1. Fixing your quick fix
    11. Adjusting your code with the “better fix”
      1. Use render_template to display web pages
      2. That list of swimmers needs to be a drop-down list
    12. Building Jinja2 templates saves you time
      1. Let’s get to know a bit about Jinja2’s markup extensions to HTML
    13. Extend base.html to create more pages
    14. Dynamically creating a drop-down list
    15. Selecting a swimmer
    16. You need to somehow process the form’s data
    17. Your form’s data is available as a dictionary
    18. You’re inching closer to a working system
    19. Functions support default parameter values
    20. Default parameter values are optional
    21. The final version of your code, 1 of 2
    22. The final version of your code, 2 of 2
    23. As a first webapp goes, this is looking good
    24. The Coach’s system is ready for prime time
  12. 8. Deployment: Run Your Code Anywhere
    1. There’s always more than one way to do something
    2. There’s still something that doesn’t feel right
    3. Jinja2 executes code between {{ and }}
    4. Cubicle Conversation
    5. The ten steps to cloud deployment
    6. A beginner account is all you need
    7. There’s nothing stopping you from starting…
    8. When in doubt, stick with the defaults
    9. The placeholder webapp doesn’t do much
    10. Deploying your code to PythonAnywhere
    11. Extract your code in the console
    12. Configure the Web tab to point to your code
    13. Edit your webapp’s WSGI file
    14. Your cloud-hosted webapp is ready!
  13. 9. Working with HTML: Web Scraping
    1. The Coach needs more data
    2. Cubicle Conversation
    3. Get to know your data before scraping
    4. We need a plan of action…
    5. A step-by-step guide to web scraping
      1. Let’s take the Coach’s advice and go with a three/two split
    6. It’s time for some HTML-parsing technology
      1. It’s time for some… em… eh… cold soup!
    7. Grab the raw HTML page from Wikipedia
    8. Get to know your scraped data
    9. You can copy a slice from any sequence
    10. It’s time for some HTML parsing power
    11. Searching your soup for tags of interest
      1. The gazpacho defaults can sometimes trip you up
    12. The returned soup is also searchable
    13. Which table contains the data you need?
    14. Four big tables and four sets of world records
    15. It’s time to extract the actual data
    16. Extract data from all the tables, 1 of 2
    17. Extract data from all the tables, 2 of 2
    18. That nested loop did the trick!
  14. 10. Working with Data: Data Manipulation
    1. Bending your data to your will…
    2. You now have the data you need…
    3. Apply what you already know…
    4. Is there too much data here?
    5. Filtering on the relay data
    6. You’re now ready to update your bar charts
    7. Cubicle Conversation
    8. Python ships with a built-in JSON library
    9. JSON is textual, but far from pretty
      1. “Importing” JSON data
    10. Getting to the webapp integration
    11. All that’s needed: an edit and a copy’n’paste…
    12. Adding the world records to your bar chart
    13. Is your latest version of the webapp ready?
      1. But… are you really done?
    14. Cubicle Conversation
    15. PythonAnywhere has you covered…
    16. You need to upload your utility code, too
    17. Deploy your latest webapp to PythonAnywhere
    18. Tell PythonAnywhere to run your latest code
    19. Test your utilities before cloud deployment
    20. Let’s run your task daily at 1:00am
  15. 11. Working with: elephants dataframes: Tabular Data
    1. The elephant in the room… or is it a panda?
    2. A dictionary of dictionaries with pandas?
    3. Start by conforming to convention
    4. A list of pandas dataframes
    5. Selecting columns from a dataframe
    6. Dataframe to dictionary, attempt #1
    7. Removing unwanted data from a dataframe
    8. Negating your pandas conditonal expression
    9. Dataframe to dictionary, attempt #2
    10. Dataframe to dictionary, attempt #3
    11. It’s another dictionary of dictionaries
    12. Comparing gazpacho to pandas
    13. It was only the shortest of glimpses…
  16. 12. Databases: Getting Organized
    1. The Coach has been in touch…
    2. Cubicle Conversation
    3. It pays to plan ahead…
    4. Task #1: Decide on your database structure
    5. The napkin structure + data
    6. Installing the DBcm module from PyPI
      1. Do this to follow along…
    7. Getting started with DBcm and SQLite
    8. DBcm works alongside the “with” statement
    9. Use triple-quoted strings for your SQL
    10. Not all SQL returns results
      1. Create the events and times tables
    11. Your tables are ready (and Task #1 is done)
    12. Determining the list of swimmer’s files
    13. Task #2: Adding data to a database table
    14. Stay safe with Python’s SQL placeholders
    15. Let’s repeat this process for the events
    16. All that’s left is your times table…
    17. The times are in the swimmer’s files…
    18. A database update utility, 1 of 2
    19. A database update utility, 2 of 2
    20. Task #2 is (finally) done
  17. 13. List Comprehensions: Database Integrations
    1. Four queries to grab the data you need
    2. Let’s explore the queries in a new notebook
    3. Five lines of loop code become one
    4. Getting from five lines of code to one…
    5. A nondunder combo mambo
    6. One query down, three to go…
    7. Two queries down, two to go…
    8. The last, but not least (query)…
    9. The database utilities code, 1 of 2
    10. The database utilities code, 2 of 2
      1. Using a data module supports future refactoring activities
    11. It’s nearly time for the database integration
    12. Cubicle Conversation
    13. It’s time to integrate your database code!
    14. Updating your existing webapp’s code
    15. Review your template(s) for changes…
    16. So… what’s the deal with your template?
    17. Let’s display a list of events…
    18. All that’s left is to draw the bar chart…
    19. Reviewing the most-recent swimclub.py code
    20. Meet the SVG-generating Jinja2 template
    21. Code is read more than it’s written.
    22. The convert_utils module
    23. list zip… what?!?
    24. Your database integrations are complete!
  18. 14. Deployment Revisited: The Finishing Touches…
    1. Cubicle Conversation
    2. Migrating to MariaDB
      1. Configuring MariaDB for the Coach’s webapp
    3. Moving the Coach’s data to MariaDB
      1. Reusing your tables, 1 of 2
    4. Apply three edits to schema.sql
    5. Reusing your tables, 2 of 2
    6. Let’s check your tables are defined correctly
    7. Copying your existing data to MariaDB
    8. Make your queries compatible with MariaDB
    9. Your database utility code need edits, too
    10. Create a new database on PythonAnywhere
    11. Adjust your database credentials dictionary
      1. Edit data_utils.py to support multiple locations
    12. Copying everything to the cloud
      1. Preparing your code and data for upload
    13. Update your webapp with your latest code
    14. Just a few more steps…
    15. Populate your cloud database with data
    16. It’s time for a PythonAnywhere Test Drive
    17. Is something wrong with PythonAnywhere?
    18. Cubicle Conversation
    19. The Coach is a happy chappy!
  19. A. The Top Ten Things We Didn’t Cover
    1. 1. Classes
      1. It’s not that we’re against classes…
      2. But, what if you can’t do without a custom class?
      3. What does Python class code look like?
      4. Playing cards with a class
    2. 2. Exceptions
    3. 3. Testing
    4. 4. The walrus operator
    5. 5. Where’s the switch? What switch?
    6. 6. Advanced language features
    7. 7. Concurrency
    8. 8. Type Hints
    9. 9. Virtual Environments
    10. 10. Tools
    11. Programmer’s code editors (and IDEs)
    12. Code formatters
      1. Taking notebooks to the next level

Product information

  • Title: Head First Python, 3rd Edition
  • Author(s): Paul Barry
  • Release date: August 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492051299