Automate the Boring Stuff with Python
Let the computer do the dull work
A short program can do in seconds the repetitive computer jobs that eat your hours, and you do not have to be a software engineer to write one.
Al Sweigart is a software developer. He wrote this book for office workers, administrators, academics and anyone else who uses a computer. It teaches Python, a programming language for beginners, then walks through short projects such as renaming files, reading spreadsheets and sending texts. You will take away what automation is for, the few ideas every program uses, and how to get started.
What you'll learn
Then a short quiz, and the key takeaways on one page.
Boring work suits a program
Many computer jobs are boring because they repeat, and a computer can do anything that repeats in seconds if you give it the right instructions.
Sweigart opens with his college roommate, who worked in a store that sold electronics, back in the early 2000s. Now and then the store got a big spreadsheet listing thousands of product prices from other stores. Three employees printed it and split the pages. They looked up their own price for each product and noted every one a rival sold for less. The whole job took a couple of days.
The roommate said he could write a program to do it. After a couple of hours he had one, and it took only a few seconds to run. The three employees told him he had done in two hours what took them two days. So a short program beat three people.
The same price check, two ways
That is the power of programming. Many people spend hours clicking and typing, unaware that the machine in front of them could do the job in seconds. The book is not for people who want to be professional developers: a few guitar lessons will not make you a rock star either. It is for everyone with repetitive work, such as moving files, filling in forms or sending prewritten replies:
Two worries stop many people before they start. The book answers both.
Try this. Open Python's interactive shell, a window where you type one instruction and see the result at once. Type print('Hello, world!') and press Enter. The book says you remember what you do much better than what you only read.
Four building blocks
Every program is built from the same few instructions: do things in order, choose, repeat a set number of times, and repeat until something is true.
Sweigart says programming is simply entering instructions for the computer to perform. Every program combines a few building blocks.
The four building blocks
Two small ideas make the blocks work. A condition is a question whose answer is True or False, such as age > 100. Here age is a variable, a box in the computer's memory that holds one value. A block is a group of lines indented the same amount, and Python runs them together under an if or a loop.
Here is a cut-down version of the book's vampire example. The vampire is 3000 years old.
age = 3000
if age < 12:
print('kiddo')
elif age > 100:
print('grannie')
elif age > 2000:
print('vampire')
The function input() waits for the user to type and always gives back text, so int() turns it into a number. Two pieces of text with + between them are joined, not added. The other two blocks repeat work. A for loop runs a set number of times. range(1, 7) counts from 1 to 6, because it stops just before the last number. A while loop keeps going as long as its condition is True, and break jumps out of it.
The book's guess-the-number game uses steps, choices and a loop. The player gets six guesses at a number between 1 and 20. This cut-down loop gives the hints:
for guessesTaken in range(1, 7):
guess = int(input())
if guess < secretNumber:
print('Too low.')
elif guess > secretNumber:
print('Too high.')
else:
break
Try this. Type 2 + 3 * 6 and then (2 + 3) * 6 into the interactive shell. You get 20 and 30. Multiplication goes first unless parentheses say otherwise.
Write it once, reuse it
Give a chunk of code a name and call it whenever you need it. Better still, call code that other people already wrote.
A function is a mini-program inside your program. The book starts with a function that prints three greeting lines. You write the lines once under a name, then call the name as often as you like. Its next example takes a name:
def hello(name):
print('Hello, ' + name)
hello('Alice')
hello('Bob')
The program prints Hello, Alice and then Hello, Bob. Copying code is a trap: when you find a bug, you must fix every copy. A function gives you one place to fix. Its variables disappear when it ends, so one function cannot spoil another's values.
A function is also a black box. You only need to know what goes in and what comes out. That leads to a big shortcut: you do not have to write everything yourself.
Your program calls other people's modules
Python comes with a library of ready-made modules, such as random. Other programmers have written many more. A tool called pip fetches them from PyPI, which works like a free app store for Python modules. For example, pip install --user requests adds the module that downloads web pages. Modules are how a beginner can update spreadsheets and send texts.
One trap: never name your own file after a module. If you save a program as random.py, then import random loads your file instead of Python's module. You get errors such as a missing randint. The same happens with any module name, such as requests.
Try this. Search online for the documentation of len(), on a page called Built-in Functions. Skim what else Python gives you for free.
5 more lessons in the app
Keep going with Automate the Boring Stuff with Python
The rest of this summary is in Bookmuse for iPhone, iPad and Android: one full summary is free every day, and Pro opens them all.
- 4Real things as lists and dictionariesHow one variable can hold a whole game board
- 5Find text by its patternRegular expressions, and a plan before the code
- 6Files by the thousandOne loop over a folder, and a dry run first
- 7Pick the most direct toolThe web, other apps, and clicking when nothing else works
- 8Bugs are normalCatch them early, read the clues, ask well
- ?QuizCheck you got the big ideas, with the answers explained.
- ✓Key takeawaysEverything worth keeping, on one screen.

Surrounded by IdiotsThomas Erikson
Atlas ShruggedAyn Rand
The 10X RuleGrant Cardone
The Great CEO WithinMatt Mochary
Crucial ConversationsJoseph Grenny, Kerry Patterson, Ron McMillan, Al Switzler, Emily Gregory
Why Nations FailDaron Acemoglu and James A. Robinson