I couldnt have written a book like this without the help of a lot of people. Id 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 Ive found in the tech industry.
Finally, I would like to thank my family, friends, and the gang at Shotwells for not minding the busy life Ive had while writing this book. Cheers!
Introduction
Youve 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 stores 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 competitors price from a file, found the product in the stores database, and noted whether the competitor was cheaper. He was still new to programming, and he spent most of his time looking up documentation in a programming book. The actual program took only a few seconds to run. My roommate and his co-workers took an extra-long lunch that day.
This is the power of computer programming. A computer is like a Swiss Army knife that you can configure for countless tasks. Many people spend hours clicking and typing to perform repetitive tasks, unaware that the machine theyre using could do their job in seconds if they gave it the right instructions.
Whom Is This Book For?
Software is at the core of so many of the tools we use today: Nearly everyone uses social networks to communicate, many people have Internet-connected computers in their phones, and most office jobs involve interacting with a computer to get work done. As a result, the demand for people who can code has skyrocketed. Countless books, interactive web tutorials, and developer boot camps promise to turn ambitious beginners into software engineers with six-figure salaries.
This book is not for those people. Its for everyone else.
On its own, this book wont turn you into a professional software developer any more than a few guitar lessons will turn you into a rock star. But if youre an office worker, administrator, academic, or anyone else who uses a computer for work or fun, you will learn the basics of programming so that you can automate simple tasks such as the following:
Moving and renaming thousands of files and sorting them into folders
Filling out online forms, no typing required
Downloading files or copy text from a website whenever it updates
Having your computer text you custom notifications
Updating or formatting Excel spreadsheets
Checking your email and sending out prewritten responses
These tasks are simple but time-consuming for humans, and theyre often so trivial or specific that theres no ready-made software to perform them. Armed with a little bit of programming knowledge, you can have your computer do these tasks for you.
Conventions
This book is not designed as a reference manual; its a guide for beginners. The coding style sometimes goes against best practices (for example, some programs use global variables), but thats a trade-off to make the code simpler to learn. This book is made for people to write throwaway code, so theres not much time spent on style and elegance. Sophisticated programming conceptslike object-oriented programming, list comprehensions, and generatorsarent covered because of the complexity they add. Veteran programmers may point out ways the code in this book could be changed to improve efficiency, but this book is mostly concerned with getting programs to work with the least amount of effort.
What Is Programming?
Television shows and films often show programmers furiously typing cryptic streams of 1s and 0s on glowing screens, but modern programming isnt that mysterious. Programming is simply the act of entering instructions for the computer to perform. These instructions might crunch some numbers, modify text, look up information in files, or communicate with other computers over the Internet.
All programs use basic instructions as building blocks. Here are a few of the most common ones, in English:
Do this; then do that.
If this condition is true, perform this action; otherwise, do that action.
Do this action that number of times.
Keep doing that until this condition is true.
You can combine these building blocks to implement more intricate decisions, too. For example, here are the programming instructions, called the source code , for a simple program written in the Python programming language. Starting at the top, the Python software runs each line of code (some lines are run only if a certain condition is true or else Python runs some other line) until it reaches the bottom.
passwordFile = open('SecretPasswordFile.txt') secretPassword = passwordFile.read() print('Enter your password.') typedPassword = input() if typedPassword == secretPassword: print('Access granted') if typedPassword == '12345': print('That password is one that an idiot puts on their luggage.') else: print('Access denied')
You might not know anything about programming, but you could probably make a reasonable guess at what the previous code does just by reading it. First, the file SecretPasswordFile.txt is opened , and the secret password in it is read . Then, the user is prompted to input a password (from the keyboard) . These two passwords are compared , and if theyre the same, the program prints Access granted to the screen . Next, the program checks to see whether the password is 12345 and hints that this choice might not be the best for a password . If the passwords are not the same, the program prints Access denied to the screen .
What Is Python?
Python refers to the Python programming language (with syntax rules for writing what is considered valid Python code) and the Python interpreter software that reads source code (written in the Python language) and performs its instructions. The Python interpreter is free to download from