Introduction

Programming can be an intimidating topic. When people ask me if they’re learning the “right” way or reading the “right” books, I remind them that programming is a skill. Like all skills, you get better by doing it and challenging yourself. Calm seas don’t make skilled sailors.

I wrote the original Automate the Boring Stuff with Python book over 10 years ago, and since then it has sold over a half-million copies. That book teaches the syntax and third-party packages of the Python programming language, but you probably picked up this workbook because you know that the world of programming can’t be captured by any single text. In these pages, you’ll find additional practice questions and projects to challenge your ability to automate boring tasks with code.

How to Use This Workbook

The 24 chapters in this book correspond to the 24 chapters of Automate the Boring Stuff with Python, 3rd edition. You can work through the two books simultaneously or use this workbook if you’ve already read the original text and want to assess how well you’ve retained that knowledge.

But even if you haven’t read the source text, you’ll find this workbook useful, especially if you fall into any of the following categories:

  • Students of other Python textbooks or courses looking to fill in the gaps in their knowledge
  • Self-taught programmers who want to test how well they’ve mastered Python’s syntax and ecosystem
  • Instructors looking for additional curriculum materials
  • Programmers experienced in other languages who want to include Python in their toolkit of skills

No matter which group you fall into, I recommend working through the workbook’s problems multiple times to secure your understanding of the underlying ideas. Mastery comes not from obtaining knowledge but from being able to recall knowledge you’ve previously obtained. One way to achieve mastery is through spaced repetition: the practice of answering questions over time, with a focus on the questions you find most difficult. (Flash cards are a common form of spaced repetition studying.) Use this workbook for continuous practice, rather than reading it once and putting it back on the shelf to gather dust.

About the Activities

This book contains questions and practice projects organized in chapters and sections that correspond to those in Automate the Boring Stuff with Python. You’ll find the answers to the questions at the back of the workbook, along with light explanations and complete, runnable solution programs for the practice projects. There are many correct ways to write a program, and yours don’t have to match these solutions exactly. If you’re at a loss as to where to begin with your program, however, you can glance at the solution code before making a renewed attempt.

Here’s a brief rundown of the kinds of questions you’ll encounter in each chapter:

  • Chapter 1: Python Basics What do expressions and statements look like? What are variables, and what kinds of data can they hold?
  • Chapter 2: if-else and Flow Control How do conditions and expressions evaluate to Boolean values? How does indentation create blocks? What’s the difference between if, elif, and else statements?
  • Chapter 3: Loops What’s the difference between for and while loops, and what are the different arguments to the range() function? What do break and continue statements do?
  • Chapter 4: Functions How can you create your own functions with parameters and return values? How do variables work in global and local scopes?
  • Chapter 5: Debugging How do logging and the debugger save you time when fixing your programs? What are breakpoints and the different ways to step through a program?
  • Chapter 6: Lists What do lists make possible? How can you add, access, change, and remove data from lists? How do lists inside lists work?
  • Chapter 7: Dictionaries and Structuring Data How are key-value pairs in dictionaries different from data in lists? How can dictionaries and lists model real-world things as data?
  • Chapter 8: Strings and Text Editing How does data represent text, and what are all the text-related methods that Python offers?
  • Chapter 9: Text Pattern Matching with Regular Expressions How can you specify not just text, but a pattern of text using the mini-language of regular expressions?
  • Chapter 10: Reading and Writing Files How do computers organize files and folders in a filesystem? What does the pathlib module do? How does Python store data in text files and then read them back into programs later?
  • Chapter 11: Organizing Files How can you list, move, rename, copy, and delete files on your computer?
  • Chapter 12: Designing and Deploying Command Line Programs Once you get your program working, how can you easily run it without opening the code editor? How can you get it to run on someone else’s computer?
  • Chapter 13: Web Scraping How can your Python scripts download files, control a web browser, and retrieve information off of the internet?
  • Chapter 14: Excel Spreadsheets How can you create, access, and edit Excel spreadsheet files (even if you don’t have Excel installed)? How do you create charts and formulas in these files?
  • Chapter 15: Google Sheets How can you create, access, and edit online Google Sheets spreadsheets? How do you set up your Python script to safely use your Google account?
  • Chapter 16: SQLite Databases What are databases and tables, and what is the language for accessing and updating data in them? What are the advantages of SQLite’s simplicity compared to other databases?
  • Chapter 17: PDF and Word Documents How can you read and edit the content of PDF and Microsoft Word files?
  • Chapter 18: CSV, JSON, and XML Files What are data serialization formats, and what are they used for? What’s the difference between CSV, JSON, and XML, and how have their histories shaped their use?
  • Chapter 19: Keeping Time, Scheduling Tasks, and Launching Programs How are dates, timestamps, and calendars represented as data by programs? How can you schedule your Python programs to run other programs?
  • Chapter 20: Sending Email, Texts, and Push Notifications How can your Python scripts send notifications to your email address or mobile phone?
  • Chapter 21: Making Graphs and Manipulating Images How do computers represent images and color as data, and how can your programs create and modify images? How can you create images of different types of graphs?
  • Chapter 22: Recognizing Text in Images How can your programs read the text from an image or scanned document? How can you fix errors in the extraction process?
  • Chapter 23: Controlling the Keyboard and Mouse How do you send mouse clicks and keyboard key presses to other software so that your Python code can interact with them? How can your programs “see” what’s on the screen?
  • Chapter 24: Text-to-Speech and Speech Recognition Engines How do you make your programs say words through your computer’s speakers? How can your programs understand words in an audio file? What are the formats for video subtitles?

A Note for Instructors

This workbook can be a useful resource for instructors teaching from Automate the Boring Stuff with Python, or other sources. The questions in Chapters 1 through 11 in particular cover the Python language and standard library and can supplement any general Python curriculum.

All of the questions are answered in the back of the workbook, and any student with a copy of the workbook can read these answers. This may be of concern for instructors who want to assign these questions as homework. (Moreover, answers to these straightforward questions are readily found online or can be generated by large language model AIs such as ChatGPT.) Nevertheless, you can use these questions in the classroom or modify them for your own purposes.

The questions use the free-response format, meaning the student must directly provide the answer. They often encourage the student to experiment in the Python interactive shell. For example, a student can answer the question “Does round(4.9) evaluate to the integer 5 or the float 5.0?” by running the code. If the student doesn’t have access to a computer, you can make the questions easier by providing a multiple-choice answer format or an answer bank they can match to a set of questions.

How to Answer Your Own Questions

Software development is a large field, and no one can expect to memorize every part of it. It should come as no surprise, then, that programmers have created software to help them program. Search engines and Python’s interactive shell are also great ways to find the information you’re looking for. You should never consider it “cheating” to search for something online. Professional software engineers do it dozens of times a day!

Knowing how to find information online is an important skill, and it requires you to carefully think about what exactly it is you want to know. It’s often much faster to find existing answers online than it is to post your question somewhere and wait hours (or days or weeks!) for a reply.

If you do need to post a question, be specific. When I teach coding online to others, I often get comments like, “My program doesn’t work,” with no other information. It’s hard as an instructor to help in these cases; the comment isn’t even a question! One way to ask thoughtful questions is via rubber duck debugging: Put a rubber duck or some other inanimate object on your desk and explain your problem to it. You can do this out loud, or write your questions in an empty document on your computer. The key is to articulate your thoughts with actual words. Explain to the duck the answers to the following questions:

  • What do I want my program to do, really?
  • What does the program seem to be doing instead?
  • Does the program partially work? If so, where does it seem to break?
  • Is there an error message, and if so, what does it say?
  • What other questions could I ask myself to help me figure this issue out?

Programming is not a passive or magical activity: There are real, concrete answers to your questions, but you’ll have to reach for them yourself. Whenever you don’t understand why your program is doing something, remember that the answer always ends up being “The program did that because, well, technically that’s what the code I wrote does.”

Another way to answer your questions is to run some code in the interactive shell. By entering Python instructions at the >>> prompt, you can execute a single instruction and immediately see its result. For example, if you pass 9.9 to the int() function, will it return 9 or 10? What error message will it show if you pass a blank string instead? And if you pass a variable that contains an integer to int(), does it raise an exception or work fine? You don’t need to look up the Python documentation to answer these questions; just enter the code into the interactive shell and find out:

>>> int(9.9)
9

>>> int('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

>>> some_variable = 42
>>> int(some_variable)
42

Many of the questions in this book can be answered in this way. If you’re wondering how some code works, the best way to find out is to execute it yourself.

Also note that, even though error messages like ValueError: invalid literal for int() with base 10: '' aren’t very clear, you can copy and paste such messages into an internet search engine to find other people who have encountered the same error, then read their explanations on how to fix it.

Continuing Your Programming Journey

When experienced software developers try to help beginners, they commonly give two bits of misguided advice. The first is that beginners should contribute to open source projects as a way to build experience. In reality, open source projects tend to be large, complicated pieces of software, and making meaningful contributions to them is beyond the ability of beginners. Even trying to create a “small feature” or fix a “simple bug” involves learning the entire structure of the project. These projects are often maintained by unpaid volunteers who might not have time to help drop-in, one-off contributors become familiar with the code base.

The second piece of advice is to work on your own projects. While this is a good idea, it doesn’t offer guidance as to what sorts of projects one can make. Beginners often don’t know what’s possible, or what is beyond their capabilities. “Create an operating system” and “create an AI helper” sound cool but are far too complicated for a single individual of any skill level to tackle.

Beginners need guide rails rather than vague advice. Here are my recommendations for coming up with a software project to create:

  • Choose something you’re interested in Automate a task you regularly perform, make a small game you like, or replicate an app you’ve seen elsewhere and enjoyed.
  • Keep the scale small Don’t try to make a commercial application with many features. Projects always take longer than you think, so imagine the simplest version of the program you want to make.
  • Come up with several ideas You’ll find it encouraging to write multiple programs and frustrating to end up with one half-finished, large program.
  • Write a list of features your program won’t have Avoid the temptation to tell yourself “It’d be cool if the program did this” by committing to a set of features you’ll leave out.
  • Stick to the Python standard library, third-party packages, and platforms you already know  If you find yourself saying, “Before I start coding this, I need to learn X,” you should find a different project.

My other Python books have several such projects. They are short,
simple, and complete examples of basic programs that don’t require a
lot of setup or complex third-party packages:

  • The Big Book of Small Python Projects (No Starch Press, 2021) A collection of 81 projects ranging from games to simulations to digital art.
  • Invent Your Own Computer Games with Python, 4th edition (No Starch Press, 2016) A book for complete Python beginners that walks through the creation of projects including Hangman, Tic-Tac-Toe,
    and games with 2D graphics.
  • Cracking Codes with Python (No Starch Press, 2018) A guide to creating classic encryption and code-breaking programs for your laptop that don’t require a supercomputer, such as the Caesar cipher, Vigenère cipher, and brute-force dictionary hacking programs.

The solution programs to this book’s practice projects are available in the downloadable resources at https://nostarch.com/automate-workbook. You can use these projects as inspiration for making more elaborate projects of your own.

These simple programs should give you an idea of what’s possible at the beginner level. Testing your Python knowledge and writing code should make you well equipped to continue your programming journey.