Automate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners [2 ed.] 1593279922, 9781593279929

The second edition of this best-selling Python book (100,000+ copies sold in print alone) uses Python 3 to teach even th

25,319 3,725 13MB

English Pages 592 [568] Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

Automate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners [2 ed.]
 1593279922, 9781593279929

Table of contents :
Title Page
Copyright Page
Dedication
About the Author
About the Technical Reviewer
BRIEF CONTENTS
CONTENTS IN DETAIL
ACKNOWLEDGMENTS
INTRODUCTION
Whom Is This Book For?
Conventions
What Is Programming?
About This Book
Downloading and Installing Python
Downloading and Installing Mu
Starting Mu
Starting IDLE
The Interactive Shell
Installing Third-Party Modules
How to Find Help
Asking Smart Programming Questions
Summary
PART I: PYTHON PROGRAMMING BASICS
1 PYTHON BASICS
Entering Expressions into the Interactive Shell
The Integer, Floating-Point, and String Data Types
String Concatenation and Replication
Storing Values in Variables
Your First Program
Dissecting Your Program
Summary
Practice Questions
2 FLOW CONTROL
Boolean Values
Comparison Operators
Boolean Operators
Mixing Boolean and Comparison Operators
Elements of Flow Control
Program Execution
Flow Control Statements
Importing Modules
Ending a Program Early with the sys.exit() Function
A Short Program: Guess the Number
A Short Program: Rock, Paper, Scissors
Summary
Practice Questions
3 FUNCTIONS
def Statements with Parameters
Return Values and return Statements
The None Value
Keyword Arguments and the print() Function
The Call Stack
Local and Global Scope
The global Statement
Exception Handling
A Short Program: Zigzag
Summary
Practice Questions
Practice Projects
4 LISTS
The List Data Type
Working with Lists
Augmented Assignment Operators
Methods
Example Program: Magic 8 Ball with a List
Sequence Data Types
References
A Short Program: Conway’s Game of Life
Summary
Practice Questions
Practice Projects
5 DICTIONARIES AND STRUCTURING DATA
The Dictionary Data Type
Pretty Printing
Using Data Structures to Model Real-World Things
Summary
Practice Questions
Practice Projects
6 MANIPULATING STRINGS
Working with Strings
Putting Strings Inside Other Strings
Useful String Methods
Numeric Values of Characters with the ord() and chr() Functions
Copying and Pasting Strings with the pyperclip Module
Project: Multi-Clipboard Automatic Messages
Project: Adding Bullets to Wiki Markup
A Short Progam: Pig Latin
Summary
Practice Questions
Practice Projects
PART II: AUTOMATING TASKS
7 PATTERN MATCHING WITH REGULAR EXPRESSIONS
Finding Patterns of Text Without Regular Expressions
Finding Patterns of Text with Regular Expressions
More Pattern Matching with Regular Expressions
Greedy and Non-greedy Matching
The findall() Method
Character Classes
Making Your Own Character Classes
The Caret and Dollar Sign Characters
The Wildcard Character
Review of Regex Symbols
Case-Insensitive Matching
Substituting Strings with the sub() Method
Managing Complex Regexes
Combining re.IGNORECASE, re.DOTALL, and re.VERBOSE
Project: Phone Number and Email Address Extractor
Summary
Practice Questions
Practice Projects
8 INPUT VALIDATION
The PyInputPlus Module
Project: How to Keep an Idiot Busy for Hours
Project: Multiplication Quiz
Summary
Practice Questions
Practice Projects
9 READING AND WRITING FILES
Files and File Paths
The File Reading/Writing Process
Saving Variables with the shelve Module
Saving Variables with the pprint.pformat() Function
Project: Generating Random Quiz Files
Project: Updatable Multi-Clipboard
Summary
Practice Questions
Practice Projects
10 ORGANIZING FILES
The shutil Module
Walking a Directory Tree
Compressing Files with the zipfile Module
Project: Renaming Files with American-Style Dates to European-Style Dates
Project: Backing Up a Folder into a ZIP File
Summary
Practice Questions
Practice Projects
11 DEBUGGING
Raising Exceptions
Getting the Traceback as a String
Assertions
Logging
Mu’s Debugger
Summary
Practice Questions
Practice Project
12 WEB SCRAPING
Project: mapIt.py with the webbrowser Module
Downloading Files from the Web with the requests Module
Saving Downloaded Files to the Hard Drive
HTML
Parsing HTML with the bs4 Module
Project: Opening All Search Results
Project: Downloading All XKCD Comics
Controlling the Browser with the selenium Module
Summary
Practice Questions
Practice Projects
13 WORKING WITH EXCEL SPREADSHEETS
Excel Documents
Installing the openpyxl Module
Reading Excel Documents
Project: Reading Data from a Spreadsheet
Writing Excel Documents
Project: Updating a Spreadsheet
Setting the Font Style of Cells
Font Objects
Formulas
Adjusting Rows and Columns
Charts
Summary
Practice Questions
Practice Projects
14 WORKING WITH GOOGLE SHEETS
Installing and Setting Up EZSheets
Spreadsheet Objects
Sheet Objects
Working with Google Sheets Quotas
Summary
Practice Questions
Practice Projects
15 WORKING WITH PDF AND WORD DOCUMENTS
PDF Documents
Project: Combining Select Pages from Many PDFs
Word Documents
Creating PDFs from Word Documents
Summary
Practice Questions
Practice Projects
16 WORKING WITH CSV FILES AND JSON DATA
The csv Module
Project: Removing the Header from CSV Files
JSON and APIs
The json Module
Project: Fetching Current Weather Data
Summary
Practice Questions
Practice Project
17 KEEPING TIME, SCHEDULING TASKS, AND LAUNCHING PROGRAMS
The time Module
Rounding Numbers
Project: Super Stopwatch
The datetime Module
Review of Python’s Time Functions
Multithreading
Project: Multithreaded XKCD Downloader
Launching Other Programs from Python
Project: Simple Countdown Program
Summary
Practice Questions
Practice Projects
18 SENDING EMAIL AND TEXT MESSAGES
Sending and Receiving Email with the Gmail API
SMTP
Sending Email
IMAP
Retrieving and Deleting Emails with IMAP
Project: Sending Member Dues Reminder Emails
Sending Text Messages with SMS Email Gateways
Sending Text Messages with Twilio
Project: “Just Text Me” Module
Summary
Practice Questions
Practice Projects
19 MANIPULATING IMAGES
Computer Image Fundamentals
Manipulating Images with Pillow
Project: Adding a Logo
Drawing on Images
Summary
Practice Questions
Practice Projects
20 CONTROLLING THE KEYBOARD AND MOUSE WITH GUI AUTOMATION
Installing the pyautogui Module
Setting Up Accessibility Apps on macOS
Staying on Track
Controlling Mouse Movement
Controlling Mouse Interaction
Planning Your Mouse Movements
Working with the Screen
Image Recognition
Getting Window Information
Controlling the Keyboard
Setting Up Your GUI Automation Scripts
Review of the PyAutoGUI Functions
Project: Automatic Form Filler
Displaying Message Boxes
Summary
Practice Questions
Practice Projects
A INSTALLING THIRD-PARTY MODULES
The pip Tool
Installing Third-Party Modules
Installing Modules for the Mu Editor
B RUNNING PROGRAMS
Running Programs from the Terminal Window
Running Python Programs on Windows
Running Python Programs on macOS
Running Python Programs on Ubuntu Linux
Running Python Programs with Assertions Disabled
C ANSWERS TO THE PRACTICE QUESTIONS
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
INDEX

Polecaj historie