Table of Contents
Section 1 Essential concepts and skills
Chapter 1 An introduction to Python programming
Introduction to Python 4
Why Python works so well as your first programming language 4
Three types of Python applications 6
The source code for a console application 8
How Python compiles and runs source code 10
How disk storage and main memory work together 12
How to use IDLE to develop programs 14
How to use the interactive shell 14
How to work with source riles 16
How to compile and run a program 18
How to fix syntax and runtime errors 20
Chapter 2 How to write your first programs
Basic coding skills 28
How to code statements 28
How to code comments 30
How to use functions 32
How to work with data types and variables 34
How to assign values to variables 34
How to name variables 36
How to work with numeric data 38
How to code arithmetic expressions 38
How to use arithmetic expressions in assignment statements 40
How to use the interactive shell for testing numeric operations 42
How to work with string data 44
How to assign strings to variables 44
How to join strings 44
How to include special characters in strings 46
How to use the interactive shell for testing string operations 48
How to use five of the Python functions 50
How to use the print() function 50
How to use the input () function 52
How to use the int(), float(), and round() functions 54
How to chain functions 54
Two illustrative programs 56
The Miles Per Gallon program 56
The Test Scores program 58
Chapter 3 How to code control statements
How to code Boolean expressions 66
How to use the relational operators 66
How to use the logical operators 68
How to compare strings 70
How to code the selection structure 72
How to code if statements 72
More examples of if statements 74
How to code nested if statements 76
How to use pseudocode to plan if statements 78
Two illustrative programs 80
The Miles Per Gallon program 80
The Invoice program 82
How to use the iteration structure 84
How to code while statements 84
How to code for statements 86
How to code break and continue statements 88
More examples of loops 90
How to use assignment expressions 92
How to use pseudocode to plan a program 94
Two illustrative programs 96
The Test Scores program 96
The Future Value program 98
Chapter 4 How to define and use functions and modules
How to define and use functions 106
How to define and call a function 106
How to define and call a main() function 108
The Future Value program with functions 110
More skills for defining and using functions 112
How to use default values for arguments 112
How to use named arguments 112
When and how to use local and global variables 114
How to create and use modules 116
How to create a module 116
How to document a module 118
How to import a module 120
The Convert Temperatures program 122
How to use standard modules 124
How to use the random module 124
The Guess the Number game 126
How to plan the functions of a program 128
How to use a hierarchy chart 128
The hierarchy chart for the Pig Dice game 130
The Pig Dice game 132
Chapter 5 How to test and debug a program
An introduction to testing and debugging 142
The three types of errors that can occur 142
Common Python errors 144
Four techniques for testing and debugging 146
How to plan the test runs 146
A simple way to trace code execution 148
How to use top-down coding and testing to simplify debugging 150
How to use the IDLE shell to test functions 152
How to use the IDLE debugger 154
How to set and remove breakpoints 154
How to step through the code 156
How to view the stack 158
Chapter 6 How to Work with lists and tuples
Basic skills for working with lists 164
How to create a list 164
How to get and set items 164
How to add and remove items 166
How to process the items in a list 168
More skills for processing the items in a list 170
How lists are passed to functions 172
The Movie List program 174
How to work with a list of lists 178
How to create a list of lists 178
How to process the items in a list of lists 178
The Movie List 2D program 180
More skills for working with lists 184
How to count, reverse, and sort the items in a list 184
How to get minimum, maximum, sum, and random values from a list 186
How to copy, slice, and concatenate lists 188
How to map, filter, and reduce the items in a list 190
How to work with list comprehensions 192
How to work with tuples 194
How to create a tuple 194
How to get items from a tuple 194
The Number Crunching program 196
Chapter 7 How to work with file I/O
An introduction to file I/O 202
How file I/O works 202
How to open and close a file 204
How to use text files 206
How to write a text file 206
How to read a text file 208
How to work with a list in a text file 210
The Movie List 1.0 program 212
How to use CSV files 216
How to write a CSV file 216
How to read a CSV file 218
How to modify the CSV format 218
The Movie List 2.0 program 220
How to use binary files 224
How to work with a binary file 224
The Movie List 3.0 program 226
How to handle a single exception 232
Chapter 8 How to handle exceptions
How to handle a single exception 232
How exceptions work 232
How to use a try statement to handle one type of exception 234
The Total Calculator program 236
How to handle multiple exceptions 238
How to use a try statement to handle multiple exceptions 238
How to get the information from an exception object 240
The Movie List 2.0 program 242
Two more skills 246
How to use a finally clause 246
How to raise an exception 248
Section 2 Other concepts and skills
Chapter 9 How to work with numbers
Basic skills for working with numbers 256
How floating-point numbers work 256
How to use the math module 258
How to format numbers 260
How to use format specifications with f-strings 260
How to use the locale module 262
How to fix rounding errors 264
How to work with decimal numbers 266
How to use the decimal module 266
The Invoice program with decimal numbers 268
The Future Value program with decimal numbers 270
Chapter 10 How to work with strings
Basic skills for working with strings 276
Unicode, indexes, slicing, duplicating, and multiline strings 276
How to search a string 278
How to loop through the characters in a string 278
How to use basic string methods 280
How to find, remove, and replace parts of a string 282
The Create Account program 284
How to split and join strings 286
How to split a string into a list of strings 286
How to join strings 288
The Movie List 2.0 program 290
The Word Counter program 292
The Hangman game 294
The user interface 294
The hierarchy chart 294
The wordlist module 296
The hangman module 296
Chapter 11 How to work with dates and times
How to get started with dates and times 304
How to create date, time, and datetime objects 304
How to create datetime objects by parsing strings 306
How to format dates and times 308
How to work with spans of time 310
The Invoice Due Date program 312
The Timer program 314
More skills for working with dates and times 316
How to get date and time pans 316
How to compare date/time objects 318
The Hotel Reservation program 320
Chapter 12 How to work with dictionaries
How to get started with dictionaries 328
How to create a dictionary 328
How to get, set, and add items 330
How to delete items 332
How to loop through keys and values 334
How to convert between dictionaries and lists 336
The Country Code program 338
The Word Counter program 342
More skills for working with dictionaries 344
How to use the merge and update operators 344
How to use dictionaries with complex objects as values 346
The Book Catalog program 348
Chapter 13 How to work with recursion and algorithms
An introduction to recursion 356
How recursion works in Python 356
How to use recursion to add a range of numbers 358
Some common recursive algorithms 360
How to compute the factorial of a number 360
How to compute a Fibonacci series 362
An algorithm for solving the Towers of Hanoi puzzle 364
The code for solving the Towers of Hanoi puzzle 366
Section 3 Object-oriented programming
Chapter 14 How to define and use your own classes
An introduction to classes and objects 374
Two UML diagrams for the Product class 374
Code that defines a Product class 376
Code that uses a Product class 376
How to create and use objects 378
How to define a class 380
How to code a constructor and attributes 380
How to code methods 382
The Product Viewer 1.0 program 384
How work with object composition 388
How object composition works 388
The Die and Dice classes 388
The Dice Roller 1.0 program 390
How to work with encapsulation 392
How object encapsulation works 392
How to hide attributes 394
How to access hidden attributes with methods 396
How to access hidden attributes with properties 398
The Die and Dice classes with encapsulation 400
The Product class with some encapsulation 402
The Pig Dice game 404
The console 404
The code 404
Chapter 15 How to work with inheritance
How to work with inheritance 414
How inheritance works 414
How to define a subclass 416
How polymorphism works 418
How to check an object's type 420
The Product Viewer 2.0 program 422
The objects module 422
The user interface and product viewer module 424
How to override object methods 426
How to define a string representation for an object 426
How to define an iterator for an object 428
The Die and Dice classes 430
Two more skills for the road 432
How to work with custom exceptions 432
When to use inheritance 434
Chapter 16 How to design an object-oriented program
Techniques for object-oriented design 442
Five steps for designing an object-oriented program 442
How to identify the data attributes 444
How to subdivide the data attributes 446
How to identify the classes 448
How to identify the methods and properties 450
How the three-tier architecture works 452
The Shopping Cart program 454
The business tier 454
The database tier 456
The presentation tier 458
Section 4 Database and GUI programming
Chapter 17 How to work with a database
An introduction to relational databases 470
How a database table is organized 470
How the tables in a database are related 472
How the columns in a table are defined 474
How to use the SQL statements for data manipulation 476
How to select data from a single table 476
How to select data from multiple tables 478
How to insert, update, and delete rows 480
How to use DB Browser for SQLite to work with a database 482
How to use DB Browser to view and edit a table in a SQLite database 482
How to use DB Browser to run SQL statements 484
How to use Python to work with a database 486
How to connect to a SQLite database 485
How to execute SELECT statements 483
How to get the rows in a result set 490
How to execute INSERT, UPDATE, and DELETE statements 492
How to test the database code 494
How to handle database exceptions 494
The Movie List program 496
The user interface 496
The business tier 496
The database tier 498
The presentation tier 502
Chapter 18 How to build a GUI Program
How to create a GUI that handles an event 512
How to display a root window 512
How to work with frames and buttons 514
How to handle a button click event 516
More skills for working with components 518
How to work with labels and text entry fields 518
How to lay out components in a grid 520
How to code a class that defines a frame 522
How to display a message box 524
The Future Value program 526
The business module 526
The ui module 526
Appendix A How to set up Windows for this book
How to install the source code for this book 536
How to install Python and IDLE 538
How to install DB Browser for SQLite 540
How to use DB Browser to verify or restore a database 542
Appendix B How to set up macOS for this book
How to install the source code for this book 546
How to install Python and IDLE 548
How to verify that Python and IDLE are working correctly 550
How to install DB Browser for SQLite 552
How to use DB Browser to verify or restore a database 554