Tải bản đầy đủ (.pdf) (505 trang)

Ebook Automate the Boring Stuff with Python

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (16.64 MB, 505 trang )

LEARN PYTHON.
GET STUFF DONE.

In Automate the Boring Stuff with Python, you’ll
learn how to use Python to write programs that do in
minutes what would take you hours to do by hand—
no prior programming experience required. Once
you’ve mastered the basics of programming, you’ll
create Python programs that effortlessly perform
useful and impressive feats of automation to:
• Search for text in a file or across multiple files
• Create, update, move, and rename files and
folders
• Search the Web and download online content
• Update and format data in Excel spreadsheets
of any size

• Send reminder emails and text notifications
• Fill out online forms
Step-by-step instructions walk you through each
program, and practice projects at the end of each
chapter challenge you to improve those programs and
use your newfound skills to automate similar tasks.
Don’t spend your time doing work a well-trained
monkey could do. Even if you’ve never written a line
of code, you can make your computer do the grunt work.
Learn how in Automate the Boring Stuff with Python.
ABOUT THE AUTHOR

Al Sweigart is a software developer and teaches programming to kids and adults. He has written several
Python books for beginners, including Hacking Secret


Ciphers with Python, Invent Your Own Computer Games
with Python, and Making Games with Python & Pygame.

• Split, merge, watermark, and encrypt PDFs

COVERS PYTHON 3

w w w.nostarch.com

“ I L I E F L AT .”

SFI-00000

$29.95 ($34.95 CDN)
SHELVE IN:
PROGRAMMING LANGUAGES/
PYTHON

This book uses a durable binding that won’t snap shut.

SWEIGART

T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™

AU TOM AT E T HE BOR ING
STUFF WITH PY THON

If you’ve ever spent hours renaming files or updating
hundreds of spreadsheet cells, you know how tedious
tasks like these can be. But what if you could have

your computer do them for you?

AUTOMATE
THE BORING STUFF
WITH PYTHON
P R A C T I C A L

P R O G R A M M I N G
F O R T O T A L B E G I N N E R S
AL SWEIGART


Automate the Boring Stuff
with Python


Automate the
Boring Stuff
with Python
Practical Programming
for Total Beginners

b y Al S w e i g a r t

San Francisco


Automate the Boring Stuff with Python. Copyright © 2015 by Al Sweigart.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval

system, without the prior written permission of the copyright owner and the publisher.
Printed in USA
Second printing
19 18 17 16 15   2 3 4 5 6 7 8 9
ISBN-10: 1-59327-599-4
ISBN-13: 978-1-59327-599-0

SFI-00000

Publisher: William Pollock
Production Editor: Laurel Chun
Cover Illustration: Josh Ellingson
Interior Design: Octopod Studios
Developmental Editors: Jennifer Griffith-Delgado, Greg Poulos, and Leslie Shen
Technical Reviewer: Ari Lacenski
Copyeditor: Kim Wimpsett
Compositor: Susan Glinert Stevens
Proofreader: Lisa Devoto Farrell
Indexer: BIM Indexing and Proofreading Services
For information on distribution, translations, or bulk sales,
please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 415.863.9900;
www.nostarch.com
Library of Congress Control Number: 2014953114
No Starch Press and the No Starch Press 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.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United
States License. To view a copy of this license, visit />or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.


For my nephew Jack


About the Author
Al Sweigart is a software developer and tech book author living in San
­Francisco. Python is his favorite programming language, and he is the
developer of several open source modules for it. His other books are freely
available under a Creative Commons license on his website http://www
.inventwithpython.com/. His cat weighs 14 pounds.

About the Tech Reviewer
Ari Lacenski is a developer of Android applications and Python software.
She lives in San Francisco, where she writes about Android programming at
and mentors with Women Who Code. She’s also a
folk guitarist.


Brief Content s

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Part I: Python Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Chapter 1: Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 2: Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Chapter 3: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Chapter 4: Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Chapter 5: Dictionaries and Structuring Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Chapter 6: Manipulating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

Part II: Automating Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Chapter 7: Pattern Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Chapter 8: Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Chapter 9: Organizing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Chapter 10: Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Chapter 11: Web Scraping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Chapter 12: Working with Excel Spreadsheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Chapter 13: Working with PDF and Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Chapter 14: Working with CSV Files and JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Chapter 15: Keeping Time, Scheduling Tasks, and Launching Programs . . . . . . . . . . . . . . 335
Chapter 16: Sending Email and Text Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Chapter 17: Manipulating Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Chapter 18: Controlling the Keyboard and Mouse with GUI Automation . . . . . . . . . . . . . . 413


Appendix A: Installing Third-Party Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Appendix B: Running Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Appendix C: Answers to the Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461



Con t e n t s in De ta il
Acknowledgments

xxiii

Introduction1
Whom Is This Book For? . . . . . . . . . . . . . . . . . . . . . . .
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Is Programming? . . . . . . . . . . . . . . . . . . . . . . . .
What Is Python? . . . . . . . . . . . . . . . . . . . . . . .
Programmers Don’t Need to Know Much Math .
Programming Is a Creative Activity . . . . . . . . . .
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Downloading and Installing Python . . . . . . . . . . . . . . . .
Starting IDLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Interactive Shell . . . . . . . . . . . . . . . . . . . .
How to Find Help . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Asking Smart Programming Questions . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

. 2
. 2

. 3
. 4
. 4
. 5
. 5
. 6
. 7
. 8
. 8
. 9
10

Part I: Python Programming Basics

11

1
Python Basics

13

Entering Expressions into the Interactive Shell . . . .
The Integer, Floating-Point, and String Data Types .
String Concatenation and Replication . . . . . . . . .
Storing Values in Variables . . . . . . . . . . . . . . . .
Assignment Statements . . . . . . . . . . . . .
Variable Names . . . . . . . . . . . . . . . . .
Your First Program . . . . . . . . . . . . . . . . . . . . . . .
Dissecting Your Program . . . . . . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . . . . .

The print() Function . . . . . . . . . . . . . . . .
The input() Function . . . . . . . . . . . . . . .
Printing the User’s Name . . . . . . . . . . . .
The len() Function . . . . . . . . . . . . . . . . .
The str(), int(), and float() Functions . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . .

2
Flow Control

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

14
16
17
18
18
20
21
22
23
23

23
24
24
25
28
28

31

Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35


Binary Boolean Operators . . . . . . . .
The not Operator . . . . . . . . . . . . . .
Mixing Boolean and Comparison Operators .
Elements of Flow Control . . . . . . . . . . . . . . .
Conditions . . . . . . . . . . . . . . . . . .
Blocks of Code . . . . . . . . . . . . . . .
Program Execution . . . . . . . . . . . . . . . . . . .
Flow Control Statements . . . . . . . . . . . . . . . .
if Statements . . . . . . . . . . . . . . . . .
else Statements . . . . . . . . . . . . . . .
elif Statements . . . . . . . . . . . . . . . .
while Loop Statements . . . . . . . . . .
break Statements . . . . . . . . . . . . . .
continue Statements . . . . . . . . . . . .
for Loops and the range() Function . .
Importing Modules . . . . . . . . . . . . . . . . . . .

from import Statements . . . . . . . . . .
Ending a Program Early with sys.exit() . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

35
36
36
37
37
37
38
38
38
39
40
45
49
50
53
57
58
58
58
59

3
Functions61
def Statements with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . .
Return Values and return Statements . . . . . . . . . . . . . . . . . . . . . . .

The None Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Keyword Arguments and print() . . . . . . . . . . . . . . . . . . . . . . . . . .
Local and Global Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Local Variables Cannot Be Used in the Global Scope . . . . .
Local Scopes Cannot Use Variables in Other Local Scopes .
Global Variables Can Be Read from a Local Scope . . . . . .
Local and Global Variables with the Same Name . . . . . . .
The global Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Short Program: Guess the Number . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Collatz Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . .
Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

63
63

65
65
67
67
68
69
69
70
72
74
76
76
77
77
77

4
Lists79
The List Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting Individual Values in a List with Indexes .
Negative Indexes . . . . . . . . . . . . . . . . . . . . . .
Getting Sublists with Slices . . . . . . . . . . . . . . .
Getting a List’s Length with len() . . . . . . . . . . . .
Changing Values in a List with Indexes . . . . . . .

x   Contents in Detail

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


80
80
82
82
83
83


List Concatenation and List Replication . . . . . . . . . . . . . . . . .
Removing Values from Lists with del Statements . . . . . . . . . . .
Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using for Loops with Lists . . . . . . . . . . . . . . . . . . . . . . . . . .
The in and not in Operators . . . . . . . . . . . . . . . . . . . . . . . .
The Multiple Assignment Trick . . . . . . . . . . . . . . . . . . . . . . .
Augmented Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finding a Value in a List with the index() Method . . . . . . . . . .
Adding Values to Lists with the append() and insert() Methods .
Removing Values from Lists with remove() . . . . . . . . . . . . . . .
Sorting the Values in a List with the sort() Method . . . . . . . . . .
Example Program: Magic 8 Ball with a List . . . . . . . . . . . . . . . . . . . . .
List-like Types: Strings and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mutable and Immutable Data Types . . . . . . . . . . . . . . . . . . .
The Tuple Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Converting Types with the list() and tuple() Functions . . . . . . . .
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The copy Module’s copy() and deepcopy() Functions . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comma Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Character Picture Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

5
Dictionaries and Structuring Data

105

The Dictionary Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dictionaries vs. Lists . . . . . . . . . . . . . . . . . . . . . . . . . .
The keys(), values(), and items() Methods . . . . . . . . . . .
Checking Whether a Key or Value Exists in a Dictionary .

The get() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The setdefault() Method . . . . . . . . . . . . . . . . . . . . . . . .
Pretty Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Data Structures to Model Real-World Things . . . . . . . . . . .
A Tic-Tac-Toe Board . . . . . . . . . . . . . . . . . . . . . . . . . .
Nested Dictionaries and Lists . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fantasy Game Inventory . . . . . . . . . . . . . . . . . . . . . . .
List to Dictionary Function for Fantasy Game Inventory . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

6
Manipulating Strings
Working with Strings . . . . . . . . . . . . . . . . . . . . .

String Literals . . . . . . . . . . . . . . . . . . . .
Indexing and Slicing Strings . . . . . . . . .
The in and not in Operators with Strings .

. 83
. 84
. 84
. 86
. 87
. 87
. 88
. 89
. 89
. 89
. 90
. 91
. 92
. 93
. 94
. 96
. 97
. 97
100
100
101
102
102
102
103


105
106
107
109
109
110
111
112
113
117
119
119
120
120
120

123
.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

123
124
126
127
Contents in Detail   xi


Useful String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The upper(), lower(), isupper(), and islower() String Methods .
The isX String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . .
The startswith() and endswith() String Methods . . . . . . . . . . .
The join() and split() String Methods . . . . . . . . . . . . . . . . . .
Justifying Text with rjust(), ljust(), and center() . . . . . . . . . . . .
Removing Whitespace with strip(), rstrip(), and lstrip() . . . . . .
Copying and Pasting Strings with the pyperclip Module . . . .
Project: Password Locker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Program Design and Data Structures . . . . . . . . . . . .
Step 2: Handle Command Line Arguments . . . . . . . . . . . . .

Step 3: Copy the Right Password . . . . . . . . . . . . . . . . . . . .
Project: Adding Bullets to Wiki Markup . . . . . . . . . . . . . . . . . . . . . .
Step 1: Copy and Paste from the Clipboard . . . . . . . . . . . .
Step 2: Separate the Lines of Text and Add the Star . . . . . . .
Step 3: Join the Modified Lines . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Table Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


Part II: Automating Tasks

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

145

7
Pattern Matching with Regular Expressions
Finding Patterns of Text Without Regular Expressions . . . . . .
Finding Patterns of Text with Regular Expressions . . . . . . . .

Creating Regex Objects . . . . . . . . . . . . . . . . . . .
Matching Regex Objects . . . . . . . . . . . . . . . . . .
Review of Regular Expression Matching . . . . . . . .
More Pattern Matching with Regular Expressions . . . . . . . .
Grouping with Parentheses . . . . . . . . . . . . . . . . .
Matching Multiple Groups with the Pipe . . . . . . . .
Optional Matching with the Question Mark . . . . . .
Matching Zero or More with the Star . . . . . . . . . .
Matching One or More with the Plus . . . . . . . . . .
Matching Specific Repetitions with Curly Brackets .
Greedy and Nongreedy Matching . . . . . . . . . . . . . . . . . .
The findall() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making Your Own Character Classes . . . . . . . . . . . . . . . .
The Caret and Dollar Sign Characters . . . . . . . . . . . . . . . .
The Wildcard Character . . . . . . . . . . . . . . . . . . . . . . . . .
Matching Everything with Dot-Star . . . . . . . . . . . .
Matching Newlines with the Dot Character . . . . . .
Review of Regex Symbols . . . . . . . . . . . . . . . . . . . . . . . . .
Case-Insensitive Matching . . . . . . . . . . . . . . . . . . . . . . . .

xii   Contents in Detail

127
128
129
131
131
133
134

135
136
136
137
137
139
139
140
141
141
142
142
142

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

147
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

148
150
150
151
152
152
152
153
154
155
155
156
156
157
158
159
159
160
161
162
162
163


Substituting Strings with the sub() Method . . . . . . . . . . . . . . . . . .
Managing Complex Regexes . . . . . . . . . . . . . . . . . . . . . . . . . .
Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE . . . . .
Project: Phone Number and Email Address Extractor . . . . . . . . . .

Step 1: Create a Regex for Phone Numbers . . . . . . . . .
Step 2: Create a Regex for Email Addresses . . . . . . . . .
Step 3: Find All Matches in the Clipboard Text . . . . . . .
Step 4: Join the Matches into a String for the Clipboard .
Running the Program . . . . . . . . . . . . . . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Strong Password Detection . . . . . . . . . . . . . . . . . . . . .
Regex Version of strip() . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

8
Reading and Writing Files
Files and File Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Backslash on Windows and Forward Slash on OS X and Linux .
The Current Working Directory . . . . . . . . . . . . . . . . . . . . . . .
Absolute vs. Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating New Folders with os.makedirs() . . . . . . . . . . . . . . . .
The os.path Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Absolute and Relative Paths . . . . . . . . . . . . . . . . . . .
Finding File Sizes and Folder Contents . . . . . . . . . . . . . . . . . .
Checking Path Validity . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The File Reading/Writing Process . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Files with the open() Function . . . . . . . . . . . . . . . . . .
Reading the Contents of Files . . . . . . . . . . . . . . . . . . . . . . . . .
Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Variables with the shelve Module . . . . . . . . . . . . . . . . . . . . . . .
Saving Variables with the pprint.pformat() Function . . . . . . . . . . . . . . . .
Project: Generating Random Quiz Files . . . . . . . . . . . . . . . . . . . . . . . .

Step 1: Store the Quiz Data in a Dictionary . . . . . . . . . . . . . .
Step 2: Create the Quiz File and Shuffle the Question Order . . .
Step 3: Create the Answer Options . . . . . . . . . . . . . . . . . . . .
Step 4: Write Content to the Quiz and Answer Key Files . . . . .
Project: Multiclipboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Comments and Shelf Setup . . . . . . . . . . . . . . . . . . . . .
Step 2: Save Clipboard Content with a Keyword . . . . . . . . . . .
Step 3: List Keywords and Load a Keyword’s Content . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending the Multiclipboard . . . . . . . . . . . . . . . . . . . . . . . . .
Mad Libs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Regex Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

163
164
164
165
166
166
167
168
169
169
169
170
171
171
171


173
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

173
174
175
175
176
177
177
179
180
180
181
182
183
184
185
186
187
188

189
189
191
192
192
193
194
194
194
194
195
195

Contents in Detail   xiii


9
Organizing Files

197

The shutil Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copying Files and Folders . . . . . . . . . . . . . . . . . . . . . .
Moving and Renaming Files and Folders . . . . . . . . . . . .
Permanently Deleting Files and Folders . . . . . . . . . . . . .
Safe Deletes with the send2trash Module . . . . . . . . . . .
Walking a Directory Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Compressing Files with the zipfile Module . . . . . . . . . . . . . . . . .
Reading ZIP Files . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extracting from ZIP Files . . . . . . . . . . . . . . . . . . . . . . .

Creating and Adding to ZIP Files . . . . . . . . . . . . . . . . .
Project: Renaming Files with American-Style Dates to
European-Style Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Create a Regex for American-Style Dates . . . . . .
Step 2: Identify the Date Parts from the Filenames . . . . .
Step 3: Form the New Filename and Rename the Files . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . .
Project: Backing Up a Folder into a ZIP File . . . . . . . . . . . . . . . .
Step 1: Figure Out the ZIP File’s Name . . . . . . . . . . . . .
Step 2: Create the New ZIP File . . . . . . . . . . . . . . . . . .
Step 3: Walk the Directory Tree and Add to the ZIP File .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selective Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting Unneeded Files . . . . . . . . . . . . . . . . . . . . . . .
Filling in the Gaps . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

198
198
199
200
201
202
203
204
205
205

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


206
206
207
209
209
209
210
211
211
212
212
213
213
213
213
214

10
Debugging215
Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . .
Getting the Traceback as a String . . . . . . . . . . . . . . . . .
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using an Assertion in a Traffic Light Simulation .
Disabling Assertions . . . . . . . . . . . . . . . . . . . .
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the logging Module . . . . . . . . . . . . . . . .
Don’t Debug with print() . . . . . . . . . . . . . . . . .
Logging Levels . . . . . . . . . . . . . . . . . . . . . . . .
Disabling Logging . . . . . . . . . . . . . . . . . . . . .
Logging to a File . . . . . . . . . . . . . . . . . . . . . .

IDLE’s Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xiv   Contents in Detail

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

216
217
219
219
221
221
221
223
223
224
225
225
226
226
226
227


Quit . . . . . . . . . . . . .
Debugging a Number
Breakpoints . . . . . . . .
Summary . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . .
Practice Project . . . . . . . . . . . .
Debugging Coin Toss .


. . . . . . . . . . . . .
Adding Program .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.

11
Web Scraping
Project: mapIt.py with the webbrowser Module . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Figure Out the URL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 2: Handle the Command Line Arguments . . . . . . . . . . . . . . . . . . . .
Step 3: Handle the Clipboard Content and Launch the Browser . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Downloading Files from the Web with the requests Module . . . . . . . . . . . . . . . . .
Downloading a Web Page with the requests.get() Function . . . . . . . . . . .
Checking for Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Saving Downloaded Files to the Hard Drive . . . . . . . . . . . . . . . . . . . . . . . . . . . .
HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resources for Learning HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Quick Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Viewing the Source HTML of a Web Page . . . . . . . . . . . . . . . . . . . . . . .
Opening Your Browser’s Developer Tools . . . . . . . . . . . . . . . . . . . . . . .
Using the Developer Tools to Find HTML Elements . . . . . . . . . . . . . . . . .
Parsing HTML with the BeautifulSoup Module . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a BeautifulSoup Object from HTML . . . . . . . . . . . . . . . . . . . . .
Finding an Element with the select() Method . . . . . . . . . . . . . . . . . . . . .
Getting Data from an Element’s Attributes . . . . . . . . . . . . . . . . . . . . . . .
Project: “I’m Feeling Lucky” Google Search . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Get the Command Line Arguments and Request the Search Page .

Step 2: Find All the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 3: Open Web Browsers for Each Result . . . . . . . . . . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project: Downloading All XKCD Comics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Design the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 2: Download the Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 3: Find and Download the Comic Image . . . . . . . . . . . . . . . . . . . .
Step 4: Save the Image and Find the Previous Comic . . . . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling the Browser with the selenium Module . . . . . . . . . . . . . . . . . . . . . . . .
Starting a Selenium-Controlled Browser . . . . . . . . . . . . . . . . . . . . . . . . .
Finding Elements on the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Clicking the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filling Out and Submitting Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending Special Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Clicking Browser Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Information on Selenium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

227
227
229
231
231
232
232

233
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

234
234
235
236
236
237

237
238
239
240
240
240
241
242
244
245
245
246
248
248
249
249
250
251
251
252
253
254
255
256
256
256
257
259
259
260

261
261

Contents in Detail   xv


Summary . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . .
Command Line Emailer . .
Image Site Downloader .
2048 . . . . . . . . . . . . . .
Link Verification . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

12
Working with Excel Spreadsheets
Excel Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing the openpyxl Module . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Excel Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening Excel Documents with OpenPyXL . . . . . . . . . . . . .
Getting Sheets from the Workbook . . . . . . . . . . . . . . . . . . .
Getting Cells from the Sheets . . . . . . . . . . . . . . . . . . . . . . .
Converting Between Column Letters and Numbers . . . . . . . .
Getting Rows and Columns from the Sheets . . . . . . . . . . . . .
Workbooks, Sheets, Cells . . . . . . . . . . . . . . . . . . . . . . . . .
Project: Reading Data from a Spreadsheet . . . . . . . . . . . . . . . . . . . .
Step 1: Read the Spreadsheet Data . . . . . . . . . . . . . . . . . .
Step 2: Populate the Data Structure . . . . . . . . . . . . . . . . . .
Step 3: Write the Results to a File . . . . . . . . . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . .
Writing Excel Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating and Saving Excel Documents . . . . . . . . . . . . . . . .
Creating and Removing Sheets . . . . . . . . . . . . . . . . . . . . .
Writing Values to Cells . . . . . . . . . . . . . . . . . . . . . . . . . . .

Project: Updating a Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Set Up a Data Structure with the Update Information .
Step 2: Check All Rows and Update Incorrect Prices . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . . . . .
Setting the Font Style of Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Font Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adjusting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Row Height and Column Width . . . . . . . . . . . . . . . .
Merging and Unmerging Cells . . . . . . . . . . . . . . . . . . . . . .
Freeze Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multiplication Table Maker . . . . . . . . . . . . . . . . . . . . . . . .
Blank Row Inserter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Spreadsheet Cell Inverter . . . . . . . . . . . . . . . . . . . . . . . . .
Text Files to Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . .
Spreadsheet to Text Files . . . . . . . . . . . . . . . . . . . . . . . . . .

xvi   Contents in Detail

261
261
262
262
263
263
263


265
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

266
266
266
267
268
268
270
270
272
272

273
274
275
276
277
277
278
278
279
280
281
281
282
282
284
285
285
286
287
288
290
291
291
291
292
292
293
293



13
Working with PDF and Word Documents
PDF Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extracting Text from PDFs . . . . . . . . . . . . . . . . . .
Decrypting PDFs . . . . . . . . . . . . . . . . . . . . . . . . .
Creating PDFs . . . . . . . . . . . . . . . . . . . . . . . . . .
Project: Combining Select Pages from Many PDFs . . . . . . . .
Step 1: Find All PDF Files . . . . . . . . . . . . . . . . . .
Step 2: Open Each PDF . . . . . . . . . . . . . . . . . . .
Step 3: Add Each Page . . . . . . . . . . . . . . . . . . .
Step 4: Save the Results . . . . . . . . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . .
Word Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Word Documents . . . . . . . . . . . . . . . . .
Getting the Full Text from a .docx File . . . . . . . . . .
Styling Paragraph and Run Objects . . . . . . . . . . .
Creating Word Documents with Nondefault Styles .
Run Attributes . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing Word Documents . . . . . . . . . . . . . . . . . .
Adding Headings . . . . . . . . . . . . . . . . . . . . . . . .
Adding Line and Page Breaks . . . . . . . . . . . . . . .
Adding Pictures . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PDF Paranoia . . . . . . . . . . . . . . . . . . . . . . . . . .
Custom Invitations as Word Documents . . . . . . . .
Brute-Force PDF Password Breaker . . . . . . . . . . . .

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

295
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

14
Working with CSV Files and JSON Data
The csv Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reader Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Data from Reader Objects in a for Loop . . . . . .
Writer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The delimiter and lineterminator Keyword Arguments . . .
Project: Removing the Header from CSV Files . . . . . . . . . . . . . . .
Step 1: Loop Through Each CSV File . . . . . . . . . . . . . .
Step 2: Read in the CSV File . . . . . . . . . . . . . . . . . . . .
Step 3: Write Out the CSV File Without the First Row . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . .
JSON and APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The json Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading JSON with the loads() Function . . . . . . . . . . . .
Writing JSON with the dumps() Function . . . . . . . . . . . .
Project: Fetching Current Weather Data . . . . . . . . . . . . . . . . . . .
Step 1: Get Location from the Command Line Argument .
Step 2: Download the JSON Data . . . . . . . . . . . . . . . .
Step 3: Load JSON Data and Print Weather . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

295
296
297
298

303
304
304
305
305
306
306
307
308
309
310
311
312
314
315
315
316
316
317
317
317
318

319
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

320
321
322
322
323
324
325
325
326
327
327
328
328
329
329
330
330
331
332
333


Contents in Detail   xvii


Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Practice Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Excel-to-CSV Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

15
Keeping Time, Scheduling Tasks,
and Launching Programs

335

The time Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The time.time() Function . . . . . . . . . . . . . . . . . . .
The time.sleep() Function . . . . . . . . . . . . . . . . . . .
Rounding Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project: Super Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Set Up the Program to Track Times . . . . . .
Step 2: Track and Print Lap Times . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . .
The datetime Module . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The timedelta Data Type . . . . . . . . . . . . . . . . . . .
Pausing Until a Specific Date . . . . . . . . . . . . . . . .
Converting datetime Objects into Strings . . . . . . . .
Converting Strings into datetime Objects . . . . . . . .
Review of Python’s Time Functions . . . . . . . . . . . . . . . . . . .
Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Arguments to the Thread’s Target Function .
Concurrency Issues . . . . . . . . . . . . . . . . . . . . . . .

Project: Multithreaded XKCD Downloader . . . . . . . . . . . . .
Step 1: Modify the Program to Use a Function . . . .
Step 2: Create and Start Threads . . . . . . . . . . . . .
Step 3: Wait for All Threads to End . . . . . . . . . . .
Launching Other Programs from Python . . . . . . . . . . . . . . .
Passing Command Line Arguments to Popen() . . . .
Task Scheduler, launchd, and cron . . . . . . . . . . . .
Opening Websites with Python . . . . . . . . . . . . . .
Running Other Python Scripts . . . . . . . . . . . . . . . .
Opening Files with Default Applications . . . . . . . .
Project: Simple Countdown Program . . . . . . . . . . . . . . . . .
Step 1: Count Down . . . . . . . . . . . . . . . . . . . . . .
Step 2: Play the Sound File . . . . . . . . . . . . . . . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Prettified Stopwatch . . . . . . . . . . . . . . . . . . . . . .
Scheduled Web Comic Downloader . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

16
Sending Email and Text Messages
SMTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending Email . . . . . . . . . . . . . . . . . . . . . .
Connecting to an SMTP Server . . . . .
Sending the SMTP “Hello” Message .
xviii   Contents in Detail

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

336
336
337
338
338
339
339
340
341
342
344
344
345
346
347
348
349
350
350
351
352
352
354
354

355
355
355
357
357
357
358
358
359
359
360
360

361
.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

362
362
363
364


Starting TLS Encryption . . . . . . . . . . . . . . . . . .
Logging in to the SMTP Server . . . . . . . . . . . . .
Sending an Email . . . . . . . . . . . . . . . . . . . . . .
Disconnecting from the SMTP Server . . . . . . . . .
IMAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Retrieving and Deleting Emails with IMAP . . . . . . . . . . .
Connecting to an IMAP Server . . . . . . . . . . . . .
Logging in to the IMAP Server . . . . . . . . . . . . .
Searching for Email . . . . . . . . . . . . . . . . . . . .
Fetching an Email and Marking It As Read . . . .

Getting Email Addresses from a Raw Message .
Getting the Body from a Raw Message . . . . . . .
Deleting Emails . . . . . . . . . . . . . . . . . . . . . . .
Disconnecting from the IMAP Server . . . . . . . . .
Project: Sending Member Dues Reminder Emails . . . . . . .
Step 1: Open the Excel File . . . . . . . . . . . . . . .
Step 2: Find All Unpaid Members . . . . . . . . . .
Step 3: Send Customized Email Reminders . . . .
Sending Text Messages with Twilio . . . . . . . . . . . . . . . .
Signing Up for a Twilio Account . . . . . . . . . . .
Sending Text Messages . . . . . . . . . . . . . . . . .
Project: “Just Text Me” Module . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Random Chore Assignment Emailer . . . . . . . . .
Umbrella Reminder . . . . . . . . . . . . . . . . . . . . .
Auto Unsubscriber . . . . . . . . . . . . . . . . . . . . .
Controlling Your Computer Through Email . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

17
Manipulating Images
Computer Image Fundamentals . . . . . . . . . . . . . . . . . . . .
Colors and RGBA Values . . . . . . . . . . . . . . . . .
Coordinates and Box Tuples . . . . . . . . . . . . . . .
Manipulating Images with Pillow . . . . . . . . . . . . . . . . . . .
Working with the Image Data Type . . . . . . . . . .
Cropping Images . . . . . . . . . . . . . . . . . . . . . . .
Copying and Pasting Images onto Other Images .
Resizing an Image . . . . . . . . . . . . . . . . . . . . . .

Rotating and Flipping Images . . . . . . . . . . . . . .
Changing Individual Pixels . . . . . . . . . . . . . . . .
Project: Adding a Logo . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Open the Logo Image . . . . . . . . . . . . . .
Step 2: Loop Over All Files and Open Images . . .
Step 3: Resize the Images . . . . . . . . . . . . . . . . .
Step 4: Add the Logo and Save the Changes . . .
Ideas for Similar Programs . . . . . . . . . . . . . . . .
Drawing on Images . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . .
Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . .

364
364
365
366
366
366
367
368
368
372
373
374
375
375
376
376
378
378

380
380
381
383
384
384
385
385
385
385
386

387
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

388
388
389
390
392
393
394

397
398
400
401
401
402
403
404
406
406
406
408

Contents in Detail   xix


Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending and Fixing the Chapter Project Programs .
Identifying Photo Folders on the Hard Drive . . . . . . .
Custom Seating Cards . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

18

Controlling the Keyboard and Mouse
with GUI Automation
Installing the pyautogui Module . . . . . . . . . . . . . . . . . .
Staying on Track . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Shutting Down Everything by Logging Out . . . . .
Pauses and Fail-Safes . . . . . . . . . . . . . . . . . . .
Controlling Mouse Movement . . . . . . . . . . . . . . . . . . . .
Moving the Mouse . . . . . . . . . . . . . . . . . . . . .
Getting the Mouse Position . . . . . . . . . . . . . . .
Project: “Where Is the Mouse Right Now?” . . . . . . . . . .
Step 1: Import the Module . . . . . . . . . . . . . . . .
Step 2: Set Up the Quit Code and Infinite Loop .
Step 3: Get and Print the Mouse Coordinates . .
Controlling Mouse Interaction . . . . . . . . . . . . . . . . . . . .
Clicking the Mouse . . . . . . . . . . . . . . . . . . . . .
Dragging the Mouse . . . . . . . . . . . . . . . . . . . .
Scrolling the Mouse . . . . . . . . . . . . . . . . . . . .
Working with the Screen . . . . . . . . . . . . . . . . . . . . . . .
Getting a Screenshot . . . . . . . . . . . . . . . . . . .
Analyzing the Screenshot . . . . . . . . . . . . . . . .
Project: Extending the mouseNow Program . . . . . . . . . .
Image Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling the Keyboard . . . . . . . . . . . . . . . . . . . . . . .
Sending a String from the Keyboard . . . . . . . . .
Key Names . . . . . . . . . . . . . . . . . . . . . . . . . .
Pressing and Releasing the Keyboard . . . . . . . .
Hotkey Combinations . . . . . . . . . . . . . . . . . . .
Review of the PyAutoGUI Functions . . . . . . . . . . . . . . . .
Project: Automatic Form Filler . . . . . . . . . . . . . . . . . . . .
Step 1: Figure Out the Steps . . . . . . . . . . . . . .

Step 2: Set Up Coordinates . . . . . . . . . . . . . . .
Step 3: Start Typing Data . . . . . . . . . . . . . . . .
Step 4: Handle Select Lists and Radio Buttons . .
Step 5: Submit the Form and Wait . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Practice Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Busy . . . . . . . . . . . . . . . . . . . . . . . . .
Instant Messenger Bot . . . . . . . . . . . . . . . . . . .
Game-Playing Bot Tutorial . . . . . . . . . . . . . . . .

xx   Contents in Detail

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


410
410
411
411
411
412

413
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


414
414
414
415
415
416
417
417
418
418
418
419
420
420
422
423
423
424
424
425
426
426
427
428
429
430
430
432
432
434

435
436
437
438
438
438
438
439


A
Installing Third-Party Modules

441

The pip Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Installing Third-Party Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442

B
Running Programs

443

Shebang Line . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running Python Programs on Windows . . . . . . . . .
Running Python Programs on OS X and Linux . . . . .
Running Python Programs with Assertions Disabled .

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

C
Answers to the Practice Questions
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter

1 . .
2 . .
3 . .
4 . .
5 . .
6 . .

7 . .
8 . .
9 . .
10 .
11 .
12 .
13 .
14 .
15 .
16 .
17 .
18 .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

443
444
445
445

447
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

448
448
450
450
451
451
452
453
453
454
455
456
456

457
457
458
458
458

Index461

Contents in Detail   xxi



Acknowledgments

I couldn’t have written a book like this without the
help of a lot of people. I’d like to thank Bill Pollock;
my editors, Laurel Chun, Leslie Shen, Greg Poulos,
and Jennifer Griffith-Delgado; and the rest of the staff
at No Starch Press for their invaluable help. Thanks to
my tech reviewer, Ari Lacenski, for great suggestions,
edits, and support.
Many thanks to our Benevolent Dictator For Life, Guido van Rossum,
and everyone at the Python Software Foundation for their great work. The
Python community is the best one I’ve found in the tech industry.
Finally, I would like to thank my family, friends, and the gang at
Shotwell’s for not minding the busy life I’ve had while writing this book.
Cheers!




Introduction

“You’ve just done in two hours what it takes
the three of us two days to do.” My college
roommate was working at a retail electronics
store in the early 2000s. Occasionally, the store
would receive a spreadsheet of thousands of product
prices from its competitor. A team of three employees
would print the spreadsheet onto a thick stack of paper and split it among
themselves. For each product price, they would look up their store’s price
and note all the products that their competitors sold for less. It usually took
a couple of days.
“You know, I could write a program to do that if you have the original
file for the printouts,” my roommate told them, when he saw them sitting
on the floor with papers scattered and stacked around them.
After a couple of hours, he had a short program that read a competitor’s price from a file, found the product in the store’s database, and noted
whether the competitor was cheaper. He was still new to programming, and


×