Automate the Boring Stuff with Python
Automate the Boring Stuff with Python
2020 英文版
目录
Table of Contents
- Introduction
- Chapter 1 – Python Basics
- Chapter 2 – Flow Control
- Chapter 3 – Functions
- Chapter 4 – Lists
- Chapter 5 – Dictionaries and Structuring Data
- Chapter 6 – Manipulating Strings
- Chapter 7 – Pattern Matching with Regular Expressions
- Chapter 8 – Input Validation
- Chapter 9 – Reading and Writing Files
- Chapter 10 – Organizing Files
- Chapter 11 – Debugging
- Chapter 12 – Web Scraping
- Chapter 13 – Working with Excel Spreadsheets
- Chapter 14 – Working with Google Spreadsheets
- Chapter 15 – Working with PDF and Word Documents
- Chapter 16 – Working with CSV Files and JSON Data
- Chapter 17 – Keeping Time, Scheduling Tasks, and Launching Programs
- Chapter 18 – Sending Email and Text Messages
- Chapter 19 – Manipulating Images
- Chapter 20 – Controlling the Keyboard and Mouse with GUI Automation
- Appendix A – Installing Third-Party Modules
- Appendix B – Running Programs
- Appendix C – Answers to the Practice Questions
(Link to the older 1st edition.)
Additional Content
- Download files used in the book
- List of CSS Selector Tutorials
- List of JSON APIs
- List of Programming Practice Sites
- List of Web Comics
- Schedulers for Windows, Mac, and Linux
- How to Do PyCon (or any tech conference)
About the Author
Al Sweigart is a software developer and teaches programming to kids and adults. He has written several books for beginners and makes them freely available at InventWithPython.com. His personal website is AlSweigart.com.1
简单例题
Figure 1-2: When a new value is assigned to a variable, the old one is forgotten.
2
Figure 2-1: A flowchart to tell you what to do if it is raining
Figure 2-2: The flowchart for an if statement
Figure 2-3: The flowchart for an else statement
Figure 2-4: The flowchart for an elif statement
Figure 2-5: The flowchart for multiple elif statements in the vampire.py program
Figure 2-6: The flowchart for the vampire2.py program. The X path will logically never happen, because if age were greater than 2000, it would have already been greater than 100.
Figure 2-7: Flowchart for the previous littleKid.py program
Figure 2-8: The flowchart for the if statement code
Figure 2-9: The flowchart for the while statement code
Figure 2-10: A flowchart of the yourName.py program
Figure 2-11: The flowchart for the yourName2.py program with an infinite loop. Note that the X path will logically never happen, because the loop condition is always True.
Figure 2-12: A flowchart for swordfish.py. The X path will logically never happen, because the loop condition is always True.
Figure 2-13: The flowchart for fiveTimes.py
3
Figure 3-1: Your meandering conversation stack
Figure 3-2: The frame objects of the call stack as abcdCallStack.py calls and returns from functions
4
Table 4-1: The Augmented Assignment Operators
Augmented assignment statement | Equivalent assignment statement |
---|---|
spam += 1 | spam = spam + 1 |
spam -= 1 | spam = spam - 1 |
spam *= 1 | spam = spam * 1 |
spam /= 1 | spam = spam / 1 |
spam %= 1 | spam = spam % 1 |
Figure 4-2: When eggs = [4, 5, 6] is executed, the contents of eggs are replaced with a new list value.
Figure 4-3: The del statement and the append() method modify the same list value in place.
Figure 4-4: spam = [0, 1, 2, 3, 4, 5] stores a reference to a list, not the actual list.
Figure 4-5: spam = cheese copies the reference, not the list.
Figure 4-6: cheese[1] = ‘Hello!’ modifies the list that both variables refer to.
Figure 4-7: cheese = copy.copy(spam) creates a second list that can be modified independently of the first.
Figure 4-8: Four steps in a Conway’s Game of Life simulation
eg:
haracter Picture Grid
Say you have a list of lists where each value in the inner lists is a one-character string, like this: grid = [[‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’],[‘.’, ‘O’, ‘O’, ‘.’, ‘.’, ‘.’],
[‘O’, ‘O’, ‘O’, ‘O’, ‘.’, ‘.’],
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’, ‘.’],
[‘.’, ‘O’, ‘O’, ‘O’, ‘O’, ‘O’],
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’, ‘.’],
[‘O’, ‘O’, ‘O’, ‘O’, ‘.’, ‘.’],
[‘.’, ‘O’, ‘O’, ‘.’, ‘.’, ‘.’],
[‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’]]
5
Figure 5-1: The coordinates of a chessboard in algebraic chess notation
Figure 5-2: A chess board modeled by the dictionary ‘1h’: ‘bking’, ‘6c’: ‘wqueen’, ‘2g’: ‘bbishop’, ‘5h’: ‘bqueen’, ‘3e’: ‘wking’}
Figure 5-3: The slots of a tic-tac-toe board with their corresponding keys
Figure 5-4: An empty tic-tac-toe board
Figure 5-5: The first move
theBoard = {‘top-L’: ‘O’, ‘top-M’: ‘O’, ‘top-R’: ‘O’,
‘mid-L’: ‘X’, ‘mid-M’: ‘X’, ‘mid-R’: ‘ ‘,
‘low-L’: ‘ ‘, ‘low-M’: ‘ ‘, ‘low-R’: ‘X’}
Figure 5-6: Player O wins.
6
Table 6-1: Escape Characters
Escape character | Prints as |
---|---|
\’ | Single quote |
\” | Double quote |
\t | Tab |
\n | Newline (line break) |
\ | Backslash |