Cover ....1
Contents....2
Welcome....12
Cover....12
Title Page....12
Copyright Page....13
Pearson’s Commitment to Diversity, Equity, and Inclusion....14
Contents in a Glance....14
Contents in a Glance....14
Location of VideoNotes....15
Preface....17
Preface....17
Changes in the Sixth Edition....17
Brief Overview of Each Chapter....18
Organization of the Text....21
Features of the Text....22
Supplements....23
Acknowledgments....24
About the Author....26
Ordering Options....26
1: Introduction to Computers and Programming....28
1: Topics....28
1.1: Introduction....28
1.2: Hardware and Software....29
Main Memory....32
Software....33
1.2: (Noninteractive) Checkpoint Questions from the Book....34
1.3: How Computers Store Data....34
1.3: (Noninteractive) Checkpoint Questions from the Book....39
1.4: How a Program Works....39
From Machine Language to Assembly Language....41
Keywords, Operators, and Syntax: An Overview....44
1.4: (Noninteractive) Checkpoint Questions from the Book....46
1.5: Using Python....47
Writing Python Programs and Running Them in Script Mode....49
The IDLE Programming Environment....50
Chapter 1: From the Book for Additional Practice....51
Chapter 1: Review Questions....51
Chapter 1: Programming Exercises....55
2: Input, Processing, and Output....57
2: Topics....57
2.1: Designing a Program....57
More About the Design Process....58
2.1: (Noninteractive) Checkpoint Questions from the Book....61
2.2: Input, Processing, and Output....62
2.3: Displaying Output with the print Function....62
2.3: (Noninteractive) Checkpoint Questions from the Book....65
2.4: Comments....65
2.5: Variables....66
Creating Variables with Assignment Statements....67
Multiple Assignment....69
Variable Naming Rules....70
Displaying Multiple Items with the print Function....71
Variable Reassignment....72
Numeric Data Types and Literals....73
Storing Strings with the str Data Type....74
Reassigning a Variable to a Different Type....75
2.5: (Noninteractive) Checkpoint Questions from the Book....76
2.6: Reading Input from the Keyboard....76
Reading Numbers with the input Function....78
2.6: (Noninteractive) Checkpoint Questions from the Book....81
2.7: Performing Calculations....81
In the Spotlight: Calculating a Percentage....82
Floating-Point and Integer Division....84
Operator Precedence....84
In the Spotlight: Calculating an Average....86
The Exponent Operator....87
The Remainder Operator....88
Converting Math Formulas to Programming Statements....89
In the Spotlight: Converting a Math Formula to a Programming Statement....90
Mixed-Type Expressions and Data Type Conversion....91
Breaking Long Statements into Multiple Lines....92
2.7: (Noninteractive) Checkpoint Questions from the Book....93
2.8: String Concatenation....93
2.8: (Noninteractive) Checkpoint Questions from the Book....95
2.9: More About the print Function....95
Escape Characters....97
2.9: (Noninteractive) Checkpoint Questions from the Book....98
2.10: Displaying Formatted Output with F-strings....99
Placeholder Expressions....100
Formatting Values....100
Specifying a Minimum Field Width....103
Aligning Values....105
The Order of Designators....107
Concatenation with F-strings....107
2.10: (Noninteractive) Checkpoint Questions from the Book....108
2.11: Named Constants....109
2.11: (Noninteractive) Checkpoint Questions from the Book....110
2.12: Introduction to Turtle Graphics....110
Setting the Turtle’s Heading to a Specific Angle....117
Changing the Drawing Color....121
Moving the Turtle to a Specific Location....122
Controlling the Turtle’s Animation Speed....124
Filling Shapes....126
Getting Input with a Dialog Box....129
In the Spotlight: The Orion Constellation Program....132
2.12: (Noninteractive) Checkpoint Questions from the Book....140
Chapter 2: From the Book for Additional Practice....140
Chapter 2: Review Questions....140
Chapter 2: Programming Exercises....145
3: Decision Structures and Boolean Logic....150
3: Topics....150
3.1: The if Statement....150
Boolean Expressions and Relational Operators....153
Putting It All Together....156
In the Spotlight: Using the if Statement....157
Single-Line if Statements....158
3.1: (Noninteractive) Checkpoint Questions from the Book....159
3.2: The if-else Statement....159
In the Spotlight: Using the if-else Statement....161
3.2: (Noninteractive) Checkpoint Questions from the Book....162
3.3: Comparing Strings....162
3.3: (Noninteractive) Checkpoint Questions from the Book....166
3.4: Nested Decision Structures and the if-elif-else Statement....166
In the Spotlight: Multiple Nested Decision Structures....171
The if-elif-else Statement....173
3.4: (Noninteractive) Checkpoint Questions from the Book....174
3.5: Logical Operators....174
The Loan Qualifier Program Revisited....178
Checking Numeric Ranges with Logical Operators....180
3.5: (Noninteractive) Checkpoint Questions from the Book....181
3.6: Boolean Variables....182
3.6: (Noninteractive) Checkpoint Questions from the Book....183
3.7: Conditional Expressions....183
3.8: Assignment Expressions and the Walrus Operator....185
3.9: Turtle Graphics: Determining the State of the Turtle....187
In the Spotlight: The Hit the Target Game....190
3.9: (Noninteractive) Checkpoint Questions from the Book....194
Chapter 3: From the Book for Additional Practice....194
Chapter 3: Review Questions....194
Chapter 3: Programming Exercises....198
4: Repetition Structures....205
4: Topics....205
4.1: Introduction to Repetition Structures....205
4.1: (Noninteractive) Checkpoint Questions from the Book....206
4.2: The while Loop: A Condition-Controlled Loop....207
The while Loop Is a Pretest Loop....210
In the Spotlight: Designing a Program with a while Loop....211
Infinite Loops....212
Using the while Loop as a Count-Controlled Loop ....213
Single-Line while Loops....216
4.2: (Noninteractive) Checkpoint Questions from the Book....217
4.3: The for Loop: A Count-Controlled Loop....217
Using the range Function with the for Loop....219
Using the Target Variable Inside the Loop....221
In the Spotlight: Designing a Count-Controlled Loop with the for Statement....223
Letting the User Control the Loop Iterations....225
Generating an Iterable Sequence that Ranges from Highest to Lowest....226
4.3: (Noninteractive) Checkpoint Questions from the Book....227
4.4: Calculating a Running Total....227
The Augmented Assignment Operators....229
4.4: (Noninteractive) Checkpoint Questions from the Book....231
4.5: Sentinels....231
In the Spotlight: Using a Sentinel....232
4.5: (Noninteractive) Checkpoint Questions from the Book....233
4.6: Input Validation Loops....233
In the Spotlight: Writing an Input Validation Loop....236
Using the Walrus Operator in an Input Validation Loop....237
4.6: (Noninteractive) Checkpoint Questions from the Book....238
4.7: Nested Loops....238
In the Spotlight: Using Nested Loops to Print Patterns....241
4.8: Using break, continue, and else with Loops....245
Using the else Clause with a Loop....248
4.9: Turtle Graphics: Using Loops to Draw Designs....249
Chapter 4: From the Book for Additional Practice....253
Chapter 4: Review Questions....253
Chapter 4: Programming Exercises....256
5: Functions....261
5: Topics....261
5.1: Introduction to Functions....261
5.1: (Noninteractive) Checkpoint Questions from the Book....263
5.2: Defining and Calling a Void Function....264
Defining and Calling a Function....264
Indentation in Python....268
5.2: (Noninteractive) Checkpoint Questions from the Book....269
5.3: Designing a Program to Use Functions....270
In the Spotlight: Defining and Calling Functions....272
Pausing Execution Until the User Presses Enter....275
Using the pass Keyword....275
5.4: Local Variables....276
5.4: (Noninteractive) Checkpoint Questions from the Book....278
5.5: Passing Arguments to Functions....278
In the Spotlight: Passing an Argument to a Function....281
Passing Multiple Arguments....282
Making Changes to Parameters....284
Keyword Arguments....286
Keyword-Only Parameters....288
Positional-Only Parameters....289
Default Arguments....289
5.5: (Noninteractive) Checkpoint Questions from the Book....292
5.6: Global Variables and Global Constants....293
In the Spotlight: Using Global Constants....295
5.6: (Noninteractive) Checkpoint Questions from the Book....296
5.7: Introduction to Value-Returning Functions: Generating Random Numbers....296
Standard Library Functions and the import Statement....297
Generating Random Numbers....298
Calling Functions from an F-String....301
Experimenting with Random Numbers in Interactive Mode....301
In the Spotlight: Using Random Numbers....302
In the Spotlight: Using Random Numbers to Represent Other Values....303
The randrange, random, and uniform Functions....304
Random Number Seeds....305
5.7: (Noninteractive) Checkpoint Questions from the Book....306
5.8: Writing Your Own Value-Returning Functions....307
How to Use Value-Returning Functions....309
Using IPO Charts....311
In the Spotlight: Modularizing with Functions....312
Returning Strings....316
Returning Boolean Values....316
Returning Multiple Values....318
Returning None from a Function....319
5.8: (Noninteractive) Checkpoint Questions from the Book....321
5.9: The math Module....321
5.9: (Noninteractive) Checkpoint Questions from the Book....324
5.10: Storing Functions in Modules....324
Conditionally Executing the main Function in a Module....327
5.11: Turtle Graphics: Modularizing Code with Functions....329
Storing Your Graphics Functions in a Module....333
Chapter 5: From the Book for Additional Practice....336
Chapter 5: Review Questions....336
Chapter 5: Programming Exercises....341
6: Files and Exceptions....348
6: Topics....348
6.1: Introduction to File Input and Output....348
Types of Files....350
Opening a File....352
Writing Data to a File....353
Reading Data from a File....355
Concatenating a Newline to a String....359
Reading a String and Stripping the Newline from It....361
Appending Data to an Existing File....362
Writing and Reading Numeric Data....363
6.1: (Noninteractive) Checkpoint Questions from the Book....366
6.2: Using Loops to Process Files....366
Reading a File with a Loop and Detecting the End of the File....367
Using Python’s for Loop to Read Lines....369
In the Spotlight: Working with Files....370
6.2: (Noninteractive) Checkpoint Questions from the Book....372
6.3: Using the with Statement to Open Files....373
Opening Multiple Files with a with Statement....375
6.4: Processing Records....376
In the Spotlight: Adding and Displaying Records....380
In the Spotlight: Searching for a Record....381
In the Spotlight: Modifying Records....383
In the Spotlight: Deleting Records....385
6.4: (Noninteractive) Checkpoint Questions from the Book....387
6.5: Exceptions....387
Handling Multiple Exceptions....392
Displaying an Exception’s Default Error Message....394
The else Clause....396
The finally Clause....397
What If an Exception Is Not Handled?....398
6.5: (Noninteractive) Checkpoint Questions from the Book....398
Chapter 6: From the Book for Additional Practice....398
Chapter 6: Review Questions....398
Chapter 6: Programming Exercises....402
7: Lists and Tuples....405
7: Topics....405
7.1: Sequences....405
7.2: Introduction to Lists....405
The Repetition Operator....407
Iterating over a List with the for Loop....408
Indexing....409
The len Function....410
Using a for Loop to Iterate by Index Over a List....411
Lists Are Mutable....411
Concatenating Lists....413
7.2: (Noninteractive) Checkpoint Questions from the Book....414
7.3: List Slicing....415
7.3: (Noninteractive) Checkpoint Questions from the Book....417
7.4: Finding Items in Lists with the in Operator....418
7.4: (Noninteractive) Checkpoint Questions from the Book....419
7.5: List Methods and Useful Built-in Functions....419
7.5: (Noninteractive) Checkpoint Questions from the Book....426
7.6: Copying Lists....426
7.7: Processing Lists....428
In the Spotlight: Using List Elements in a Math Expression....428
Totaling the Values in a List....430
Averaging the Values in a List....430
Passing a List as an Argument to a Function....431
Returning a List from a Function....432
In the Spotlight: Processing a List....434
Randomly Selecting List Elements....436
Working with Lists and Files....437
7.8: List Comprehensions....439
Using if Clauses with List Comprehensions....441
7.8: (Noninteractive) Checkpoint Questions from the Book....442
7.9: Two-Dimensional Lists....442
7.9: (Noninteractive) Checkpoint Questions from the Book....447
7.10: Tuples....447
Reassigning a Tuple to a Variable....449
Storing Mutable Objects in a Tuple....449
Converting Between Lists and Tuples....451
7.10: (Noninteractive) Checkpoint Questions from the Book....452
7.11: Plotting List Data with the matplotlib Package....452
Plotting a Line Graph....453
Plotting a Bar Chart....464
Plotting a Pie Chart....469
7.11: (Noninteractive) Checkpoint Questions from the Book....472
Chapter 7: From the Book for Additional Practice....472
Chapter 7: Review Questions....472
Chapter 7: Programming Exercises....476
8: More About Strings....481
8: Topics....481
8.1: Basic String Operations....481
Accessing the Individual Characters in a String....481
String Concatenation....485
Strings Are Immutable....487
8.1: (Noninteractive) Checkpoint Questions from the Book....488
8.2: String Slicing....488
In the Spotlight: Extracting Characters from a String....490
8.2: (Noninteractive) Checkpoint Questions from the Book....491
8.3: Testing, Searching, and Manipulating Strings....492
String Methods....493
In the Spotlight: Validating the Characters in a Password....499
The Repetition Operator....501
Splitting a String....502
In the Spotlight: String Tokens....504
In the Spotlight: Reading CSV Files....506
8.3: (Noninteractive) Checkpoint Questions from the Book....507
Chapter 8: From the Book for Additional Practice....508
Chapter 8: Review Questions....508
Chapter 8: Programming Exercises....511
9: Dictionaries and Sets....517
9: Topics....517
9.1: Dictionaries....517
Retrieving a Value from a Dictionary....518
Using the in and not in Operators to Test for a Value in a Dictionary....519
Adding Elements to an Existing Dictionary....520
Deleting Elements....520
Getting the Number of Elements in a Dictionary....521
Mixing Data Types in a Dictionary....522
Creating an Empty Dictionary....523
Using the for Loop to Iterate over a Dictionary....524
Some Dictionary Methods....524
In the Spotlight: Using a Dictionary to Simulate a Deck of Cards....530
In the Spotlight: Storing Names and Birthdays in a Dictionary....533
The Dictionary Merge and Update Operators....538
Dictionary Comprehensions....539
Using if Clauses with Dictionary Comprehensions....541
9.1: (Noninteractive) Checkpoint Questions from the Book....542
9.2: Sets....543
Adding and Removing Elements....545
Using the for Loop to Iterate over a Set....547
Using the in and not in Operators to Test for a Value in a Set....548
Finding the Union of Sets....548
Finding the Intersection of Sets....549
Finding the Difference of Sets....550
Finding the Symmetric Difference of Sets....550
Finding Subsets and Supersets....551
In the Spotlight: Set Operations....552
Set Comprehensions....554
9.2: (Noninteractive) Checkpoint Questions from the Book....555
9.3: Serializing Objects....556
9.3: (Noninteractive) Checkpoint Questions from the Book....562
Chapter 9: From the Book for Additional Practice....562
Chapter 9: Review Questions....562
Chapter 9: Programming Exercises....569
10: Classes and Object-Oriented Programming....574
10: Topics....574
10.1: Procedural and Object-Oriented Programming....574
10.1: (Noninteractive) Checkpoint Questions from the Book....577
10.2: Classes....577
Class Definitions....579
Hiding Attributes....584
Storing Classes in Modules....587
The BankAccount Class....588
The __str__ Method....591
10.2: (Noninteractive) Checkpoint Questions from the Book....594
10.3: Working with Instances....594
In the Spotlight: Creating the CellPhone Class....597
Accessor and Mutator Methods....599
In the Spotlight: Storing Objects in a List....600
Passing Objects as Arguments....601
In the Spotlight: Pickling Your Own Objects....603
In the Spotlight: Storing Objects in a Dictionary....605
10.3: (Noninteractive) Checkpoint Questions from the Book....613
10.4: Techniques for Designing Classes....613
Finding the Classes in a Problem....614
Identifying a Class’s Responsibilities....621
10.4: (Noninteractive) Checkpoint Questions from the Book....626
Chapter 10: From the Book for Additional Practice....626
Chapter 10: Review Questions....626
Chapter 10: Programming Exercises....629
11: Inheritance....634
11: Topics....634
11.1: Introduction to Inheritance....634
In the Spotlight: Using Inheritance....644
11.1: (Noninteractive) Checkpoint Questions from the Book....648
11.2: Polymorphism....648
11.2: (Noninteractive) Checkpoint Questions from the Book....655
Chapter 11: From the Book for Additional Practice....655
Chapter 11: Review Questions....655
Chapter 11: Programming Exercises....657
12: Recursion....659
12: Topics....659
12.1: Introduction to Recursion....659
12.2: Problem Solving with Recursion....661
12.2: (Noninteractive) Checkpoint Questions from the Book....665
12.3: Examples of Recursive Algorithms....665
Chapter 12: From the Book for Additional Practice....672
Chapter 12: Review Questions....672
Chapter 12: Programming Exercises....674
13: GUI Programming....676
13: Topics....676
13.1: Graphical User Interfaces....676
13.1: (Noninteractive) Checkpoint Questions from the Book....679
13.2: Using the tkinter Module....679
13.2: (Noninteractive) Checkpoint Questions from the Book....683
13.3: Displaying Text with Label Widgets....683
13.3: (Noninteractive) Checkpoint Questions from the Book....693
13.4: Organizing Widgets with Frames....694
13.5: Button Widgets and Info Dialog Boxes....697
13.6: Getting Input with the Entry Widget....701
13.7: Using Labels as Output Fields....704
In the Spotlight: Creating a GUI Program....707
13.7: (Noninteractive) Checkpoint Questions from the Book....712
13.8: Radio Buttons and Check Buttons....712
13.8: (Noninteractive) Checkpoint Questions from the Book....718
13.9: Listbox Widgets....718
Specifying the Size of the Listbox....720
Using a Loop to Populate the Listbox....720
Selecting Items in a Listbox....721
Deleting Items from a Listbox....724
Executing a Callback Function When the User Clicks a Listbox Item....725
In the Spotlight: The Time Zone Program....726
Adding Scrollbars to a Listbox....730
13.9: (Noninteractive) Checkpoint Questions from the Book....738
13.10: Drawing Shapes with the Canvas Widget....738
Drawing Lines: The create_line Method....741
Drawing Rectangles: The create_rectangle Method....743
Drawing Ovals: The create_oval Method....746
Drawing Arcs: The create_arc Method....748
Drawing Polygons: The create_polygon Method....754
Drawing Text: The create_text Method....757
13.10: (Noninteractive) Checkpoint Questions from the Book....762
Chapter 13: From the Book for Additional Practice....762
Chapter 13: Review Questions....762
Chapter 13: Programming Exercises....766
14: Database Programming....770
14: Topics....770
14.1: Database Management Systems....770
14.1: (Noninteractive) Checkpoint Questions from the Book....772
14.2: Tables, Rows, and Columns....772
Column Data Types....774
Primary Keys....775
Identity Columns....775
Allowing Null Values....777
14.2: (Noninteractive) Checkpoint Questions from the Book....777
14.3: Opening and Closing a Database Connection with SQLite....777
14.3: (Noninteractive) Checkpoint Questions from the Book....780
14.4: Creating and Deleting Tables....780
Creating Multiple Tables....783
Creating a Table Only If It Does Not Already Exist....784
Deleting a Table....784
14.4: (Noninteractive) Checkpoint Questions from the Book....785
14.5: Adding Data to a Table....785
Inserting Multiple Rows with One INSERT Statement....788
Inserting NULL Data....788
Inserting the Values of Variables....789
Watch Out for SQL Injection Attacks....791
14.5: (Noninteractive) Checkpoint Questions from the Book....791
14.6: Querying Data with the SQL SELECT Statement....792
The SELECT Statement....793
Selecting All the Columns in a Table....796
Specifying Search Criteria with the WHERE Clause....798
SQL Logical Operators: AND, OR, and NOT....800
String Comparisons in a SELECT Statement....801
Using the LIKE Operator....801
Sorting the Results of a SELECT Query....803
Aggregate Functions....805
14.6: (Noninteractive) Checkpoint Questions from the Book....807
14.7: Updating and Deleting Existing Rows....808
Updating Multiple Columns....811
Determining the Number of Rows Updated....811
Deleting Rows with the DELETE Statement....812
Determining the Number of Rows Deleted....814
14.7: (Noninteractive) Checkpoint Questions from the Book....814
14.8: More About Primary Keys....814
The RowID Column in SQLite....815
Integer Primary Keys in SQLite....815
Primary Keys Other Than Integer....816
Composite Keys....816
14.8: (Noninteractive) Checkpoint Questions from the Book....818
14.9: Handling Database Exceptions....818
14.9: (Noninteractive) Checkpoint Questions from the Book....820
14.10: CRUD Operations....820
In the Spotlight: Inventory CRUD Application....820
14.11: Relational Data....827
Foreign Keys....828
Entity Relationship Diagrams....829
Creating Foreign Keys in SQL....830
Updating Relational Data....834
Deleting Relational Data....834
Retrieving Columns from Multiple Tables in a SELECT Statement....835
In the Spotlight: A GUI Application to Read a Database....837
14.11: (Noninteractive) Checkpoint Questions from the Book....842
Chapter 14: From the Book for Additional Practice....842
Chapter 14: Review Questions....842
Chapter 14: Programming Exercises....849
Appendix A: Installing Python....852
Appendix B: Introduction to IDLE....854
Writing a Python Program in the IDLE Editor....856
Color Coding....857
Automatic Indentation....857
Saving a Program....858
Running a Program....858
Appendix C: The ASCII Character Set....862
Appendix D: Predefined Named Colors....864
Appendix E: More About the import Statement....872
Appendix F: Formatting Numeric Output with the format() Function....875
Formatting in Scientific Notation....876
Inserting Comma Separators....876
Specifying a Minimum Field Width....877
Formatting a Floating-Point Number as a Percentage....878
Formatting Integers....879
Appendix G: Installing Modules with the pip Utility....880
Appendix H: Answers to Noninteractive Checkpoints....881
Chapter 1: Answers to Noninteractive Checkpoints....881
Chapter 2: Answers to Noninteractive Checkpoints....881
Chapter 3: Answers to Noninteractive Checkpoints....884
Chapter 4: Answers to Noninteractive Checkpoints....886
Chapter 5: Answers to Noninteractive Checkpoints....887
Chapter 6: Answers to Noninteractive Checkpoints....889
Chapter 7: Answers to Noninteractive Checkpoints....890
Chapter 8: Answers to Noninteractive Checkpoints....892
Chapter 9: Answers to Noninteractive Checkpoints....893
Chapter 10: Answers to Noninteractive Checkpoints....895
Chapter 11: Answers to Noninteractive Checkpoints....896
Chapter 12: Answers to Noninteractive Checkpoints....896
Chapter 13: Answers to Noninteractive Checkpoints....896
Chapter 14: Answers to Noninteractive Checkpoints....898
Student Supplemental Materials....901
Glossary....902
Footnotes....912
Index....914
Starting Out with Python® introduces programming concepts and problem-solving skills using Tony Gaddis' accessible approach. Written for novice programmers, Gaddis uses easy-to-understand language to introduce concepts. Control structures are explained, then classes and GUI applications. Every chapter includes clear and easy-to-read code listings, practical real-world examples, focused explanations and an abundance of exercises. As you progress, you'll learn to recognize how to design the logic of high-quality programs and then implement those programs using Python.
The 6th Edition is thoroughly updated with new language features and functionality for versions of Python up through Python 3.9.
Revel®empowers you to actively participate in learning. More than a digital textbook, Revel delivers an engaging blend of author content, media, and assessment. With Revel, you can read and practice in one continuous experience anytime, anywhere, on any device.