Automate the Boring Stuff with Python

Automate the Boring Stuff with Python

2020 英文版

目录

Table of Contents

(Link to the older 1st edition.)

Additional Content

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

书1在线web - 图1简单例题

书1在线web - 图2

Figure 1-2: When a new value is assigned to a variable, the old one is forgotten.

书1在线web - 图3

2

书1在线web - 图4

Figure 2-1: A flowchart to tell you what to do if it is raining

书1在线web - 图5

书1在线web - 图6

Figure 2-2: The flowchart for an if statement

书1在线web - 图7

Figure 2-3: The flowchart for an else statement

书1在线web - 图8

Figure 2-4: The flowchart for an elif statement

书1在线web - 图9

Figure 2-5: The flowchart for multiple elif statements in the vampire.py program

书1在线web - 图10

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.

书1在线web - 图11

Figure 2-7: Flowchart for the previous littleKid.py program

书1在线web - 图12

Figure 2-8: The flowchart for the if statement code

书1在线web - 图13

Figure 2-9: The flowchart for the while statement code

书1在线web - 图14

Figure 2-10: A flowchart of the yourName.py program

书1在线web - 图15

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.

书1在线web - 图16

Figure 2-12: A flowchart for swordfish.py. The X path will logically never happen, because the loop condition is always True.

书1在线web - 图17

Figure 2-13: The flowchart for fiveTimes.py

3

书1在线web - 图18

Figure 3-1: Your meandering conversation stack

书1在线web - 图19

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

书1在线web - 图20

Figure 4-2: When eggs = [4, 5, 6] is executed, the contents of eggs are replaced with a new list value.

书1在线web - 图21

Figure 4-3: The del statement and the append() method modify the same list value in place.

书1在线web - 图22

Figure 4-4: spam = [0, 1, 2, 3, 4, 5] stores a reference to a list, not the actual list.

书1在线web - 图23

Figure 4-5: spam = cheese copies the reference, not the list.

书1在线web - 图24

Figure 4-6: cheese[1] = ‘Hello!’ modifies the list that both variables refer to.

书1在线web - 图25

Figure 4-7: cheese = copy.copy(spam) creates a second list that can be modified independently of the first.

书1在线web - 图26

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

书1在线web - 图27

Figure 5-1: The coordinates of a chessboard in algebraic chess notation

书1在线web - 图28

Figure 5-2: A chess board modeled by the dictionary ‘1h’: ‘bking’, ‘6c’: ‘wqueen’, ‘2g’: ‘bbishop’, ‘5h’: ‘bqueen’, ‘3e’: ‘wking’}

书1在线web - 图29

Figure 5-3: The slots of a tic-tac-toe board with their corresponding keys

书1在线web - 图30

Figure 5-4: An empty tic-tac-toe board

书1在线web - 图31

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’}

书1在线web - 图32

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

end