Cay Horstmann San Jose State University
Rance Necaise Randolph-Macon College
Book Details
Price
|
4.00 |
---|---|
Pages
| 769 p |
File Size
|
24,376 KB |
File Type
|
PDF format |
ISBN ISBN-BRV
| 978-1-119-05655-3 978-1-119-05636-2 |
Copyright©
| 2016 John Wiley & Sons, Inc |
This book is an introduction to computer programming using Python that focuses on
the essentials—and on effective learning. Designed to serve a wide range of student
interests and abilities, it is suitable for a first course in programming for computer
scientists, engineers, and students in other disciplines. No prior programming experience
is required, and only a modest amount of high school algebra is needed. For
pedagogical reasons, the book uses Python 3, which is more regular than Python 2.
Here are the book’s key features:
Present fundamentals first.
The book takes a traditional route, first stressing control structures, functions, procedural
decomposition, and the built-in data structures. Objects are used when appropriate
in the early chapters. Students start designing and implementing their own
classes in Chapter 9.
Guidance and worked examples help students succeed.
Beginning programmers often ask “How do I start? Now what do I do?” Of course,
an activity as complex as programming cannot be reduced to cookbook-style instructions.
However, step-by-step guidance is immensely helpful for building confidence
and providing an outline for the task at hand. “Problem Solving” sections stress the
importance of design and planning. “How To” guides help students with common
programming tasks. Numerous Worked Examples demonstrate how to apply chapter
concepts to interesting problems.
Problem solving strategies are made explicit.
Practical, step-by-step illustrations of techniques help students devise and evaluate
solutions to programming problems. Introduced where they are most relevant, these
strategies address barriers to success for many students. Strategies included are:
• Algorithm Design (with pseudocode)
• First Do It By Hand (doing sample
calculations by hand)
• Flowcharts
• Test Cases
• Hand-Tracing
• Storyboards
• Solve a Simpler Problem First
• Reusable Functions
• Stepwise Refinement
• Adapting Algorithms
• Discovering Algorithms by Manipulating Physical Objects
• Tracing Objects
• Patterns for Object Data
• Thinking Recursively
• Estimating the Running Time of an Algorithm
Practice makes perfect.
Of course, programming students need to be able to implement nontrivial programs,
but they first need to have the confidence that they can succeed. This book contains
a substantial number of self-check questions at the end of each section. “Practice It”
pointers suggest exercises to try after each section. And additional practice opportunities,
including automatically-graded programming exercises and skill-oriented
multiple-choice questions, are available online.
A visual approach motivates the reader and eases navigation.
Photographs present visual analogies that explain the
nature and behavior of computer concepts. Step-bystep
figures illustrate complex program operations.
Syntax boxes and example tables present a variety
of typical and special cases in a compact format. It
is easy to get the “lay of the land” by browsing the
visuals, before focusing on the textual material.
Focus on the essentials while being
technically accurate.
An encyclopedic coverage is not helpful for a beginning
programmer, but neither is the opposite—
reducing the material to a list of simplistic bullet points. In this book, the essentials
are presented in digestible chunks, with separate notes that go deeper into good practices
or language features when the reader is ready for the additional information.
Table of Contents
PREFACE iii
SPECIAL FEATURES xviii
INTRODUCTION 1
1.1 Computer Programs 2
1.2 The Anatomy of a Computer 3
CS 1 Computers Are Everywhere 5
1.3 The Python Programming Language 5
1.4 Becoming Familiar with Your Programming
Environment 6
PT 1 Interactive Mode 9
PT 2 Backup Copies 9
ST 1 The Python Interpreter 10
1.5 Analyzing Your First Program 11
1.6 Errors 13
CE 1 Misspelling Words 15
1.7 PROBLEM SOLVING: Algorithm Design 15
HT 1 Describing an Algorithm with
Pseudocode 19
WE 1 Writing an Algorithm for Tiling a Floor 20
PROGRAMMING WITH
NUMBERS AND STRINGS 27
2.1 Variables 28
Defining Variables 28
Number Types 30
Variable Names 31
Constants 32
Comments 33
CE 1 Using Undefined Variables 34
PT 1 Choose Descriptive Variable Names 34
PT 2 Do Not Use Magic Numbers 35
2.2 Arithmetic 35
Basic Arithmetic Operations 35
Powers 36
Floor Division and Remainder 37
Calling Functions 38
Mathematical Functions 39
CE 2 Roundoff Errors 41
CE 3 Unbalanced Parentheses 41
PT 3 Use Spaces in Expressions 42
ST 1 Other Ways to Import Modules 42
ST 2 Combining Assignment and Arithmetic 42
ST 3 Line Joining 43
2.3 PROBLEM SOLVING: First Do It By Hand 43
WE 1 Computing Travel Time 45
2.4 Strings 46
The String Type 46
Concatenation and Repetition 47
Converting Between Numbers and Strings 48
Strings and Characters 48
String Methods 50
ST 4 Character Values 51
ST 5 Escape Sequences 52
CS 1 International Alphabets and Unicode 52
2.5 Input and Output 53
User Input 53
Numerical Input 54
Formatted Output 54
PT 4 Don’t Wait to Convert 58
HT 1 Writing Simple Programs 58
WE 2 Computing the Cost of Stamps 61
CS 2 The Pentium Floating-Point Bug 63
2.6 GRAPHICS: Simple Drawings 63
Creating a Window 64
Lines and Polygons 66
Filled Shapes and Color 67
Ovals, Circles, and Text 69
HT 2 GRAPHICS: Drawing Graphical Shapes 70
TOOLBOX 1 Symbolic Processing with SymPy 73
DECISIONS 91
3.1 The if Statement 92
CE 1 Tabs 96
PT 1 Avoid Duplication in Branches 96
ST 1 Conditional Expressions 97
3.2 Relational Operators 97
CE 2 Exact Comparison of Floating-Point
Numbers 101
ST 2 Lexicographic Ordering of Strings 101
HT 1 Implementing an if Statement 102
WE 1 Extracting the Middle 104
3.3 Nested Branches 106
PT 2 Hand-Tracing 108
CS 1 Denver’s Luggage Handling System 109
3.4 Multiple Alternatives 110
TOOLBOX 1 Sending E-mail 113
3.5 PROBLEM SOLVING: Flowcharts 115
3.6 PROBLEM SOLVING: Test Cases 119
PT 3 Make a Schedule and Make Time for
Unexpected Problems 120
3.7 Boolean Variables and Operators 121
CE 3 Confusing and and or Conditions 124
PT 4 Readability 124
ST 3 Chaining Relational Operators 125
ST 4 Short-Circuit Evaluation of Boolean
Operators 125
ST 5 De Morgan’s Law 126
3.8 Analyzing Strings 126
3.9 APPLICATION: Input Validation 130
ST 6 Terminating a Program 133
ST 7 Interactive Graphical Programs 133
CS 2 Artificial Intelligence 134
WE 2 GRAPHICS: Intersecting Circles 134
TOOLBOX 2 Plotting Simple Graphs 138
LOOPS 165
4.1 The while Loop 166
CE 1 Don’t Think “Are We There Yet?” 170
CE 2 Infinite Loops 171
CE 3 Off-by-One Errors 171
CS 1 The First Bug 172
4.2 PROBLEM SOLVING: Hand-Tracing 173
4.3 APPLICATION: Processing Sentinel
Values 176
ST 1 Processing Sentinel Values with a
Boolean Variable 179
ST 2 Redirection of Input and Output 179
4.4 PROBLEM SOLVING: Storyboards 180
4.5 Common Loop Algorithms 183
Sum and Average Value 183
Counting Matches 184
Prompting Until a Match is Found 184
Maximum and Minimum 184
Comparing Adjacent Values 185
4.6 The for Loop 187
PT 1 Count Iterations 191
HT 1 Writing a Loop 192
4.7 Nested Loops 194
ST 3 Special Form of the print Function 198
WE 1 Average Exam Grades 198
WE 2 A Grade Distribution Histogram 200
4.8 Processing Strings 202
Counting Matches 202
Finding All Matches 203
Finding the First or Last Match 203
Validating a String 204
Building a New String 204
4.9 APPLICATION: Random Numbers and
Simulations 206
Generating Random Numbers 207
Simulating Die Tosses 207
The Monte Carlo Method 208
WE 3 GRAPHICS: Bull’s Eye 210
4.10 GRAPHICS: Digital Image Processing 212
Filtering Images 212
Reconfiguring Images 215
4.11 PROBLEM SOLVING: Solve a Simpler
Problem First 217
CS 2 Digital Piracy 223
FUNCTIONS 245
5.1 Functions as Black Boxes 246
5.2 Implementing and Testing Functions 248
Implementing a Function 248
Testing a Function 249
Programs that Contain Functions 250
PT 1 Function Comments 252
5.3 Parameter Passing 252
PT 2 Do Not Modify Parameter Variables 254
CE 1 Trying to Modify Arguments 254
5.4 Return Values 255
ST 1 Using Single-Line Compound
Statements 256
HT 1 Implementing a Function 257
WE 1 Generating Random Passwords 259
5.5 Functions Without Return Values 263
5.6 PROBLEM SOLVING: Reusable
Functions 265
CS 1 Personal Computing 268
5.7 PROBLEM SOLVING: Stepwise
Refinement 269
PT 3 Keep Functions Short 273
PT 4 Tracing Functions 274
PT 5 Stubs 275
WE 2 Calculating a Course Grade 275
WE 3 Using a Debugger 278
5.8 Variable Scope 282
PT 6 Avoid Global Variables 285
WE 4 GRAPHICS: Rolling Dice 285
5.9 GRAPHICS: Building an Image Processing
Toolkit 288
Getting Started 288
Comparing Images 289
Adjusting Image Brightness 290
Rotating an Image 291
Using the Toolkit 292
WE 5 Plotting Growth or Decay 294
5.10 Recursive Functions (Optional) 296
HT 2 Thinking Recursively 299
LISTS 315
6.1 Basic Properties of Lists 316
Creating Lists 316
Accessing List Elements 317
Traversing Lists 318
List References 319
CE 1 Out-of-Range Errors 320
ST 1 Reverse Subscripts 320
PT 1 Use Lists for Sequences of Related
Items 321
CS 1 Computer Viruses 321
6.2 List Operations 322
Appending Elements 322
Inserting an Element 322
Finding an Element 323
Removing an Element 324
Concatenation and Replication 325
Equality Testing 325
Sum, Maximum, Minimum, and Sorting 325
Copying Lists 326
ST 2 Slices 328
6.3 Common List Algorithms 328
Filling 329
Combining List Elements 329
Element Separators 329
Maximum and Minimum 330
Linear Search 330
Collecting and Counting Matches 331
Removing Matches 331
Swapping Elements 332
Reading Input 333
WE 1 Plotting Trigonometric Functions 335
6.4 Using Lists with Functions 338
ST 3 Call by Value and Call by Reference 341
ST 4 Tuples 342
ST 5 Functions with a Variable Number of
Arguments 342
ST 6 Tuple Assignment 343
ST 7 Returning Multiple Values with Tuples 343
TOOLBOX 1 Editing Sound Files 344
6.5 PROBLEM SOLVING: Adapting
Algorithms 345
HT 1 Working with Lists 347
WE 2 Rolling the Dice 349
6.6 PROBLEM SOLVING: Discovering Algorithms by
Manipulating Physical Objects 352
6.7 Tables 356
Creating Tables 357
Accessing Elements 358
Locating Neighboring Elements 358
Computing Row and Column Totals 359
Using Tables with Functions 360
WE 3 A World Population Table 362
ST 8 Tables with Variable Row Lengths 364
WE 4 GRAPHICS: Drawing Regular Polygons 365
FILES AND EXCEPTIONS 383
7.1 Reading and Writing Text Files 384
Opening a File 384
Reading from a File 385
Writing from a File 386
A File Processing Example 386
CE 1 Backslashes in File Names 388
7.2 Text Input and Output 388
Iterating over the Lines of a File 388
Reading Words 390
Reading Characters 392
Reading Records 393
ST 1 Reading the Entire File 397
ST 2 Regular Expressions 397
ST 3 Character Encodings 398
TOOLBOX 1 Working with CSV Files 399
7.3 Command Line Arguments 401
HT 1 Processing Text Files 404
WE 1 Analyzing Baby Names 407
TOOLBOX 2 Working with Files and
Directories 410
CS 1 Encryption Algorithms 412
7.4 Binary Files and Random Access
(Optional) 413
Reading and Writing Binary Files 413
Random Access 414
Image Files 415
Processing BMP Files 416
WE 2 GRAPHICS: Displaying a Scene File 419
7.5 Exception Handling 422
Raising Exceptions 423
Handling Exceptions 424
The finally Clause 426
PT 1 Raise Early, Handle Late 428
PT 2 Do Not Use except and finally in the
Same try Statement 428
ST 4 The with Statement 428
TOOLBOX 3 Reading Web Pages 429
7.6 APPLICATION: Handling Input Errors 430
TOOLBOX 4 Statistical Analysis 433
WE 3 Creating a Bubble Chart 438
CS 2 The Ariane Rocket Incident 441
SETS AND
DICTIONARIES 457
8.1 Sets 458
Creating and Using Sets 458
Adding and Removing Elements 459
Subsets 460
Set Union, Intersection, and Difference 461
WE 1 Counting Unique Words 465
PT 1 Use Python Sets, Not Lists, for Efficient Set
Operations 466
ST 1 Hashing 467
CS 1 Standardization 468
8.2 Dictionaries 468
Creating Dictionaries 469
Accessing Dictionary Values 470
Adding and Modifying Items 470
Removing Items 471
Traversing a Dictionary 472
ST 2 Iterating over Dictionary Items 475
ST 3 Storing Data Records 475
WE 2 Translating Text Messages 476
8.3 Complex Structures 478
A Dictionary of Sets 478
A Dictionary of Lists 481
ST 4 User Modules 484
WE 3 GRAPHICS: Pie Charts 484
TOOLBOX 1 Harvesting JSON Data from
the Web 489
OBJECTS AND CLASSES 499
9.1 Object-Oriented Programming 500
9.2 Implementing a Simple Class 502
9.3 Specifying the Public Interface of
a Class 506
9.4 Designing the Data Representation 508
PT 1 Make All Instance Variables Private, Most
Methods Public 509
9.5 Constructors 510
CE 1 Trying to Call a Constructor 512
ST 1 Default and Named Arguments 512
9.6 Implementing Methods 513
PT 2 Define Instance Variables Only in the
Constructor 516
ST 2 Class Variables 516
9.7 Testing a Class 517
HT 1 Implementing a Class 519
WE 1 Implementing a Bank Account Class 522
9.8 PROBLEM SOLVING: Tracing Objects 525
9.9 PROBLEM SOLVING: Patterns for
Object Data 528
Keeping a Total 528
Counting Events 529
Collecting Values 529
Managing Properties of an Object 530
Modeling Objects with Distinct States 530
Describing the Position of an Object 531
CS 1 Electronic Voting Machines 533
9.10 Object References 534
Shared References 534
The None Reference 536
The self Reference 536
The Lifetime of Objects 537
9.11 APPLICATION: Writing a Fraction
Class 538
Fraction Class Design 538
The Constructor 539
Special Methods 540
Arithmetic Operations 542
Logical Operations 543
ST 3 Object Types and Instances 546
WE 2 GRAPHICS: A Die Class 547
CS 2 Open Source and Free Software 550
INHERITANCE 563
10.1 Inheritance Hierarchies 564
PT 1 Use a Single Class for Variation in Values,
Inheritance for Variation in Behavior 567
ST 1 The Cosmic Superclass: object 568
10.2 Implementing Subclasses 569
CE 1 Confusing Super- and Subclasses 572
10.3 Calling the Superclass Constructor 573
10.4 Overriding Methods 577
CE 2 Forgetting to Use the super Function When
Invoking a Superclass Method 580
10.5 Polymorphism 580
ST 2 Subclasses and Instances 584
ST 3 Dynamic Method Lookup 584
ST 4 Abstract Classes 585
CE 3 Don’t Use Type Tests 586
HT 1 Developing an Inheritance Hierarchy 586
WE 1 Implementing an Employee Hierarchy for
Payroll Processing 591
10.6 APPLICATION: A Geometric Shape Class
Hierarchy 594
The Base Class 595
Basic Shapes 597
Groups of Shapes 600
RECURSION 611
11.1 Triangle Numbers Revisited 612
CE 1 Infinite Recursion 615
ST 1 Recursion with Objects 616
11.2 PROBLEM SOLVING: Thinking
Recursively 616
WE 1 Finding Files 620
11.3 Recursive Helper Functions 621
11.4 The Efficiency of Recursion 622
11.5 Permutations 627
CS 1 The Limits of Computation 630
11.6 Backtracking 631
WE 2 Towers of Hanoi 636
11.7 Mutual Recursion 639
TOOLBOX 1 Analyzing Web Pages with
Beautiful Soup 643
SORTING AND
SEARCHING 655
12.1 Selection Sort 656
12.2 Profiling the Selection Sort
Algorithm 658
12.3 Analyzing the Performance of the
Selection Sort Algorithm 660
ST 1 Oh, Omega, and Theta 662
ST 2 Insertion Sort 663
12.4 Merge Sort 664
12.5 Analyzing the Merge Sort Algorithm 667
ST 3 The Quicksort Algorithm 669
12.6 Searching 671
Linear Search 671
Binary Search 672
12.7 PROBLEM SOLVING: Estimating the Running
Time of an Algorithm 674
Linear Time 674
Quadratic Time 675
The Triangle Pattern 676
Logarithmic Time 677
PT 1 Searching and Sorting 679
ST 4 Comparing Objects 679
WE 1 Enhancing the Insertion Sort Algorithm 680
CS 1 The First Programmer 683
Appendix A PYTHON OPERATOR SUMMARY A-1
Appendix B PYTHON RESERVED WORD
SUMMARY A-3
Appendix C THE PYTHON STANDARD
LIBRARY A-5
Appendix D THE BASIC LATIN AND LATIN-1 SUBSETS
OF UNICODE*
Appendix E BINARY NUMBERS AND BIT OPERATIONS*
Appendix F HTML SUMMARY*
GLOSSARY A-20
INDEX A-25
CREDITS A-40
A Tour of the Book
Figure 1 shows the dependencies between the chapters and how topics are organized.
The core material of the book is:
Chapter 1. Introduction
Chapter 2. Programming with
Numbers and Strings
Chapter 3. Decisions
Chapter 4. Loops
Chapter 5. Functions
Chapter 6. Lists
Chapter 7. Files and Exceptions
Chapter 8. Sets and Dictionaries
Two chapters cover object-oriented programming:
Chapter 9. Objects and Classes
Chapter 10. Inheritance
Two chapters support a course that goes more deeply into algorithm design and analysis:
Chapter 11. Recursion
Chapter 12. Sorting and Searching