Download full Python for Everyone 2nd Edition Cay Horstmann ebook all chapters
Download full Python for Everyone 2nd Edition Cay Horstmann ebook all chapters
Download full Python for Everyone 2nd Edition Cay Horstmann ebook all chapters
https://ebookgate.com
https://ebookgate.com/product/python-for-
everyone-2nd-edition-cay-horstmann/
https://ebookgate.com/product/python-for-everyone-1st-edition-cay-s-
horstmann/
ebookgate.com
https://ebookgate.com/product/object-oriented-design-and-patterns-2nd-
edition-cay-s-horstmann/
ebookgate.com
https://ebookgate.com/product/java-concepts-late-objects-3rd-edition-
edition-cay-s-horstmann/
ebookgate.com
https://ebookgate.com/product/mark-for-everyone-2nd-edition-tom-
wright/
ebookgate.com
Python for Bioinformatics 2nd Edition Sebastian Bassi
https://ebookgate.com/product/python-for-bioinformatics-2nd-edition-
sebastian-bassi/
ebookgate.com
https://ebookgate.com/product/python-for-scientists-2nd-edition-john-
m-stewart/
ebookgate.com
https://ebookgate.com/product/six-sigma-for-everyone-1st-edition-
george-eckes/
ebookgate.com
https://ebookgate.com/product/a-web-for-everyone-1st-edition-sarah-
horton/
ebookgate.com
https://ebookgate.com/product/python-the-complete-manual-the-
essential-handbook-for-python-users-master-python-today-first-edition-
unknown/
ebookgate.com
Cay Horstmann
San Jose State University
Rance Necaise
Randolph-Macon College
Python for
Everyone
2/e
Copyright © 2016 John Wiley & Sons, Inc.
ISBN 978-1-119-05655-3
ISBN-BRV 978-1-119-05636-2
Toolbox Sections
Many optional “Toolbox” sections introduce useful packages in the wonderful eco-
system of Python libraries. Students are empowered to perform useful work such
as statistical computations, drawing graphs and charts, sending e-mail, processing
spreadsheets, and analyzing web pages. The libraries are placed in the context of
computer science principles, and students learn how those principles apply to solving
real-world problems. Each Toolbox is accompanied by many new end-of-chapter
review and programming exercises.
Data Plotting
Several new Worked Examples show students how to create a visual representation
of data through graphical plots. These examples use the pyplot library to create simple
data plots as they show students how to apply the language constructs introduced in
the respective chapters.
Interactive Learning
Additional interactive content is available that integrates with this text and immerses
students in activities designed to foster in-depth learning. Students don’t just watch
animations and code traces, they work on generating them. The activities provide
instant feedback to show students what they did right and where they need to study
more. To find out more about how to make this content available in your course, visit
http://wiley.com/go/pfe2interactivities.
“CodeCheck” is an innovative online service that students can use to work on pro-
gramming problems. You can assign exercises that have already been prepared, and
you can easily add your own. Visit http://codecheck.it to learn more and to try it out.
Fundamentals
1. Introduction
Object-Oriented Programming
Data Structures & Algorithms
2. Programming
with Numbers
and Strings
3. Decisions
4. Loops
A gentle
introduction to recursion
is optional.
5. Functions
Exercises
End-of-chapter exercises contain a broad mix of review and programming questions,
with optional questions from graphics, science, and business. Designed to engage
students, the exercises illustrate the value of programming in applied fields.
Web Resources
This book is complemented by a complete suite of online resources. Go to www.wiley.
com/college/horstmann to visit the online companion sites, which include
• Source code for all examples programs and Worked Examples in the book.
• Lecture presentation slides (for instructors only).
• Solutions to all review and programming exercises (for instructors only).
• A test bank that focuses on skills, not just terminology (for instructors only). This
extensive set of multiple-choice questions can be used with a word processor or
imported into a course management system.
• “CodeCheck” assignments that allow students to work on programming prob-
lems presented in an innovative online service and receive immediate feedback.
Instructors can assign exercises that have already been prepared, or easily add
their own.
CONTENTS
PREFACE iii CE3 Unbalanced Parentheses 41
SPECIAL FEATURES xviii PT3 Use Spaces in Expressions 42
ST1 Other Ways to Import Modules 42
ST2 Combining Assignment and Arithmetic 42
1 INTRODUCTION 1 ST3 Line Joining 43
ST2 Redirection of Input and Output 179 CE1 Trying to Modify Arguments 254
WE1 Analyzing Baby Names 407 ST3 Storing Data Records 475
TOOLBOX2 Working with Files and WE2 Translating Text Messages 476
Directories 410 8.3 Complex Structures 478
CS1 Encryption Algorithms 412 A Dictionary of Sets 478
7.4 Binary Files and Random Access A Dictionary of Lists 481
(Optional) 413 ST4 User Modules 484
Reading and Writing Binary Files 413 WE3 GRAPHICS: Pie Charts 484
Random Access 414 TOOLBOX1 Harvesting JSON Data from
Image Files 415 the Web 489
Processing BMP Files 416
WE2 GRAPHICS: Displaying a Scene File 419 9 OBJECTS AND CLASSES 499
7.5 Exception Handling 422
Raising Exceptions 423
9.1 Object-Oriented Programming 500
Handling Exceptions 424 9.2 Implementing a Simple Class 502
The finally Clause 426 9.3 Specifying the Public Interface of
PT1 Raise Early, Handle Late 428 a Class 506
PT2 Do Not Use except and finally in the
Same try Statement 428
9.4 Designing the Data Representation 508
PT1 Make All Instance Variables Private, Most
ST4 The with Statement 428
Methods Public 509
TOOLBOX3 Reading Web Pages 429
9.5 Constructors 510
7.6 APPLICATION: Handling Input Errors 430
CE1 Trying to Call a Constructor 512
TOOLBOX4 Statistical Analysis 433
ST1 Default and Named Arguments 512
WE3 Creating a Bubble Chart 438
CS2 The Ariane Rocket Incident 441 9.6 Implementing Methods 513
PT2 Define Instance Variables Only in the
Constructor 516
8 SETS AND ST2 Class Variables 516
DICTIONARIES 457 9.7 Testing a Class 517
8.1 Sets 458 HT1 Implementing a Class 519
WE1 Implementing a Bank Account Class 522
Creating and Using Sets 458
Adding and Removing Elements 459 9.8 PROBLEM SOLVING: Tracing Objects 525
Subsets 460 9.9 PROBLEM SOLVING: Patterns for
Set Union, Intersection, and Difference 461
Object Data 528
WE1 Counting Unique Words 465
Keeping a Total 528
Counting Events 529
Collecting Values 529 11 RECURSION 611
Managing Properties of an Object 530
Modeling Objects with Distinct States 530 11.1 Triangle Numbers Revisited 612
Describing the Position of an Object 531 CE1 Infinite Recursion 615
CS1 Electronic Voting Machines 533 ST1 Recursion with Objects 616
9.10 Object References 534 11.2 PROBLEM SOLVING: Thinking
Shared References 534 Recursively 616
The None Reference 536 WE1 Finding Files 620
The self Reference 536
11.3 Recursive Helper Functions 621
The Lifetime of Objects 537
11.4 The Efficiency of Recursion 622
9.11 APPLICATION: Writing a Fraction
Class 538 11.5 Permutations 627
CS1 The Limits of Computation 630
Fraction Class Design 538
The Constructor 539 11.6 Backtracking 631
Special Methods 540 WE2 Towers of Hanoi 636
Arithmetic Operations 542
11.7 Mutual Recursion 639
Logical Operations 543
TOOLBOX1 Analyzing Web Pages with
ST3 Object Types and Instances 546 Beautiful Soup 643
WE2 GRAPHICS: A Die Class 547
CS2 Open Source and Free Software 550
12 SORTING AND
SEARCHING 655
10 INHERITANCE 563
12.1 Selection Sort 656
10.1 Inheritance Hierarchies 564
12.2 Profiling the Selection Sort
PT1 Use a Single Class for Variation in Values,
Inheritance for Variation in Behavior 567
Algorithm 658
ST1 The Cosmic Superclass: object 568 12.3 Analyzing the Performance of the
10.2 Implementing Subclasses 569 Selection Sort Algorithm 660
ST1 Oh, Omega, and Theta 662
CE1 Confusing Super- and Subclasses 572
ST2 Insertion Sort 663
10.3 Calling the Superclass Constructor 573
12.4 Merge Sort 664
10.4 Overriding Methods 577
CE2 Forgetting to Use the super Function When
12.5 Analyzing the Merge Sort Algorithm 667
Invoking a Superclass Method 580 ST3 The Quicksort Algorithm 669
Programming
Special Topics Random Facts
Tips
© Stephen Coburn/123RF.com.
© Mikhail Mishchenko/123RF Limited. © modella/123RF.com.
Interactive Mode 9 The Python Interpreter 10 Computers Are Everywhere 5
Backup Copies 9
Count Iterations 191 Processing Sentinel Values The First Bug 172
with a Boolean Variable 179 Digital Piracy 223
Redirection of Input and
Output 179
Special Form of the print
Function 188
7 Backslashes in File Names 388 Working with CSV Files 399 Processing Text Files 404
Working with Files and Analyzing Baby Names 407
Directories 410 Graphics: Displaying a
Reading Web Pages 429 Scene File 419
Statistical Analysis 433
Creating a Bubble Chart 438
11 Infinite Recursion 615 Analyzing Web Pages with Finding Files 620
Beautiful Soup 643 Towers of Hanoi 636
Programming
Special Topics Random Facts
Tips
© Stephen Coburn/123RF.com.
Use ListsMishchenko/123RF
© Mikhail for SequencesLimited.
of Reverse Subscripts
© modella/123RF.com. 320 Computer Viruses 321
Related Items 321 Slices 328
Call by Value and
Call by Reference 341
Tuples 342
Functions with a Variable
Number of Arguments 342
Tuple Assignment 343
Returning Multiple Values
with Tuples 343
Tables with Variable
Row Lengths 364
Raise Early, Handle Late 428 Reading the Entire File 397 Encryption Algorithms 412
Do Not Use except and finally Regular Expressions 397 The Ariane Rocket Incident 441
in the Same try Statement 428 Character Encodings 398
The with Statement 428
Use Python Sets, Not Lists, for Hashing 467 Standardization 468
Efficient Set Operations 466 Iterating over
Dictionary Items 475
Storing Data Records 475
User Modules 484
Make all Instance Variables Private, Default and Named Electronic Voting Machines 533
Most Methods Public 509 Arguments 512 Open Source and
Define Instance Variables Class Variables 516 Free Software 550
Only in the Constructor 516 Object Types and Instances 546
Searching and Sorting 679 Oh, Omega, and Theta 662 The First Programmer 683
Insertion Sort 663
The Quicksort Algorithm 669
Comparing Objects 679
1
INTRODUCTION
CHAPTER GOALS
CHAPTER CONTENTS
© JanPietruszka/iStockphoto.
JanPietruszka/iStockphoto.
2
1.2 The Anatomy of a Computer 3
make small changes in a program that lead to immediate improvements, and to see the
computer become an extension of your mental powers.
Figure 1 Central
© Amorphis/iStockphoto. Processing Unit Figure 2 A Hard Disk
PhotoDisc, Inc./Getty Images, Inc.
4 Chapter 1 Introduction
Printer
CPU
Monitor
Microphone
Memory
Speakers
Network
controller Internet
To interact with a human user, a computer requires peripheral devices. The com-
puter transmits information (called output) to the user through a display screen,
speakers, and printers. The user can enter information (called input) for the computer
by using a keyboard or a pointing device such as a mouse.
Some computers are self-contained units, whereas others are interconnected
through networks. Through the network cabling, the computer can read data and
programs from central storage locations or send data to other computers. To the user
of a networked computer, it may not even be obvious which data reside on the com-
puter itself and which are transmitted through the network.
Figure 3 gives a schematic overview of the architecture of a personal computer.
Program instructions and data (such as text, numbers, audio, or video) are stored on
the hard disk, on a compact disk (or DVD), or elsewhere on the network. When a
program is started, it is brought into memory, where the CPU can read it. The CPU
reads the program one instruction at a time. As directed by these instructions, the
CPU reads data, modifies it, and writes it back to memory or the hard disk. Some pro-
gram instructions will cause the CPU to place dots on the display screen or printer or
to vibrate the speaker. As these actions happen many times over and at great speed,
the human user will perceive images and sound. Some program instructions read user
input from the keyboard or mouse. The program analyzes the nature of these inputs
and then executes the next appropriate instruction.