Introduction |
|
xviii | |
Part 1: Programming fundamentals |
|
|
|
2 | (14) |
|
|
4 | (2) |
|
|
5 | (1) |
|
|
5 | (1) |
|
Build a place to work with Python |
|
|
6 | (4) |
|
|
6 | (1) |
|
|
7 | (3) |
|
|
10 | (4) |
|
|
14 | (2) |
|
|
16 | (28) |
|
|
18 | (4) |
|
Programming and party planning |
|
|
18 | (1) |
|
|
19 | (2) |
|
|
21 | (1) |
|
Computers as data processors |
|
|
22 | (9) |
|
Machines and computers and us |
|
|
22 | (2) |
|
Programs as data processors |
|
|
24 | (1) |
|
Python as a data processor |
|
|
25 | (6) |
|
|
31 | (5) |
|
Work with Python functions |
|
|
36 | (5) |
|
|
36 | (2) |
|
|
38 | (1) |
|
Investigate data storage using bin |
|
|
39 | (2) |
|
|
41 | (3) |
|
3 Python program structure |
|
|
44 | (28) |
|
Write your first Python program |
|
|
46 | (11) |
|
Run Python programs using IDLE |
|
|
46 | (5) |
|
Get program output using the print function |
|
|
51 | (6) |
|
|
57 | (4) |
|
|
57 | (3) |
|
|
60 | (1) |
|
|
61 | (2) |
|
Code samples and comments |
|
|
62 | (1) |
|
Run Python from the desktop |
|
|
63 | (1) |
|
Delay the end of the program |
|
|
64 | (1) |
|
|
64 | (6) |
|
Adding the Pygame library |
|
|
65 | (1) |
|
|
66 | (4) |
|
|
70 | (2) |
|
|
72 | (32) |
|
|
74 | (5) |
|
|
76 | (3) |
|
|
79 | (8) |
|
Marking the start and end of strings |
|
|
81 | (1) |
|
Escape characters in text |
|
|
82 | (2) |
|
Read in text using the input function |
|
|
84 | (3) |
|
|
87 | (14) |
|
Convert strings into integer values |
|
|
87 | (2) |
|
Whole numbers and real numbers |
|
|
89 | (1) |
|
Real numbers and floating-point numbers |
|
|
90 | (5) |
|
Convert strings into floating-point values |
|
|
95 | (1) |
|
|
96 | (2) |
|
Convert between float and int |
|
|
98 | (3) |
|
|
101 | (1) |
|
|
102 | (2) |
|
5 Making decisions in programs |
|
|
104 | (36) |
|
|
106 | (12) |
|
|
106 | (3) |
|
|
109 | (2) |
|
|
111 | (3) |
|
|
114 | (4) |
|
|
118 | (11) |
|
|
127 | (1) |
|
|
128 | (1) |
|
Use decisions to make an application |
|
|
129 | (5) |
|
Design the user interface |
|
|
129 | (1) |
|
Implement a user interface |
|
|
130 | (2) |
|
|
132 | (1) |
|
|
133 | (1) |
|
|
134 | (4) |
|
|
138 | (2) |
|
6 Repeating actions with loops |
|
|
140 | (30) |
|
|
142 | (20) |
|
Repeat a sequence of statements using while |
|
|
142 | (5) |
|
Handling invalid user entry |
|
|
147 | (5) |
|
Detect invalid number entry using exceptions |
|
|
152 | (2) |
|
Exceptions and number reading |
|
|
154 | (2) |
|
Handling multiple exceptions |
|
|
156 | (1) |
|
|
157 | (1) |
|
Return to the top of a loop with continue |
|
|
158 | (1) |
|
|
159 | (3) |
|
The for loop construction |
|
|
162 | (5) |
|
Make a digital clock using snaps |
|
|
167 | (1) |
|
|
168 | (2) |
|
7 Using functions to simplify programs |
|
|
170 | (40) |
|
|
172 | (21) |
|
Give information to functions using parameters |
|
|
176 | (9) |
|
Return values from function calls |
|
|
185 | (8) |
|
|
193 | (15) |
|
Create a text input function |
|
|
193 | (2) |
|
Add help information to functions |
|
|
195 | (2) |
|
Create a number input function |
|
|
197 | (4) |
|
Convert our functions into a Python module |
|
|
201 | (1) |
|
|
202 | (6) |
|
|
208 | (2) |
|
8 Storing collections of data |
|
|
210 | (54) |
|
|
212 | (9) |
|
Limitations of individual variables |
|
|
214 | (1) |
|
|
215 | (3) |
|
|
218 | (1) |
|
Display a list using a for loop |
|
|
219 | (2) |
|
Refactor programs into functions |
|
|
221 | (6) |
|
Create placeholder functions |
|
|
224 | (1) |
|
|
225 | (2) |
|
|
227 | (11) |
|
Initialize a list with test data |
|
|
228 | (1) |
|
Sort a list from high to low |
|
|
228 | (6) |
|
Sort a list from low to high |
|
|
234 | (1) |
|
Find the highest and lowest sales values |
|
|
235 | (1) |
|
Evaluate total and average sales |
|
|
236 | (1) |
|
|
237 | (1) |
|
|
238 | (13) |
|
|
239 | (3) |
|
|
242 | (2) |
|
|
244 | (2) |
|
|
246 | (1) |
|
|
247 | (4) |
|
|
251 | (4) |
|
Use loops to work with tables |
|
|
253 | (2) |
|
Use lists as lookup tables |
|
|
255 | (1) |
|
|
256 | (3) |
|
|
259 | (5) |
Part 2: Advanced programming |
|
|
9 Use classes to store data |
|
|
264 | (44) |
|
|
266 | (34) |
|
|
267 | (2) |
|
Store contact details in separate lists |
|
|
269 | (3) |
|
Use a class to store contact details |
|
|
272 | (3) |
|
Use the Contact class in the Tiny Contacts program |
|
|
275 | (3) |
|
|
278 | (11) |
|
Save contacts in a file using pickle |
|
|
289 | (3) |
|
Load contacts from a file using pickle |
|
|
292 | (1) |
|
Add save and load to Tiny Contacts |
|
|
293 | (1) |
|
|
294 | (6) |
|
|
300 | (5) |
|
|
300 | (2) |
|
|
302 | (1) |
|
Return a dictionary from a function |
|
|
303 | (1) |
|
Use a dictionary to store contacts |
|
|
303 | (2) |
|
|
305 | (3) |
|
10 Use classes to create active objects |
|
|
308 | (64) |
|
|
310 | (22) |
|
Add a data attribute to a class |
|
|
311 | (1) |
|
|
312 | (2) |
|
Create method attributes for a class |
|
|
314 | (2) |
|
Add validation to methods |
|
|
316 | (12) |
|
Protect a data attribute against damage |
|
|
328 | (3) |
|
|
331 | (1) |
|
|
332 | (5) |
|
|
337 | (9) |
|
|
340 | (6) |
|
The _str_ method in a class |
|
|
346 | (4) |
|
|
348 | (2) |
|
Session tracking in Time Tracker |
|
|
350 | (13) |
|
|
355 | (6) |
|
|
361 | (2) |
|
|
363 | (5) |
|
|
368 | (4) |
|
11 Object-based solution design |
|
|
372 | (66) |
|
|
374 | (36) |
|
|
376 | (1) |
|
|
376 | (3) |
|
Creating superclasses and subclasses |
|
|
379 | (17) |
|
|
396 | (9) |
|
Implement application behaviors |
|
|
405 | (4) |
|
|
409 | (1) |
|
Create a FashionShop component |
|
|
410 | (11) |
|
Create a user interface component |
|
|
417 | (4) |
|
|
421 | (1) |
|
|
422 | (12) |
|
|
426 | (5) |
|
Sets versus class hierarchies |
|
|
431 | (3) |
|
|
434 | (4) |
|
|
438 | (48) |
|
|
440 | (20) |
|
|
440 | (6) |
|
|
446 | (5) |
|
Iterator functions and the yield statement |
|
|
451 | (6) |
|
Functions with an arbitrary number of arguments |
|
|
457 | (3) |
|
|
460 | (10) |
|
|
460 | (1) |
|
Add a readme function to BTCInput |
|
|
461 | (1) |
|
Run a module as a program |
|
|
462 | (1) |
|
Detect whether a module is executed as a program |
|
|
463 | (1) |
|
|
464 | (2) |
|
Import modules from packages |
|
|
466 | (4) |
|
|
470 | (8) |
|
The Python assert statement |
|
|
471 | (1) |
|
The Python unittest module |
|
|
472 | (4) |
|
|
476 | (2) |
|
View program documentation |
|
|
478 | (5) |
|
|
483 | (3) |
|
|
486 | (2) |
Part 3: Useful Python (Digital-only) |
|
|
The chapter PDF files for this Part are available at https://aka.ms/BeginCodePython/downloads. |
|
|
|
13 Python and Graphical User Interfaces |
|
|
488 | (60) |
|
|
490 | (9) |
|
Install Visual Studio Code |
|
|
490 | (1) |
|
Install the Python Extension in Visual Studio Code |
|
|
491 | (1) |
|
|
492 | (1) |
|
|
493 | (1) |
|
|
494 | (5) |
|
|
499 | (1) |
|
Create a Graphical User Interface with Tkinter |
|
|
499 | (47) |
|
Create a graphical application |
|
|
506 | (1) |
|
|
507 | (3) |
|
Create an event handler function |
|
|
510 | (1) |
|
|
511 | (1) |
|
Handle errors in a graphical user interface |
|
|
512 | (2) |
|
|
514 | (4) |
|
|
518 | (4) |
|
|
522 | (1) |
|
|
523 | (3) |
|
|
526 | (2) |
|
Group display elements in frames |
|
|
528 | (1) |
|
Create an editable StockItem using a GUI |
|
|
529 | (8) |
|
Create a Listbox selector |
|
|
537 | (7) |
|
An application with a graphical user interface |
|
|
544 | (2) |
|
|
546 | (2) |
|
14 Python programs as network clients |
|
|
548 | (22) |
|
|
550 | (12) |
|
Consume the web from Python |
|
|
562 | (5) |
|
|
562 | (1) |
|
|
562 | (5) |
|
|
567 | (3) |
|
15 Python programs as network servers |
|
|
570 | (22) |
|
Create a web server in Python |
|
|
572 | (18) |
|
A tiny socket-based server |
|
|
572 | (5) |
|
|
577 | (2) |
|
Serve webpages from files |
|
|
579 | (5) |
|
Get information from web users |
|
|
584 | (6) |
|
Host Python applications on the web |
|
|
590 | (1) |
|
|
590 | (2) |
|
16 Create games with Pygame |
|
|
592 | |
|
Getting started with pygame |
|
|
594 | (7) |
|
|
601 | (5) |
|
|
601 | (1) |
|
Load an image into a game |
|
|
602 | (2) |
|
|
604 | (2) |
|
Get user input from pygame |
|
|
606 | (3) |
|
|
609 | (20) |
|
|
614 | (3) |
|
Control the player sprite |
|
|
617 | (1) |
|
|
618 | (1) |
|
Add lots of sprite instances |
|
|
619 | (1) |
|
|
620 | (5) |
|
|
625 | (4) |
|
|
629 | (7) |
|
|
629 | (5) |
|
|
634 | (1) |
|
|
635 | (1) |
|
|
636 | |