Cover image. The book’s title is Automate the Boring Stuff with Python Workbook. The book’s subtitle is Projects and Exercises to Sharpen Your Python Skills. The author’s name is Al Sweigart. The illustration shows a blue pencil sketch of a robot sitting in a lawn chair reading a full-color copy of Automate the Boring Stuff with Python while a lawn mower drives by itself through the grass in the background. There are sketches of houses and trees in the background that look like a typical suburban neighborhood. The black No Starch Press stylized iron logo is in the bottom right corner.

AUTOMATE THE BORING STUFF WITH PYTHON WORKBOOK

Projects and Exercises to Sharpen Your Python Skills

by Al Sweigart

Publisher’s logo.

no starch press®

San Francisco

AUTOMATE THE BORING STUFF WITH PYTHON WORKBOOK. Copyright © 2026 by Al Sweigart.

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

Some rights reserved.

When attributing this work, you must credit the author as follows: “Al Sweigart, published by No Starch Press® Inc.,” provide a link to the license, and indicate if changes were made. You may not use the material for commercial purposes. For ShareAlike purposes, if you transform or build upon the material, you must distribute your contributions under the same license as the original.

Translations of this work are not covered under this license; all translation rights are reserved by the publisher. For permission to translate this work, please contact [email protected].

Moral rights of the author have been asserted.

First printing

29 28 27 26 25   1 2 3 4 5

ISBN-13: 978-1-7185-0450-9 (print)

ISBN-13: 978-1-7185-0451-6 (ebook)

Publisher’s logo. Shows a stylized iron with a registration symbol.

Published by No Starch Press®, Inc.

245 8th Street, San Francisco, CA 94103

phone: +1.415.863.9900

www.nostarch.com; [email protected]

Publisher: William Pollock

Managing Editor: Jill Franklin

Production Manager: Sabrina Plomitallo-González

Production Editor: Allison Felus

Developmental Editor: Frances Saux

Cover Illustrator: Rob Fiore

Interior Design: Octopod Studios with SPG

Technical Reviewer: Daniel Zingaro

Copyeditor: Audrey Doyle

Proofreader: Daniel Wolff

For permissions beyond the scope of this license or customer service inquiries, please contact [email protected]. For information on distribution, bulk sales, or corporate sales: [email protected]. To report counterfeit copies or piracy: [email protected]. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b-14, 11317 Tallinn, Estonia, [email protected], +3375690241.

No Starch Press and the No Starch Press iron logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.

For Loren

About the Author

Al Sweigart is a software developer, author, artist, and fellow of the Python Software Foundation. He is the author of several programming books for beginners, including Automate the Boring Stuff with Python, 3rd edition; Invent Your Own Computer Games with Python; The Big Book of Small Python Projects; and Beyond the Basic Stuff with Python (all from No Starch Press). He is a speaker at several international PyCon conferences. His website is https://inventwithpython.com.

Reports that Al is an AI have been grossly exaggerated.

About the Technical Reviewer

Dr. Daniel Zingaro is an associate professor of computer science at the University of Toronto. He is internationally known for his uniquely interactive approach to teaching, his leading research on teaching with generative AI, and his learner-centered textbooks, which are used by thousands of students around the world. He is the author of Algorithmic Thinking, 2nd edition (No Starch Press, 2024) and Learn to Code by Solving Problems (No Starch Press, 2021) and a co-author of Learn AI-Assisted Python Programming with GitHub Copilot and ChatGPT (Manning, 2023).

BRIEF CONTENTS

  1. Acknowledgments

  2. Introduction

  3. Chapter 1: Python Basics

  4. Chapter 2: if-else and Flow Control

  5. Chapter 3: Loops

  6. Chapter 4: Functions

  7. Chapter 5: Debugging

  8. Chapter 6: Lists

  9. Chapter 7: Dictionaries and Structuring Data

  10. Chapter 8: Strings and Text Editing

  11. Chapter 9: Text Pattern Matching with Regular Expressions

  12. Chapter 10: Reading and Writing Files

  13. Chapter 11: Organizing Files

  14. Chapter 12: Designing and Deploying Command Line Programs

  15. Chapter 13: Web Scraping

  16. Chapter 14: Excel Spreadsheets

  17. Chapter 15: Google Sheets

  18. Chapter 16: SQLite Databases

  19. Chapter 17: PDF and Word Documents

  20. Chapter 18: CSV, JSON, and XML Files

  21. Chapter 19: Keeping Time, Scheduling Tasks, and Launching Programs

  22. Chapter 20: Sending Email, Texts, and Push Notifications

  23. Chapter 21: Making Graphs and Manipulating Images

  24. Chapter 22: Recognizing Text in Images

  25. Chapter 23: Controlling the Keyboard and Mouse

  26. Chapter 24: Text-to-Speech and Speech Recognition Engines

  27. Answers

CONTENTS IN DETAIL

  1. ACKNOWLEDGMENTS

  2. INTRODUCTION

    1. How to Use This Workbook

    2. About the Activities

    3. A Note for Instructors

    4. How to Answer Your Own Questions

    5. Continuing Your Programming Journey

  3. 1PYTHON BASICS

  4. Practice Questions

    1. Entering Expressions into the Interactive Shell

    2. The Integer, Floating-Point, and String Data Types

    3. String Concatenation and Replication

    4. Storing Values in Variables

    5. Your First Program

    6. How Computers Store Data with Binary Numbers

  5. Practice Projects

    1. Rectangle Printer

    2. Perimeter and Area Calculator

  6. 2IF-ELSE AND FLOW CONTROL

  7. Practice Questions

    1. Boolean Values

    2. Comparison Operators

    3. Boolean Operators

    4. Components of Flow Control

    5. Flow Control Statements

  8. Practice Projects

    1. Fixing the Safe Temperature Program

    2. Single-Expression Safe Temperature

    3. Fizz Buzz

  9. 3LOOPS

  10. Practice Questions

    1. while Loop Statements

    2. for Loops and the range() Function

    3. Importing Modules

    4. Ending a Program Early with sys.exit()

  11. Practice Projects

    1. Tree Printer

    2. Christmas Tree Printer

  12. 4FUNCTIONS

  13. Practice Questions

    1. Creating Functions

    2. Arguments and Parameters

    3. Return Values and return Statements

    4. The None Value

    5. The Call Stack

    6. Local and Global Scopes

    7. Exception Handling

  14. Practice Projects

    1. Transaction Tracker

    2. Arithmetic Functions Without Arithmetic Operators

    3. Tick Tock

  15. 5DEBUGGING

  16. Practice Questions

    1. Raising Exceptions

    2. Assertions

    3. Logging

    4. Mu’s Debugger

  17. Practice Projects

    1. Buggy Grade-Average Calculator

    2. Zero Division Error

    3. Leap Year Calculator

    4. Writing Buggy Code on Purpose

  18. 6LISTS

  19. Practice Questions

    1. The List Data Type

    2. Working with Lists

    3. Augmented Assignment Operators

    4. Methods

    5. Short-Circuiting Boolean Operators

    6. Sequence Data Types

    7. References

  20. Practice Projects

    1. Pangram Detector

    2. Coordinate Directions

  21. 7DICTIONARIES AND STRUCTURING DATA

  22. Practice Questions

    1. The Dictionary Data Type

    2. Model Real-World Things Using Data Structures

    3. Nested Dictionaries and Lists

  23. Practice Projects

    1. Random Weather Data Generator

    2. Average-Temperature Analyzer

    3. Chess Rook Capture Predictor

  24. 8STRINGS AND TEXT EDITING

  25. Practice Questions

    1. Working with Strings

    2. F-Strings

    3. Useful String Methods

    4. Numeric Code Points of Characters

    5. Copying and Pasting Strings

  26. Practice Projects

    1. Word Match Game

    2. Diagonal Stripe Scroll Animation

    3. mOcKiNg SpOnGeBoB mEmE

  27. 9TEXT PATTERN MATCHING WITH REGULAR EXPRESSIONS

  28. Practice Questions

    1. The Syntax of Regular Expressions

    2. Qualifier Syntax: What Characters to Match

    3. Quantifier Syntax: How Many Qualifiers to Match

    4. Greedy and Non-Greedy Matching

    5. Matching at the Start and End of a String

    6. Case-Insensitive Matching

    7. Substituting Strings

    8. Managing Complex Regexes with Verbose Mode

    9. Humre: A Module for Human-Readable Regexes

  29. Practice Projects

    1. Hashtag-Finding Regex

    2. Price-Finding Regex

    3. Creating a CSV File of PyCon Speakers

    4. Laugh Score

    5. Word Twister—ordW wisterT

  30. 10READING AND WRITING FILES

  31. Practice Questions

    1. Files and Filepaths

    2. The File Reading and Writing Process

    3. Saving Variables with the shelve Module

  32. Practice Projects

    1. Text File Combiner

    2. Zigzag File

    3. Rock, Paper, Scissors with Saved Games

  33. 11ORGANIZING FILES

  34. Practice Questions

    1. The shutil Module

    2. Walking a Directory Tree

    3. Compressing Files with the zipfile Module

  35. Practice Projects

    1. Duplicate Filename Finder

    2. Alphabetized Folders

    3. ZIP File Folder Extractor

  36. 12DESIGNING AND DEPLOYING COMMAND LINE PROGRAMS

  37. Practice Questions

    1. A Program by Any Other Name

    2. Using the Terminal

    3. Virtual Environments

    4. Installing Python Packages with pip

    5. Self-Aware Python Programs

    6. Text-Based Program Design

    7. Pop-Up Message Boxes with PyMsgBox

    8. Deploying Python Programs

    9. Compiling Python Programs with PyInstaller

  38. Practice Projects

    1. Guess the Number with PyMsgBox

    2. Timer with PyMsgBox

    3. Compiling the Timer and Guess the Number Programs

  39. 13WEB SCRAPING

  40. Practice Questions

    1. HTTP and HTTPS

    2. Downloading Files from the Web with the requests Module

    3. Accessing a Weather API

    4. Understanding HTML

    5. Parsing HTML with Beautiful Soup

    6. Controlling the Browser with Selenium

    7. Controlling the Browser with Playwright

  41. Practice Projects

    1. Headline Downloader

    2. Image Downloader

    3. Breadcrumb Follower

    4. HTML Chessboard

  42. 14EXCEL SPREADSHEETS

  43. Practice Questions

    1. Reading Excel Files

    2. Writing Excel Documents

    3. Setting the Font Style of Cells

    4. Formulas

    5. Adjusting Rows and Columns

    6. Charts

    7. Practice Projects

      1. Search Term Finder

      2. Excel Home Folder Report

    8. 15GOOGLE SHEETS

    9. Practice Questions

      1. Installing and Setting Up EZSheets

      2. Spreadsheet Objects

      3. Sheet Objects

      4. Google Forms

      5. Working with Google Sheets Quotas

    10. Practice Projects

      1. Uploading All Files in a Folder

      2. Google Sheets Home Folder Report

    11. 16SQLITE DATABASES

    12. Practice Questions

      1. Spreadsheets vs. Databases

      2. SQLite vs. Other SQL Databases

      3. Creating Databases and Tables

      4. CRUD Database Operations

      5. Rolling Back Transactions

      6. Backing Up Databases

      7. Altering and Dropping Tables

      8. Joining Multiple Tables with Foreign Keys

      9. In-Memory Databases and Backups

    13. Practice Projects

      1. Monitoring Free Disk Space Levels

      2. Database-to-String Converter

    14. 17PDF AND WORD DOCUMENTS

    15. Practice Questions

      1. PDF Documents

      2. Word Documents

    16. Practice Projects

      1. PDF Document Word Counter

      2. Searching All PDFs in a Folder

      3. Word Document Logger for Guess the Number

      4. Converting Text Files to Word Documents

      5. Bolding Words in a Word Document

    17. 18CSV, JSON, AND XML FILES

    18. Practice Questions

      1. The CSV Format

      2. Versatile Plaintext Formats

    19. Practice Projects

      1. Fizz Buzz (CSV)

      2. Guess the Number Statistics (CSV)

      3. Guess the Number Statistics (JSON)

      4. Guess the Number Statistics (XML)

    20. 19KEEPING TIME, SCHEDULING TASKS, AND LAUNCHING PROGRAMS

    21. Practice Questions

      1. The time Module

      2. The datetime Module

      3. Launching Other Programs from Python

    22. Practice Projects

      1. Alarm with Sound

      2. Image Opener

      3. “Next Holiday” Reporter

    23. 20SENDING EMAIL, TEXTS, AND PUSH NOTIFICATIONS

    24. Practice Questions

      1. The Gmail API

      2. SMS Email Gateways

      3. Push Notifications

    25. Practice Projects

      1. “Quote of the Day” Email

      2. “Quote of the Day” Push Notification

    26. 21MAKING GRAPHS AND MANIPULATING IMAGES

    27. Practice Questions

      1. Computer Image Fundamentals

      2. Manipulating Images with Pillow

      3. Drawing on Images

      4. Copying and Pasting Images to the Clipboard

      5. Creating Graphs with Matplotlib

    28. Practice Projects

      1. Snowpal Image

      2. Rainbow Flag Image Generator

      3. Clipboard Image Recorder

    29. 22RECOGNIZING TEXT IN IMAGES

    30. Practice Questions

      1. Installing Tesseract and PyTesseract

      2. OCR Fundamentals

      3. Recognizing Text in Non-English Languages

      4. The NAPS2 Scanner Application

    31. Practice Projects

      1. Searchable Web Comics

      2. Enhancing Text in Web Comics

    32. 23CONTROLLING THE KEYBOARD AND MOUSE

    33. Practice Questions

      1. Staying on Track

      2. Controlling Mouse Movement

      3. Controlling Mouse Interaction

      4. Planning Your Mouse Movements

      5. Taking Screenshots

      6. Image Recognition

      7. Getting Window Information

      8. Controlling the Keyboard

    34. Practice Projects

      1. Jackson Pollock Bot

      2. Mouse Movement Recorder

      3. Mouse Movement Playback

    35. 24TEXT-TO-SPEECH AND SPEECH RECOGNITION ENGINES

    36. Practice Questions

      1. Text-to-Speech Engine

      2. Speech Recognition

      3. Creating Subtitle Files

      4. Downloading Videos from Websites

    37. Practice Projects

      1. Knock-Knock Jokes

      2. 12 Days of Christmas

      3. Podcast Word Search

    38. ANSWERS

ACKNOWLEDGMENTS

It’s misleading to have just my name on the cover.

I couldn’t have written this book without the help of a lot of people. I’d like to thank my publisher, Bill Pollock; my editors, Jill Franklin, Sabrina Plomitallo-González, Allison Felus, Frances Saux, and Audrey Doyle; and the rest of the staff at No Starch Press for their invaluable help. Thanks so much to my tech reviewer, Daniel Zingaro, for great suggestions, edits, and support.

Many thanks to everyone at the Python Software Foundation for their great work. The organizers and volunteers of all the various PyCon and DjangoCon conferences are extraordinary. The Python community is the best one I’ve found in the tech industry.

Thank you.