Automate the Boring Stuff with Python Workbook Projects and Exercises to Sharpen Your Python Skills (Al Sweigart)(Z-Library)
Python
No Description
189
Views
0
Downloads
0.00
Total Donations
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
® S W E IG A R T ® THE F INEST IN GEEK ENTERTA INMENT ™ www.nostarch.com This workbook transforms Al Sweigart’s best-selling guide from a reading experience into a coding experience. Following Automate the Boring Stuff with Python chapter by chapter, this workbook will help you turn concepts into muscle memory through carefully designed exercises, projects, and real Python scripts. Every concept from Automate is reinforced through carefully sequenced questions, exercises, and projects that help you think like a programmer and prove to yourself that you really get it. At the end of each chapter, you’ll tackle miniprojects that bring everything together. Whether you’re renaming fi les, scraping websites, converting text to speech, modifying spreadsheets, or sending emails, you’ll build scripts that do real work. Fun projects like image generators and word games are in the mix too, not just boring stuff. Every question and project come with a solution at the back of the book, so you’re never stuck. Inside, you’ll fi nd: • Fill-in-the-blank and short-answer questions that reinforce key concepts • Targeted exercises to build fl uency with variables, loops, functions, regular expressions, and more • Real Python scripts to run, tweak, and debug • Projects that automate common tasks like organizing fi les, backing up data, and scheduling programs • Full working code for every project so you can check your work and learn by example With over 500,000 copies sold worldwide, Automate the Boring Stuff with Python has become the go-to guide for learning Python. This workbook gives you the structure, feedback, and smart reinforcement to go from reading code to writing it with confi dence. A B O U T T H E A U T H O R Al Sweigart is a software developer, fellow of the Python Software Foundation, and author of several popular programming books including Automate the Boring Stuff with Python, The Big Book of Small Python Projects, Beyond the Basic Stuff with Python, Coding with Minecraft, and The Recursive Book of Recursion (all from No Starch Press). Y O U ’ V E S E E N W H A T P Y T H O N C A N D O . N O W G O D E E P E R . $39.99 ($53.99 CDN) “ I L I E F LAT.” This book uses a durable binding that won’t snap shut A U T O M A T E T H E B O R I N G S T U F F W I T H P Y T H O N W O R K B O O K ® P R O J E C T S A N D E X E R C I S E S T O S H A R P E N Y O U R P Y T H O N S K I L L S A U T O M A T E T H E B O R IN G S T U F F W IT H P Y T H O N W O R K B O O K A L S W E I G A R T
Page
2
(This page has no text content)
Page
3
AUTOMATE THE BORING STUFF WITH PYTHON WORKBOOK
Page
4
(This page has no text content)
Page
5
A U T O M AT E T H E B O R I N G S T U F F W I T H P Y T H O N W O R K B O O K P r o j e c t s a n d E x e r c i s e s t o S h a r p e n Yo u r Py t h o n S k i l l s by Al Sweigar t ® San Francisco
Page
6
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 rights@nostarch.com. Moral rights of the author have been asserted. Printed in the United States of America 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) Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com 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 info@nostarch.com. For information on distribution, bulk sales, or corporate sales: sales@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b-14, 11317 Tallinn, Estonia, hello@eucompliancepartner.com, +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. [E] ®
Page
7
For Loren
Page
8
(This page has no text content)
Page
9
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).
Page
10
(This page has no text content)
Page
11
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter 1: Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: if-else and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Chapter 3: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Chapter 4: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Chapter 5: Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Chapter 6: Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 7: Dictionaries and Structuring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 8: Strings and Text Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Chapter 9: Text Pattern Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . 61 Chapter 10: Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 11: Organizing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter 12: Designing and Deploying Command Line Programs . . . . . . . . . . . . . . . . . . . . 81 Chapter 13: Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Chapter 14: Excel Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Chapter 15: Google Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Chapter 16: SQLite Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter 17: PDF and Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Chapter 18: CSV, JSON, and XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Chapter 19: Keeping Time, Scheduling Tasks, and Launching Programs . . . . . . . . . . . . . . 131 Chapter 20: Sending Email, Texts, and Push Notifications . . . . . . . . . . . . . . . . . . . . . . . . 137 Chapter 21: Making Graphs and Manipulating Images . . . . . . . . . . . . . . . . . . . . . . . . . 141 Chapter 22: Recognizing Text in Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chapter 23: Controlling the Keyboard and Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Chapter 24: Text-to-Speech and Speech Recognition Engines . . . . . . . . . . . . . . . . . . . . . . 161 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Page
12
(This page has no text content)
Page
13
C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS xix INTRODUCTION xxi How to Use This Workbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii About the Activities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii A Note for Instructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv How to Answer Your Own Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv Continuing Your Programming Journey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv 1 PYTHON BASICS 1 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Entering Expressions into the Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . 2 The Integer, Floating-Point, and String Data Types . . . . . . . . . . . . . . . . . . . . . . 3 String Concatenation and Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Storing Values in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 How Computers Store Data with Binary Numbers . . . . . . . . . . . . . . . . . . . . . . 6 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Rectangle Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Perimeter and Area Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 IF-ELSE AND FLOW CONTROL 9 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Components of Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Flow Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Fixing the Safe Temperature Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Single-Expression Safe Temperature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Fizz Buzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3 LOOPS 17 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 while Loop Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 for Loops and the range() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Ending a Program Early with sys .exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Tree Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Christmas Tree Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Page
14
xii Contents in Detail 4 FUNCTIONS 25 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Creating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Return Values and return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 The None Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The Call Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Local and Global Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 Transaction Tracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Arithmetic Functions Without Arithmetic Operators . . . . . . . . . . . . . . . . . . . . 31 Tick Tock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5 DEBUGGING 33 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Mu’s Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Buggy Grade-Average Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Zero Division Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Leap Year Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Writing Buggy Code on Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 6 LISTS 39 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40 The List Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Augmented Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Short-Circuiting Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Sequence Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Pangram Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Coordinate Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 7 DICTIONARIES AND STRUCTURING DATA 47 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 The Dictionary Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Model Real-World Things Using Data Structures . . . . . . . . . . . . . . . . . . . . . . 49 Nested Dictionaries and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Random Weather Data Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Average-Temperature Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Chess Rook Capture Predictor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Page
15
Contents in Detail xiii 8 STRINGS AND TEXT EDITING 53 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 F-Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Useful String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Numeric Code Points of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Copying and Pasting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Word Match Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Diagonal Stripe Scroll Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 mOcKiNg SpOnGeBoB mEmE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 9 TEXT PATTERN MATCHING WITH REGULAR EXPRESSIONS 61 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62 The Syntax of Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Qualifier Syntax: What Characters to Match . . . . . . . . . . . . . . . . . . . . . . . . 63 Quantifier Syntax: How Many Qualifiers to Match . . . . . . . . . . . . . . . . . . . . 63 Greedy and Non-Greedy Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Matching at the Start and End of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Case-Insensitive Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Substituting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Managing Complex Regexes with Verbose Mode . . . . . . . . . . . . . . . . . . . . . 65 Humre: A Module for Human-Readable Regexes . . . . . . . . . . . . . . . . . . . . . . 65 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Hashtag-Finding Regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Price-Finding Regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Creating a CSV File of PyCon Speakers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Laugh Score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Word Twister—ordW wisterT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 10 READING AND WRITING FILES 69 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Files and Filepaths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 The File Reading and Writing Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Saving Variables with the shelve Module . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 Text File Combiner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Zigzag File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Rock, Paper, Scissors with Saved Games . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 11 ORGANIZING FILES 75 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 The shutil Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Walking a Directory Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Compressing Files with the zipfile Module . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78 Duplicate Filename Finder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Alphabetized Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 ZIP File Folder Extractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Page
16
xiv Contents in Detail 12 DESIGNING AND DEPLOYING COMMAND LINE PROGRAMS 81 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 A Program by Any Other Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Using the Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Virtual Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Installing Python Packages with pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Self-Aware Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Text-Based Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Pop-Up Message Boxes with PyMsgBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Deploying Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Compiling Python Programs with PyInstaller . . . . . . . . . . . . . . . . . . . . . . . . . 86 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Guess the Number with PyMsgBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Timer with PyMsgBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Compiling the Timer and Guess the Number Programs . . . . . . . . . . . . . . . . . 87 13 WEB SCRAPING 89 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 HTTP and HTTPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Downloading Files from the Web with the requests Module . . . . . . . . . . . . . . 90 Accessing a Weather API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Understanding HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Parsing HTML with Beautiful Soup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Controlling the Browser with Selenium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Controlling the Browser with Playwright . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 Headline Downloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Image Downloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Breadcrumb Follower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 HTML Chessboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 14 EXCEL SPREADSHEETS 99 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 Reading Excel Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Writing Excel Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Setting the Font Style of Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Adjusting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Search Term Finder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Excel Home Folder Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 15 GOOGLE SHEETS 105 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 Installing and Setting Up EZSheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Spreadsheet Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Sheet Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Google Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Working with Google Sheets Quotas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Page
17
Contents in Detail xv Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 Uploading All Files in a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Google Sheets Home Folder Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 16 SQLITE DATABASES 111 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112 Spreadsheets vs . Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 SQLite vs . Other SQL Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Creating Databases and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 CRUD Database Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Rolling Back Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Backing Up Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Altering and Dropping Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Joining Multiple Tables with Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . 116 In-Memory Databases and Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Monitoring Free Disk Space Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Database-to-String Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 17 PDF AND WORD DOCUMENTS 119 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 PDF Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 PDF Document Word Counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Searching All PDFs in a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Word Document Logger for Guess the Number . . . . . . . . . . . . . . . . . . . . . . 122 Converting Text Files to Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Bolding Words in a Word Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 18 CSV, JSON, AND XML FILES 125 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 The CSV Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Versatile Plaintext Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Fizz Buzz (CSV) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Guess the Number Statistics (CSV) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Guess the Number Statistics (JSON) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Guess the Number Statistics (XML) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 19 KEEPING TIME, SCHEDULING TASKS, AND LAUNCHING PROGRAMS 131 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132 The time Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 The datetime Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Launching Other Programs from Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 Alarm with Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Image Opener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 “Next Holiday” Reporter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Page
18
xvi Contents in Detail 20 SENDING EMAIL, TEXTS, AND PUSH NOTIFICATIONS 137 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 The Gmail API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 SMS Email Gateways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Push Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 “Quote of the Day” Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 “Quote of the Day” Push Notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 21 MAKING GRAPHS AND MANIPULATING IMAGES 141 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Computer Image Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Manipulating Images with Pillow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Drawing on Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Copying and Pasting Images to the Clipboard . . . . . . . . . . . . . . . . . . . . . . 145 Creating Graphs with Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Snowpal Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Rainbow Flag Image Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Clipboard Image Recorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 22 RECOGNIZING TEXT IN IMAGES 149 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Installing Tesseract and PyTesseract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 OCR Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Recognizing Text in Non-English Languages . . . . . . . . . . . . . . . . . . . . . . . . 151 The NAPS2 Scanner Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152 Searchable Web Comics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Enhancing Text in Web Comics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 23 CONTROLLING THE KEYBOARD AND MOUSE 153 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Staying on Track . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Controlling Mouse Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Controlling Mouse Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Planning Your Mouse Movements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Taking Screenshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Image Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Getting Window Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Controlling the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Jackson Pollock Bot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Mouse Movement Recorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Mouse Movement Playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Page
19
Contents in Detail xvii 24 TEXT-TO-SPEECH AND SPEECH RECOGNITION ENGINES 161 Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Text-to-Speech Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Speech Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Creating Subtitle Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Downloading Videos from Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 Knock-Knock Jokes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 12 Days of Christmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Podcast Word Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 ANSWERS 167
Page
20
(This page has no text content)
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Automate the Boring Stuff with Python Workbook: Projects and Exercises to Sharpen Your Python Skills
## 【One-Line Pitch】
A hands-on companion to Al Sweigart's classic *Automate the Boring Stuff with Python*, this workbook transforms reading into doing through 24 chapters of targeted exercises, debugging challenges, and real-world automation projects. Perfect for self-taught learners and instructors who want to move from understanding Python syntax to confidently writing scripts that save time.
## 【Book Arc】
- **Opening (~0%–10%)**: Introduces the workbook's purpose—turning the parent book into a coding experience—with fill-in-the-blank questions, short-answer exercises, and miniprojects that reinforce variables, expressions, and core syntax through interactive shell experimentation.
- **Early (~10%–23%)**: Covers fundamentals in sequence: flow control (if/else), loops, functions, debugging, lists, dictionaries, strings, and regular expressions, with each chapter building on the last through practice questions and small projects like the Tree Printer.
- **Early–Middle (~23%–32%)**: Moves into file handling and organizing files, then transitions to practical automation—command-line program design, web scraping with requests/Beautiful Soup/Selenium/Playwright, and working with Excel spreadsheets.
- **Middle (~32%–48%)**: Continues with Google Sheets, SQLite databases, PDF/Word documents, CSV/JSON/XML files, and introduces debugging challenges that require running programs under a debugger to find and fix bugs.
- **Late (~48%–end)**: Covers scheduling tasks, launching programs, sending emails/texts/push notifications, creating graphs, manipulating images, OCR text recognition, keyboard/mouse control, and text-to-speech/speech recognition—the full automation toolkit.
## 【Key Takeaways】
- **Interactive shell as a learning tool** (Early): The workbook explicitly encourages using Python's interactive shell to verify answers—not as cheating, but as professional practice for checking code behavior.
- **Pattern recognition in projects** (Early): Projects like the Tree Printer teach you to break down visual patterns into mathematical formulas (spaces = size - row_num, branches = row_num * 2 - 1), building algorithmic thinking.
- **Debugging as a core skill** (Middle): Dedicated debugging exercises—like fixing a grade-average program that returns 0 instead of 75—train you to use debuggers systematically rather than guessing.
- **Web scraping stack** (Early): Covers the full modern toolkit—requests for downloads, Beautiful Soup for HTML parsing, Selenium and Playwright for browser automation—with practical projects like headline and image downloaders.
- **Spreadsheet automation** (Early–Middle): Excel and Google Sheets chapters cover reading, writing, styling cells, formulas, row/column adjustment, and charts—directly applicable to office work.
- **Data format fluency** (Middle): CSV, JSON, and XML handling prepares you for real-world data exchange, while SQLite introduces database persistence for larger projects.
- **Complete automation ecosystem** (Late): The final chapters tie everything together—scheduling, notifications, image processing, OCR, GUI automation, and speech—showing how Python can handle nearly any repetitive task.
## 【Reading Tips】
- **Skim the practice questions, focus on projects**: The fill-in-the-blank and short-answer questions reinforce terminology, but the real learning comes from the end-of-chapter projects—prioritize building those.
- **Use the interactive shell aggressively**: Many answers can be discovered by experimenting at the `>>>` prompt. This builds the verification habit professional developers use daily.
- **Debugging chapters deserve deep reading**: The debugging exercises (like the ZeroDivisionError and Leap Year Calculator) are where you'll internalize systematic bug-finding—don't rush through them.
- **Skip around based on your needs**: The workbook is modular; if you already know Python basics, jump to the web scraping or spreadsheet chapters. If you're teaching, Chapters 1–11 cover language fundamentals suitable for any curriculum.
- **Expect to modify the projects**: The book provides full working code, but the real value is tweaking, breaking, and fixing scripts to understand how they work.
## 【Coverage Limits】
This guide covers the workbook's structure and representative content from the opening through roughly the middle chapters. The excerpts do not include detailed content from the later chapters (scheduling, email, image manipulation, OCR, GUI automation, speech recognition) beyond their chapter titles and learning objectives.
##
Passage locations
Excerpt 1
re developer, fellow of the mix too, not just boring stuff. Python Software Foundation, and author of several popular programming books including Automate Ev...
View in text
Page 11
. . . . . . . . 161 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 12 DESIGN...
View in text
Excerpt 3
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...
View in text
Excerpt 4
that follows the def statement is the body of the function. To correctly understand a program, you must be able to distin- guish the body, which runs only wh...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay