100% found this document useful (1 vote)
25 views47 pages

Etextbook 978-0134379760 The Practice of Computing Using Python (3rd Edition) 2024 Scribd Download

Python

Uploaded by

oldersalame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
25 views47 pages

Etextbook 978-0134379760 The Practice of Computing Using Python (3rd Edition) 2024 Scribd Download

Python

Uploaded by

oldersalame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Download the full version of the ebook at

https://ebookmass.com

eTextbook 978-0134379760 The Practice of


Computing Using Python (3rd Edition)

https://ebookmass.com/product/etextbook-978-
0134379760-the-practice-of-computing-using-
python-3rd-edition/

Explore and download more ebook at https://ebookmass.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Introduction To Computing And Problem Solving Using Python


1st Edition E. Balaguruswamy

https://ebookmass.com/product/introduction-to-computing-and-problem-
solving-using-python-1st-edition-e-balaguruswamy/

testbankdeal.com

Bayesian Optimization: Theory and Practice Using Python


1st Edition Peng Liu

https://ebookmass.com/product/bayesian-optimization-theory-and-
practice-using-python-1st-edition-peng-liu/

testbankdeal.com

Introduction to Computing Using Python: An Application


Development Focus, 2nd Edition – Ebook PDF Version

https://ebookmass.com/product/introduction-to-computing-using-python-
an-application-development-focus-2nd-edition-ebook-pdf-version/

testbankdeal.com

Bayesian Optimization : Theory and Practice Using Python


Peng Liu

https://ebookmass.com/product/bayesian-optimization-theory-and-
practice-using-python-peng-liu/

testbankdeal.com
eTextbook 978-1111531102 Ethics in a Computing Culture
(Advanced Topics)

https://ebookmass.com/product/etextbook-978-1111531102-ethics-in-a-
computing-culture-advanced-topics/

testbankdeal.com

eTextbook 978-0134173054 Statistics for Managers Using


Microsoft Excel

https://ebookmass.com/product/etextbook-978-0134173054-statistics-for-
managers-using-microsoft-excel/

testbankdeal.com

eTextbook 978-9351500827 Discovering Statistics Using IBM


SPSS Statistics, 4th Edition

https://ebookmass.com/product/etextbook-978-9351500827-discovering-
statistics-using-ibm-spss-statistics-4th-edition/

testbankdeal.com

eTextbook 978-1305263727 Theory and Practice of Counseling


and Psychotherapy

https://ebookmass.com/product/etextbook-978-1305263727-theory-and-
practice-of-counseling-and-psychotherapy/

testbankdeal.com

Quantum Computing by Practice: Python Programming in the


Cloud with Qiskit and IBM-Q Vladimir Silva

https://ebookmass.com/product/quantum-computing-by-practice-python-
programming-in-the-cloud-with-qiskit-and-ibm-q-vladimir-silva/

testbankdeal.com
THE PRACTICE OF COMPUTING USING

3RD EDITION

WILLIAM RICHARD
PUNCH • ENBODY
C O N T E N T S

VIDEONOTES xxiv
PREFACE xxv
PREFACE TO THE SECOND EDITION xxix
1.0.1 Data Manipulation xxx
1.0.2 Problem Solving and Case Studies xxx
1.0.3 Code Examples xxx
1.0.4 Interactive Sessions xxxi
1.0.5 Exercises and Programming Projects xxxi
1.0.6 Self-Test Exercises xxxi
1.0.7 Programming Tips xxxi

PART 1 THINKING ABOUT COMPUTING 1


Chapter 0 The Study of Computer Science 3
0.1 Why Computer Science? 3
0.1.1 Importance of Computer Science 3
0.1.2 Computer Science Around You 4
0.1.3 Computer “Science” 4
0.1.4 Computer Science Through Computer Programming 6
0.2 The Difficulty and Promise of Programming 6
0.2.1 Difficulty 1: Two Things at Once 6
0.2.2 Difficulty 2: What Is a Good Program? 9
0.2.3 The Promise of a Computer Program 10
0.3 Choosing a Computer Language 11
0.3.1 Different Computer Languages 11
0.3.2 Why Python? 11
0.3.3 Is Python the Best Language? 13
0.4 What Is Computation? 13
0.5 What Is a Computer? 13

vii
viii CONTENTS

0.5.1 Computation in Nature 14


0.5.2 The Human Computer 17
0.6 The Modern, Electronic Computer 18
0.6.1 It’s the Switch! 18
0.6.2 The Transistor 19
0.7 A High-Level Look at a Modern Computer 24
0.8 Representing Data 26
0.8.1 Binary Data 26
0.8.2 Working with Binary 27
0.8.3 Limits 28
0.8.4 Representing Letters 29
0.8.5 Representing Other Data 30
0.8.6 What Does a Number Represent? 31
0.8.7 How to Talk About Quantities of Data 32
0.8.8 How Much Data Is That? 32
0.9 Overview of Coming Chapters 34

P A R T 2 S TA RT I N G T O P R O G R A M 35
Chapter 1 Beginnings 37
1.1 Practice, Practice, Practice 37
1.2 QuickStart, the Circumference Program 38
1.2.1 Examining the Code 40
1.3 An Interactive Session 42
1.4 Parts of a Program 43
1.4.1 Modules 43
1.4.2 Statements and Expressions 43
1.4.3 Whitespace 45
1.4.4 Comments 46
1.4.5 Special Python Elements: Tokens 46
1.4.6 Naming Objects 48
1.4.7 Recommendations on Naming 49
1.5 Variables 49
1.5.1 Variable Creation and Assignment 50
1.6 Objects and Types 53
1.6.1 Numbers 55
1.6.2 Other Built-In Types 57
1.6.3 Object Types: Not Variable Types 58
1.6.4 Constructing New Values 60
CONTENTS ix

1.7 Operators 61
1.7.1 Integer Operators 61
1.7.2 Floating-Point Operators 64
1.7.3 Mixed Operations 64
1.7.4 Order of Operations and Parentheses 65
1.7.5 Augmented Assignment Operators: A Shortcut! 66
1.8 Your First Module, Math 68
1.9 Developing an Algorithm 69
1.9.1 New Rule—Testing 73
1.10 Visual Vignette: Turtle Graphics 74
1.11 What’s Wrong with My Code? 75
Chapter 2 Control 87
2.1 QuickStart Control 87
2.1.1 Selection 87
2.1.2 Booleans for Decisions 89
2.1.3 The if Statement 89
2.1.4 Example: What Lead Is Safe in Basketball? 92
2.1.5 Repetition 96
2.1.6 Example: Finding Perfect Numbers 100
2.1.7 Example: Classifying Numbers 105
2.2 In-Depth Control 109
2.2.1 True and False: Booleans 109
2.2.2 Boolean Variables 110
2.2.3 Relational Operators 110
2.2.4 Boolean Operators 115
2.2.5 Precedence 116
2.2.6 Boolean Operators Example 117
2.2.7 Another Word on Assignments 120
2.2.8 The Selection Statement for Decisions 122
2.2.9 More on Python Decision Statements 122
2.2.10 Repetition: the while Statement 126
2.2.11 Sentinel Loop 136
2.2.12 Summary of Repetition 136
2.2.13 More on the for Statement 137
2.2.14 Nesting 140
2.2.15 Hailstone Sequence Example 142
2.3 Visual Vignette: Plotting Data with Pylab 143
2.3.1 First Plot and Using a List 144
2.3.2 More Interesting Plot: A Sine Wave 145
x CONTENTS

2.4 Computer Science Perspectives: Minimal Universal Computing 147


2.4.1 Minimal Universal Computing 147
2.5 What’s Wrong with My Code? 148
Chapter 3 Algorithms and Program Development 161
3.1 What Is an Algorithm? 161
3.1.1 Example Algorithms 162
3.2 Algorithm Features 163
3.2.1 Algorithm versus Program 163
3.2.2 Qualities of an Algorithm 165
3.2.3 Can We Really Do All That? 167
3.3 What Is a Program? 167
3.3.1 Readability 167
3.3.2 Robust 171
3.3.3 Correctness 172
3.4 Strategies for Program Design 173
3.4.1 Engage and Commit 173
3.4.2 Understand, Then Visualize 174
3.4.3 Think Before You Program 175
3.4.4 Experiment 175
3.4.5 Simplify 175
3.4.6 Stop and Think 177
3.4.7 Relax: Give Yourself a Break 177
3.5 A Simple Example 177
3.5.1 Build the Skeleton 178
3.5.2 Output 178
3.5.3 Input 179
3.5.4 Doing the Calculation 181

P A R T 3 D AT A S T R U C T U R E S A N D F U N C T I O N S 187
Chapter 4 Working with Strings 189
4.1 The String Type 190
4.1.1 The Triple-Quote String 190
4.1.2 Nonprinting Characters 191
4.1.3 String Representation 191
4.1.4 Strings as a Sequence 192
4.1.5 More Indexing and Slicing 193
4.1.6 Strings Are Iterable 198
CONTENTS xi

4.2 String Operations 199


4.2.1 Concatenation (+) and Repetition (*) 199
4.2.2 Determining When + Indicates Addition or
Concatenation? 200
4.2.3 Comparison Operators 201
4.2.4 The in Operator 202
4.2.5 String Collections Are Immutable 203
4.3 A Preview of Functions and Methods 205
4.3.1 A String Method 205
4.3.2 Determining Method Names and Method Arguments 208
4.3.3 String Methods 210
4.3.4 String Functions 210
4.4 Formatted Output for Strings 211
4.4.1 Descriptor Codes 212
4.4.2 Width and Alignment Descriptors 213
4.4.3 Floating-Point Precision Descriptor 214
4.5 Control and Strings 215
4.6 Working with Strings 218
4.6.1 Example: Reordering a Person’s Name 218
4.6.2 Palindromes 220
4.7 More String Formatting 223
4.8 Unicode 226
4.9 A GUI to Check a Palindrome 228
4.10 What’s Wrong with My Code? 232
Chapter 5 Functions—QuickStart 245
5.1 What Is a Function? 245
5.1.1 Why Have Functions? 246
5.2 Python Functions 247
5.3 Flow of Control with Functions 250
5.3.1 Function Flow in Detail 251
5.3.2 Parameter Passing 251
5.3.3 Another Function Example 253
5.3.4 Function Example: Area of a Triangle 254
5.3.5 Functions Calling Functions 258
5.3.6 When to Use a Function 259
5.3.7 What If There Is No Return Statement? 260
5.3.8 What If There Are Multiple Return Statements? 260
xii CONTENTS

5.4 Visual Vignette: Turtle Flag 261


5.5 What’s Wrong with My Code? 262
Chapter 6 Files and Exceptions I 271
6.1 What Is a File? 271
6.2 Accessing Files: Reading Text Files 271
6.2.1 What’s Really Happening? 272
6.3 Accessing Files: Writing Text Files 273
6.4 Reading and Writing Text Files in a Program 274
6.5 File Creation and Overwriting 275
6.5.1 Files and Functions Example: Word Puzzle 276
6.6 First Cut, Handling Errors 282
6.6.1 Error Names 283
6.6.2 The try-except Construct 283
6.6.3 try-except Flow of Control 284
6.6.4 Exception Example 285
6.7 Example: Counting Poker Hands 288
6.7.1 Program to Count Poker Hands 291
6.8 GUI to Count Poker Hands 299
6.8.1 Count Hands Function 300
6.8.2 The Rest of the GUI Code 302
6.9 Error Check Float Input 304
6.10 What’s Wrong with My Code? 304
Chapter 7 Lists and Tuples 311
7.1 What Is a List? 311
7.2 What You Already Know How To Do With Lists 313
7.2.1 Indexing and Slicing 314
7.2.2 Operators 315
7.2.3 Functions 317
7.2.4 List Iteration 318
7.3 Lists Are Different than Strings 319
7.3.1 Lists Are Mutable 319
7.3.2 List Methods 320
7.4 Old and New Friends: Split and Other Functions and Methods 325
7.4.1 Split and Multiple Assignment 325
7.4.2 List to String and Back Again, Using join 326
7.4.3 The Sorted Function 327
CONTENTS xiii

7.5 Working with Some Examples 328


7.5.1 Anagrams 328
7.5.2 Example: File Analysis 334
7.6 Mutable Objects and References 340
7.6.1 Shallow versus Deep Copy 345
7.6.2 Mutable versus Immutable 349
7.7 Tuples 350
7.7.1 Tuples from Lists 352
7.7.2 Why Tuples? 353
7.8 Lists: The Data Structure 353
7.8.1 Example Data Structure 354
7.8.2 Other Example Data Structures 355
7.9 Algorithm Example: U.S. EPA Automobile Mileage Data 355
7.9.1 CSV Module 365
7.10 Visual Vignette: Plotting EPA Data 366
7.11 List Comprehension 368
7.11.1 Comprehensions, Expressions, and the Ternary
Operator 370
7.12 Visual Vignette: More Plotting 370
7.12.1 Pylab Arrays 371
7.12.2 Plotting Trigonometric Functions 373
7.13 GUI to Find Anagrams 374
7.13.1 Function Model 374
7.13.2 Controller 375
7.14 What’s Wrong with My Code? 377
Chapter 8 More on Functions 395
8.1 Scope 395
8.1.1 Arguments, Parameters, and Namespaces 397
8.1.2 Passing Mutable Objects 399
8.1.3 Returning a Complex Object 401
8.1.4 Refactoring evens 403
8.2 Default Values and Parameters as Keywords 404
8.2.1 Example: Default Values and Parameter Keywords 405
8.3 Functions as Objects 407
8.3.1 Function Annotations 408
8.3.2 Docstrings 409
xiv CONTENTS

8.4 Example: Determining a Final Grade 410


8.4.1 The Data 410
8.4.2 The Design 410
8.4.3 Function: weighted_grade 411
8.4.4 Function: parse_line 411
8.4.5 Function: main 412
8.4.6 Example Use 413
8.5 Pass “by Value” or “by Reference” 413
8.6 What’s Wrong with My Code? 414
Chapter 9 Dictionaries and Sets 423
9.1 Dictionaries 423
9.1.1 Dictionary Example 424
9.1.2 Python Dictionaries 425
9.1.3 Dictionary Indexing and Assignment 425
9.1.4 Operators 426
9.1.5 Ordered Dictionaries 431
9.2 Word Count Example 432
9.2.1 Count Words in a String 432
9.2.2 Word Frequency for Gettysburg Address 433
9.2.3 Output and Comments 437
9.3 Periodic Table Example 438
9.3.1 Working with CSV Files 439
9.3.2 Algorithm Overview 441
9.3.3 Functions for Divide and Conquer 441
9.4 Sets 445
9.4.1 History 445
9.4.2 What’s in a Set? 445
9.4.3 Python Sets 446
9.4.4 Methods, Operators, and Functions for Python Sets 447
9.4.5 Set Methods 447
9.5 Set Applications 452
9.5.1 Relationship between Words of Different 452
9.5.2 Output and Comments 456
9.6 Scope: The Full Story 456
9.6.1 Namespaces and Scope 457
9.6.2 Search Rule for Scope 457
9.6.3 Local 457
9.6.4 Global 458
9.6.5 Built-Ins 462
9.6.6 Enclosed 463
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
CONTENTS xv

9.7 Using zip to Create Dictionaries 464


9.8 Dictionary and Set Comprehensions 465
9.9 Visual Vignette: Bar Graph of Word Frequency 466
9.9.1 Getting the Data Right 466
9.9.2 Labels and the xticks Command 467
9.9.3 Plotting 467
9.10 GUI to Compare Files 468
9.10.1 Controller and View 469
9.10.2 Function Model 471
9.11 What’s Wrong with My Code? 473
Chapter 10 More Program Development 483
10.1 Introduction 483
10.2 Divide and Conquer 483
10.2.1 Top-Down Refinement 484
10.3 The Breast Cancer Classifier 484
10.3.1 The Problem 484
10.3.2 The Approach: Classification 485
10.3.3 Training and Testing the Classifier 485
10.3.4 Building the Classifier 485
10.4 Designing the Classifier Algorithm 487
10.4.1 Divided, now Conquer 490
10.4.2 Data Structures 491
10.4.3 File Format 491
10.4.4 The make_training_set Function 492
10.4.5 The make_test_set Function 496
10.4.6 The train_classifier Function 497
10.4.7 train_classifier, Round 2 499
10.4.8 Testing the Classifier on New Data 502
10.4.9 The report_results Function 506
10.5 Running the Classifier on Full Data 508
10.5.1 Training versus Testing 508
10.6 Other Interesting Problems 512
10.6.1 Tag Clouds 512
10.6.2 S&P 500 Predictions 514
10.6.3 Predicting Religion with Flags 517
10.7 GUI to Plot the Stock Market 519
10.7.1 Function Model 519
10.7.2 Controller and View 521
xvi CONTENTS

P A R T 4 C L A S S E S , M A K I N G Y O U R O W N D AT A S T R U C T U R E S
AND ALGORITHMS 527
Chapter 11 Introduction to Classes 529
11.1 QuickStart: Simple Student Class 529
11.2 Object-Oriented Programming 530
11.2.1 Python Is Object-Oriented! 530
11.2.2 Characteristics of OOP 531
11.3 Working with OOP 531
11.3.1 Class and Instance 531
11.4 Working with Classes and Instances 532
11.4.1 Built-In Class and Instance 532
11.4.2 Our First Class 534
11.4.3 Changing Attributes 536
11.4.4 The Special Relationship Between an Instance and
Class: instance-of 537
11.5 Object Methods 540
11.5.1 Using Object Methods 540
11.5.2 Writing Methods 541
11.5.3 The Special Argument self 542
11.5.4 Methods Are the Interface to a Class Instance 544
11.6 Fitting into the Python Class Model 545
11.6.1 Making Programmer-Defined Classes 545
11.6.2 A Student Class 545
11.6.3 Python Standard Methods 546
11.6.4 Now There Are Three: Class Designer, Programmer,
and User 550
11.7 Example: Point Class 551
11.7.1 Construction 553
11.7.2 Distance 553
11.7.3 Summing Two Points 553
11.7.4 Improving the Point Class 554
11.8 Python and OOP 558
11.8.1 Encapsulation 558
11.8.2 Inheritance 559
11.8.3 Polymorphism 559
11.9 Python and Other OOP Languages 559
11.9.1 Public versus Private 559
11.9.2 Indicating Privacy Using Double Underscores (__) 560
CONTENTS xvii

11.9.3 Python’s Philosophy 561


11.9.4 Modifying an Instance 562
11.10 What’s Wrong with My Code? 562
Chapter 12 More on Classes 571
12.1 More About Class Properties 571
12.1.1 Rational Number (Fraction) Class Example 572
12.2 How Does Python Know? 574
12.2.1 Classes, Types, and Introspection 574
12.2.2 Remember Operator Overloading 577
12.3 Creating Your Own Operator Overloading 577
12.3.1 Mapping Operators to Special Methods 578
12.4 Building the Rational Number Class 581
12.4.1 Making the Class 581
12.4.2 Review Fraction Addition 583
12.4.3 Back to Adding Fractions 586
12.4.4 Equality and Reducing Rationals 590
12.4.5 Divide and Conquer at Work 593
12.5 What Doesn’t Work (Yet) 593
12.5.1 Introspection 594
12.5.2 Repairing int + Rational Errors 596
12.6 Inheritance 598
12.6.1 The “Find the Attribute” Game 599
12.6.2 Using Inheritance 602
12.6.3 Example: The Standard Model 603
12.7 What’s Wrong with My Code? 608
Chapter 13 Program Development with Classes 615
13.1 Predator–Prey Problem 615
13.1.1 The Rules 616
13.1.2 Simulation Using Object-Oriented Programming 617
13.2 Classes 617
13.2.1 Island Class 617
13.2.2 Predator and Prey, Kinds of Animals 619
13.2.3 Predator and Prey Classes 622
13.2.4 Object Diagram 623
13.2.5 Filling the Island 623
13.3 Adding Behavior 626
13.3.1 Refinement: Add Movement 626
13.3.2 Refinement: Time Simulation Loop 629
xviii CONTENTS

13.4 Refinement: Eating, Breeding, and Keeping Time 630


13.4.1 Improved Time Loop 631
13.4.2 Breeding 634
13.4.3 Eating 636
13.4.4 The Tick of the Clock 637
13.5 Refinement: How Many Times to Move? 638
13.6 Visual Vignette: Graphing Population Size 639

PART 5 BEING A BETTER PROGRAMMER 643


Chapter 14 Files and Exceptions II 645
14.1 More Details on Files 645
14.1.1 Other File Access Methods, Reading 647
14.1.2 Other File Access Methods, Writing 649
14.1.3 Universal New Line Format 651
14.1.4 Moving Around in a File 652
14.1.5 Closing a File 654
14.1.6 The with Statement 654
14.1.7 Text File Encodings; Unicode 655
14.2 CSV Files 656
14.2.1 CSV Module 657
14.2.2 CSV Reader 658
14.2.3 CSV Writer 659
14.2.4 Example: Update Some Grades 659
14.3 Module: os 661
14.3.1 Directory (Folder) Structure 662
14.3.2 os Module Functions 663
14.3.3 os Module Example 665
14.4 More on Exceptions 667
14.4.1 Basic Exception Handling 668
14.4.2 A Simple Example 669
14.4.3 Events 671
14.4.4 A Philosophy Concerning Exceptions 672
14.5 Exception: else and finally 673
14.5.1 finally and with 673
14.5.2 Example: Refactoring the Reprompting of a File Name 673
14.6 More on Exceptions 675
14.6.1 Raise 675
14.6.2 Create Your Own 676
14.7 Example: Password Manager 677
CONTENTS xix

Chapter 15 Recursion: Another Control Mechanism 687


15.1 What Is Recursion? 687
15.2 Mathematics and Rabbits 689
15.3 Let’s Write Our Own: Reversing a String 692
15.4 How Does Recursion Actually Work? 694
15.4.1 Stack Data Structure 695
15.4.2 Stacks and Function Calls 697
15.4.3 A Better Fibonacci 699
15.5 Recursion in Figures 700
15.5.1 Recursive Tree 700
15.5.2 Sierpinski Triangles 702
15.6 Recursion to Non-recursion 703
15.7 GUI for Turtle Drawing 704
15.7.1 Using Turtle Graphics to Draw 704
15.7.2 Function Model 705
15.7.3 Controller and View 706
Chapter 16 Other Fun Stuff with Python 709
16.1 Numbers 709
16.1.1 Fractions 710
16.1.2 Decimal 714
16.1.3 Complex Numbers 718
16.1.4 Statistics Module 720
16.1.5 Random Numbers 722
16.2 Even More on Functions 724
16.2.1 Having a Varying Number of Parameters 725
16.2.2 Iterators and Generators 728
16.2.3 Other Functional Programming Ideas 733
16.2.4 Some Functional Programming Tools 734
16.2.5 Decorators: Functions Calling Functions 736
16.3 Classes 741
16.3.1 Properties 742
16.3.2 Serializing an Instance: pickle 745
16.4 Other Things in Python 748
16.4.1 Data Types 748
16.4.2 Built-in Modules 748
16.4.3 Modules on the Internet 749
Chapter 17 The End, or Perhaps the Beginning 751
xx CONTENTS

APPENDICES 753
Appendix A Getting and Using Python 753
A.1 About Python 753
A.1.1 History 753
A.1.2 Python 3 753
A.1.3 Python Is Free and Portable 754
A.1.4 Installing Anaconda 756
A.1.5 Starting Our Python IDE: Spyder 756
A.1.6 Working with Python 757
A.1.7 Making a Program 760
A.2 The IPython Console 762
A.2.1 Anatomy of an iPython Session 763
A.2.2 Your Top Three iPython Tips 764
A.2.3 Completion and the Tab Key 764
A.2.4 The ? Character 766
A.2.5 More iPython Tips 766
A.3 Some Conventions for This Book 769
A.3.1 Interactive Code 770
A.3.2 Program: Written Code 770
A.3.3 Combined Program and Output 770
A.4 Summary 771
Appendix B Simple Drawing with Turtle Graphics 773
B.0.1 What Is a Turtle? 773
B.0.2 Motion 775
B.0.3 Drawing 775
B.0.4 Color 777
B.0.5 Drawing with Color 779
B.0.6 Other Commands 781
B.1 Tidbits 783
B.1.1 Reset/Close the Turtle Window 783
Appendix C What’s Wrong with My Code? 785
C.1 It’s Your Fault! 785
C.1.1 Kinds of Errors 785
C.1.2 “Bugs” and Debugging 787
C.2 Debugging 789
C.2.1 Testing for Correctness 789
C.2.2 Probes 789
C.2.3 Debugging with Spyder Example 1 789
C.2.4 Debugging Example 1 Using print() 793
CONTENTS xxi

C.2.5 Debugging with Spyder Example 2 794


C.2.6 More Debugging Tips 802
C.3 More about Testing 803
C.3.1 Testing Is Hard! 804
C.3.2 Importance of Testing 805
C.3.3 Other Kinds of Testing 805
C.4 What’s Wrong with My Code? 805
C.4.1 Chapter 1: Beginnings 805
C.4.2 Chapter 2: Control 807
C.4.3 Chapter 4: Strings 808
C.4.4 Chapter 5: Functions 809
C.4.5 Chapter 6: Files and Exceptions 810
C.4.6 Chapter 7: Lists and Tuples 811
C.4.7 Chapter 8: More Functions 812
C.4.8 Chapter 9: Dictionaries 813
C.4.9 Chapter 11: Classes I 814
C.4.10 Chapter 12: Classes II 815
Appendix D Pylab: A Plotting and Numeric Tool 817
D.1 Plotting 817
D.2 Working with pylab 818
D.2.1 Plot Command 818
D.2.2 Colors, Marks, and Lines 819
D.2.3 Generating X-Values 819
D.2.4 Plot Properties 820
D.2.5 Tick Labels 821
D.2.6 Legend 822
D.2.7 Bar Graphs 824
D.2.8 Histograms 824
D.2.9 Pie Charts 825
D.2.10 How Powerful Is pylab? 826
Appendix E Quick Introduction to Web-based User Interfaces 829
E.0.1 MVC Architecture 830
E.1 Flask 830
E.2 QuickStart Flask, Hello World 831
E.2.1 What Just Happened? 832
E.2.2 Multiple Routes 833
E.2.3 Stacked Routes, Passing Address Arguments 835
E.3 Serving Up Real HTML Pages 836
E.3.1 A Little Bit of HTML 836
E.3.2 HTML Tags 836
xxii CONTENTS

E.3.3 Flask Returning Web Pages 838


E.3.4 Getting Arguments into Our Web Pages 839
E.4 Active Web Pages 841
E.4.1 Forms in wtforms 841
E.4.2 A Good Example Goes a Long Way 842
E.4.3 Many Fields Example 847
E.5 Displaying and Updating Images 852
E.6 Odds and Ends 857
Appendix F Table of UTF-8 One Byte Encodings 859
Appendix G Precedence 861
Appendix H Naming Conventions 863
H.1 Python Style Elements 864
H.2 Naming Conventions 864
H.2.1 Our Added Naming Conventions 864
H.3 Other Python Conventions 865
Appendix I Check Yourself Solutions 867
I.1 Chapter 1 867
Variables and Assignment 867
Types and Operators 867
I.2 Chapter 2 868
Basic Control Check 868
Loop Control Check 868
More Control Check 868
for and range Check 868
I.3 Chapter 4 869
Slicing Check 869
String Comparison Check 869
I.4 Chapter 5 869
Simple Functions Check 869
I.5 Chapter 6 869
Exception Check 869
Function Practice with Strings 870
I.6 Chapter 7 870
Basic Lists Check 870
Lists and Strings Check 870
Mutable List Check 870
CONTENTS xxiii

I.7 Chapter 8 870


Passing Mutables Check 870
More on Functions Check 871
I.8 Chapter 9 871
Dictionary Check 871
Set Check 871
I.9 Chapter 11 871
Basic Classes Check 871
Defining Special Methods 871
I.10 Chapter 12 872
Check Defining Your Own Operators 872
I.11 Chapter 14 872
Basic File Operations 872
Basic Exception Control 872
INDEX 873
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
V I D E O N O T E S

VideoNote 0.1 Getting Python 13
VideoNote 1.1 Simple Arithmetic 64
VideoNote 1.2 Solving Your First Problem 73
VideoNote 2.1 Simple Control 96
VideoNote 2.2 Nested Control 140
VideoNote 3.1 Algorithm Decomposition 177
VideoNote 3.2 Algorithm Development 185
VideoNote 4.1 Playing with Strings 210
VideoNote 4.2 String Formatting 214
VideoNote 5.1 Simple Functions 251
VideoNote 5.2 Problem Design Using Functions 261
VideoNote 6.1 Reading Files 272
VideoNote 6.2 Simple Exception Handling 285
VideoNote 7.1 List Operations 327
VideoNote 7.2 List Application 349
VideoNote 8.1 More on Parameters 405
VideoNote 9.1 Using a Dictionary 437
VideoNote 9.2 More Dictionaries 465
VideoNote 10.1 Program Development: Tag Cloud 512
VideoNote 11.1 Designing a Class 545
VideoNote 11.2 Improving a Class 554
VideoNote 12.1 Augmenting a Class 593
VideoNote 12.2 Create a Class 596
VideoNote 13.1 Improve Simulation 623
VideoNote 14.1 Dictionary Exceptions 669
VideoNote 15.1 Recursion 692
VideoNote 16.1 Properties 742

xxiv
P R E F A C E

A FIRST COURSE IN COMPUTER SCIENCE IS ABOUT A NEW WAY OF SOLVING PROBLEMS
computationally. Our goal is that after the course, students when presented with a problem
will think, “Hey, I can write a program to do that!”
The teaching of problem solving is inexorably intertwined with the computer language
used. Thus, the choice of language for this first course is very important. We have cho-
sen Python as the introductory language for beginning programming students—majors and
non-majors alike—based on our combined 55 years of experience teaching undergradu-
ate introductory computer science at Michigan State University. Having taught the course
in Pascal, C/C++, and now Python, we know that an introductory programming language
should have two characteristics. First, it should be relatively simple to learn. Python’s sim-
plicity, powerful built-in data structures, and advanced control constructs allow students to
focus more on problem solving and less on language issues. Second, it should be practical.
Python supports learning not only fundamental programming issues such as typical pro-
gramming constructs, a fundamental object-oriented approach, common data structures,
and so on, but also more complex computing issues such as threads and regular expres-
sions. Finally, Python is “industrial strength” forming the backbone of companies such as
YouTube, DropBox, Industrial Light and Magic, and many others.
We emphasize both the fundamental issues of programming and practicality by focus-
ing on data manipulation and analysis as a theme—allowing students to work on real prob-
lems using either publicly available data sets from various Internet sources or self-generated
data sets from their own work and interests. We also emphasize the development of pro-
grams, providing multiple, worked out, examples, and three entire chapters for detailed de-
sign and implementation of programs. As part of this one-semester course, our students
have analyzed breast cancer data, catalogued movie actor relationships, predicted disrup-
tions of satellites from solar storms, and completed many other data analysis problems. We
have also found that concepts learned in a Python CS1 course transitioned to a CS2 C++
course with little or no impact on either the class material or the students.

xxv
Random documents with unrelated
content Scribd suggests to you:
The Fool’s Paradise

THERE is an old picture-shop in the West-Central district of London,


notable for the grime of its canvases, in the window of which there is to
be seen at this moment—unless a confiding purchaser has just borne it
off—a girl’s head and bust by some very indifferent Dutch hand, under
which is printed on the frame the startling and courageous legend,
“The Coral Necklace. By Jan Vermeer, of Delft.”
Of course the ascription is inaccurate. Were it accurate and the
picture worthy of it, this little shop would be the Mecca of the first art
experts of Europe and America, and the dealer would be in the way to
affluence; nor does the picture’s present owner probably believe in it.
But what of some previous possessor who did believe in it—some
simple soul who was genuinely convinced that upon his wall hung a
portrait by this rarest and most exquisite and radiant of Dutch masters?
Do you not envy him his easy credence, his want of fastidious taste? I
do.
A little while ago there was a lawsuit—indeed a series of lawsuits
—all turning upon the collection of porcelain left by a wealthy Regent
Street merchant, whose hobby was the acquisition of china. As a man
in the prime of life he had been a good judge; but as he grew older and
his brain weakened his sense of discrimination left him, and it was
discovered that his later purchases, so far from being the priceless
examples of Dresden and other ware which they were thought to be,
were all-but worthless. This naturally was a grief and disappointment to
the heirs who were to benefit from the sale; but for us to be sorry for
him is as foolish a waste of sympathy as I know. For though there he
sat, that old amateur of ceramics, surrounded by the mediocre, yet in
that he believed it to be the choicest he was enviable. That belief is the
heart of the case, since it is not what things really are, but what one
thinks things to be, that is the important matter.
Truth has a slightly different expression for every one. To this aged
connoisseur with his decaying faculties her expression was falseness
itself, could he have scrutinised it with intelligence, but to his dim eyes
it looked like the finest candour, and therefore it was the finest candour.
He sunned himself in it, and passing his hands lovingly over the
spurious shepherdesses was happy. The point is that he could not
have been happier had the porcelain been truly of the rarest and most
wonderful.
I hope it will never be my fortune to visit a picture collector whose
walls are hung entirely with obvious copies which he believes to be
original, and flagrant daubs which he thinks masterpieces—a collector
in short who relies only on the posthumous activity of artists; yet if it is,
I hope I shall know how to control myself when he displays his
treasures. But of one thing I am certain: that no matter how I may
suffer from the concealment of my true feelings as an art lover, I shall
experience a genuine affection for my host, and a genuine delight in
his transparent, credible nature. Surely the people who live in fool’s
paradises are the salt of the earth. The man who says of a fine thing,
“A fine thing and my own,” I can admire, but not necessarily with
warmth; the man (he is very common) who says of a fine thing, “A poor
thing, but my own,” I have very little use for; but the man who says of a
poor thing, “A fine thing and my own,” him I admire cordially, and could
almost embrace.
But about this Vermeer. I cannot get it out of my head, for Vermeer
is a painter of whom, as you know, I have made some study, and the
thought of any one really sitting down excitedly with this grotesquely
misattributed picture in his room, reading the lying label without a
qualm, even with pride, scanning the commonplace paint with no
twinge of dubiety—it is this thought which beats me. The man who
confidently had the legend printed on the frame must indeed have
been a simpleton beyond appraisement—the very briniest salt of the
soil. For consider: the copyists, the forgers, may do credible things with
Corot, even with Raphael. Every day they are writing David Cox’s
signature on old water-colours; false ascriptions are the life-blood of
too many firms. That is true. But Vermeer—there is only one Vermeer!
and yet some man could know enough about Vermeer to wish to have
something by him on his wall (modest wish—there are not, as I have
been saying, forty known Vermeer canvases in the world), and then be
satisfied with this! If ever I longed to meet a freak it is he—not only to
examine his bumps, but to abase myself before him. For there is a true
philosopher, a really wise man, if you like.
Meanwhile I wish some dramatist with an eye to quaint character, if
there be such a one left, would set upon the stage for us a paradisiacal
fool such as this—a simple kind of enthusiast without a shred of critical
faculty or a drop of guile, whom we might see amiably fondling his
geese and deeming them swans. That would give me, for one, great
pleasure. Lamb, in his Captain Jackson, approached and skirted the
type, but Vermeer’s “Coral Necklace” would not have attracted that
engaging creature. If Anatole France were a dramatist and would
return to the gentle, smiling mood in which he thought out and built up
his Sylvestre Bonnard, he might give us this collector. I can think of no
one else; and even he would probably be a little too much inclined to
whip something on his back, such a castigator and ridiculer as he is.
Consolers of Genius

I HAVE just added another famous dog to my list. It was a good list
before, but it is now richer. It included Matthew Arnold’s Geist and Max
and Kaiser, George Meredith’s Islet, Cowper’s Beau, Newton’s
Diamond, Mrs. Browning’s Flush, Mr. Lehmann’s Rufus, all Dr. John
Brown’s many friends, Scott’s deerhounds, Mortimer Collins’s St.
Bernards, Pope’s spaniel. I remember only these as I write, but of
course there are many others. And to this company enters now
“Pomero.”
Landor’s “Pomero” came to him late in life—in the early ’forties—by
which time the old man—he was then nearing seventy but had twenty
fairly stormy years left—had settled again in England, his wife and
family and most of his sympathies being far away in Italy. At Bath he
then lived, making occasional visits to Gore House, and varying the
composition of exquisite prose and tender felicitous verse with quarrels
and tempests and tempests and reconciliations and tempests and
lawsuits. Such then was the possessor of “Pomero”—or, as he would
probably have called himself, the proud possession of “Pomero”—of
whom such glimpses as I have had come to me in scraps of letters
quoted by Forster in his Life of this noble, troubled, impossible,
glorious creature.
Here is one, written by Landor at Warwick, when away from home,
or what stood for home at that period—1844. Pomero had only just
arrived from Fiesole; and it is worth remarking that had Landor lived to-
day no such fortune would ever have been his, for never would he
have survived such explosions of rage as the modern six months’
quarantine for imported dogs would have brought on him. (Think of him
expressing his views to the custom-house officer at Dover!) “Daily,” he
wrote, “do I think of Bath and Pomero. I fancy him lying on the narrow
window-sill, and watching the good people go to church. He has not
yet made up his mind between the Anglican and Roman Catholic; but I
hope he will continue in the faith of his forefathers, if it will make him
happier.”
Pomero, I should say, was a Pomeranian; but let me quote Sir
Sidney Colvin’s charming sentences upon both man and dog. “With
‘Pomero’ Landor would prattle in English and Italian as affectionately
as a mother with a child. Pomero was his darling, the wisest and most
beautiful of his race; Pomero had the brightest eyes and the most
wonderful yaller tail ever seen. Sometimes it was Landor’s humour to
quote Pomero in speech and writing as a kind of sagacious elder
brother whose opinion had to be consulted on all subjects before he
would deliver his own. This creature accompanied his master wherever
he went, barking ‘not fiercely but familiarly’ at friend and stranger, and
when they came in would either station himself upon his master’s head
to watch the people passing in the street, or else lie curled up in his
basket until Landor, in talk with some visitor, began to laugh, and his
laugh to grow and grow, when Pomero would spring up and leap upon
and fume about him, barking and screaming for sympathy until the
whole street resounded. The two together, master and dog, were for
years to be encountered daily on their walks about Bath and its vicinity,
and there are many who perfectly well remember them; the majestic
old man, looking not a whit the less impressive for his rusty and dusty
brown suit, his bulging boots, his rumpled linen, or his battered hat;
and his noisy, soft-haired, quick-glancing, inseparable companion.”
Landseer, one feels, should have painted them: Dignity and
Fidelity, Unreason and Understanding, Lion and Pomeranian. Since he
did not, we must go to Forster’s extracts from the letters to fill in the
picture. Another passage, also in 1844: “Pomero was on my knee
when your letter came. He is now looking out of the window; a sad
male gossip, as I often tell him. I dare not take him with me to London.
He would most certainly be stolen, and I would rather lose Ipsley or
Llanthony. The people of the house love him like a child, and declare
he is as sensible as a Christian. He not only is as sensible, but much
more Christian than some of those who have lately brought strife and
contention into the Church.”
Again: “Pomero is sitting in a state of contemplation, with his nose
before the fire. He twinkles his ears and his feathery tail at your
salutation. He now licks his lips and turns round, which means ‘Return
mine.’ The easterly wind has an evident effect upon his nerves. Last
evening I took him to hear Luisina de Sodre play and sing. She is my
friend the Countess de Molande’s granddaughter and daughter of De
Sodre, Minister of Brazil to the Pope a few years ago. Pomero was
deeply affected, and lay close to the pedal on her gown, singing in a
great variety of tones, not always in time. It is unfortunate that he
always will take a part where there is music, for he sings even worse
than I do.”
So far the letters have been to Forster. Here is a passage from one
to Landor’s sisters, also in 1844: “Let me congratulate you on the
accident that deprives you of your carriage-horses. Next to servants,
horses are the greatest trouble in life. Dogs are blessings, true
blessings. Pomero, who sends his love, is the comfort of my solitude
and the delight of my life. He is quite a public character here in Bath.
Everybody knows him and salutes him. He barks aloud at all familiarly,
not fiercely. He takes equal liberties with his fellow-creatures, if indeed
dogs are more his fellow-creatures than I am. I think it was St. Francis
de Sales who called birds and quadrupeds his sisters and brothers.
Few saints have been so good-tempered, and not many so wise.”
For twelve years Pomero lived to make his master (his servant)
happy or less unhappy, and then he died. That is the tragic thing—the
brief life of these loyal devotees. It is not right, not fair, that so much
love and energy should so quickly pass away. Many sensitive persons
refuse for this reason to keep dogs at all. That, I think, is going too far,
but I can understand it. Life at its longest for a human being is so brief
and so fraught with disappointment and disillusion that, at least, one
feels, the span of the most faithful and satisfying friends that man
knows might have been made commensurate.... Pomero, as I have
said, was Landor’s for twelve years, and then he died. Writing to
Forster on the 10th of March, 1856, the old man—he was eighty-one—
tells the news: “Pomero, dear Pomero, died this evening at about four
o’clock. I have been able to think of nothing else....”
A few days later he wrote again: “Everybody in this house grieves
for Pomero. The cat lies day and night upon his grave, and I will not
disturb the kind creature, though I want to plant some violets upon it,
and to have his epitaph placed around his little urn:—
O urna! nunquam sis tuo eruta hortulo:
Cor intus est fidele, nam cor est canis.
Vale, hortule! aeternumque, Pomero! vale.
Sed, si datur, nostri memor.”

Eighty-one though he was, Landor had still nine years before him
—years of trouble, and fury, and exile. Not till 1864 did he meet
Pomero again.
Pomero had been Landor’s confidant and delight for five years
when, in 1849, there came to one of the most illustrious of his
contemporaries—and a critic of the world not less impatient than
himself, but how different!—a similar companion. It was not, it is true, a
Pomeranian, but a dog none the less.
The news was thus broken by one of the most remarkable women
of all time to, as it happens, the same friend who had been first told of
the arrival of Pomero. “O Lord!” she writes, wilfully, characteristically as
ever, “O Lord! I forgot to tell you I have got a little dog, and Mr. C. has
accepted it with an amiability! To be sure, when he comes down
gloomy in the morning, or comes in wearied from his walk, the
infatuated little beast dances round him on his hind legs as I ought to
do and can’t; and he feels flattered and surprised by such unwonted
capers to his honour and glory.” So wrote Jane Welsh Carlyle to John
Forster, on the 11th of December, 1849.
Sixteen years later the writer of that letter died suddenly in her
carriage in Hyde Park, and thus ended a life of heroic vivacity. Her
husband, deprived for ever of the power of sustained work, difficult
enough when he had her service and intelligence within call, spent a
few months in his early bereavement in collecting and arranging and
annotating her marvellous correspondence; and one does not envy
him his feelings as he did it. Coming to the note to Forster which I have
quoted, he thus introduced it: “Poor little Nero, the dog, must have
come this winter, or ‘Fall’ (1849)? Railway guard (from Dilberoglue,
Manchester) brought him in one evening late. A little Cuban (Maltese?
and otherwise mongrel) shock, mostly white—a most affectionate,
lively little dog, otherwise of small merit, and little or no training. Much
innocent sport there arose out of him; much quizzical ingenuous
preparation of me for admitting of him: ‘My dear, it’s borne in upon my
mind that I’m to have a dog,’ etc., etc., and with such a look and style!
We had many walks together, he and I, for the next ten years; a great
deal of small traffic, poor little animal, so loyal, so loving, so naïve and
true with what of dim intellect he had! Once, perhaps in his third year
here, he came pattering upstairs to my garret; scratched duly, was let
in, and brought me (literally) the ‘gift of a horse’ (which I had talked of
needing)! Brought me, to wit, a letter hung to his neck, inclosing on a
saddler’s card the picture of a horse, and adjoined to it a cheque for
£50—full half of some poor legacy which had fallen to her! Can I ever
forget such a thing? I was not slave enough to take the money; and got
a horse next year, on the common terms—but all Potosi, and the
diggings new and old, had not in them, as I now feel, so rich a gift!”
These three volumes of Mrs. Carlyle’s indomitably gay
correspondence, laughing at her crosses, making light of her
disappointments, extracting whatever of merriment or sunshine was
possible, and never with any trace of self-commendation or
consciousness of heroism: and a woman too who must have known
that, given a fair chance, which she never had, she would have shone
in her own way with hardly less brilliancy than her bear; who must have
known she was worth petting, and considering, and adoring rightly—
these three volumes of brilliant good-humour against odds, with the
dour, intolerant, solitary widower re-living the irrecoverable past as he
read them over and edited them, counting his lost opportunities on
every page, are surely as tragic a work as literature knows. But Nero is
pawing at the desk. The note continues: “Poor Nero’s last good days
were with us at Aberdour, in 1859. Twice or thrice I flung him into the
sea there, which he didn’t at all like; and in consequence of which he
even ceased to follow me at bathing time, the very strongest measure
he could take—or pretend to take. For two or three mornings
accordingly I had seen nothing of Nero, but the third or fourth morning,
on striking out to swim a few yards, I heard gradually a kind of
swashing behind me; looking back, it was Nero out on voluntary
humble partnership—ready to swim with me to Edinburgh, or to the
world’s end, if I liked.”
Pomero, as I said, lived for twelve years with his whirlwind adorer.
Nero had a shorter life with that strange Scotch couple only by a few
months. This is the end of Carlyle’s note: “Fife had done his mistress,
and still more him, a great deal of good. But, alas, in Cook’s grounds
here, within a month or two a butcher’s cart (in her very sight) ran over
him neck and lungs: all winter he wheezed and suffered; ‘Feb. 1st,
1860,’ he died (prussic acid, and the doctor obliged at last!). I could not
have believed my grief then and since would have been the twentieth
part of what it was—nay, that the want of him would have been to me
other than a riddance. Our last midnight walk together (for he insisted
on trying to come), Jan. 31st, is still painful to my thought. ‘Little dim,
white speck, of Life, of Love, Fidelity, and Feeling, girdled by the
Darkness as of Night Eternal!’ Her tears were passionate and bitter,
but repressed themselves, as was fit, I think, the first day. Top of the
garden, by her direction, Nero was put under ground. A small stone
tablet with date she also got, which, broken by careless servants, is
still there—a little protected now.”
It is there still, but few visitors to that gloomy Chelsea house,
where two geniuses, a man and woman, failed sufficiently to subdue
and blend their individualities for so many years, ever walk down the
garden to see it. Underneath are the remains of one who could neither
read nor write nor frame systems, but who lived the only successful life
of the three.
An American Hero

WHO was William Allen Richardson? I once asked. Since the


publication of the volume of essays in which the problem was so
tiresomely propounded many letters have reached me, each with its
own solution. All are different; and their differences show how
important it was that a warrior for truth should come forward and fling
the question in the world’s face. For the growth of legend and myth that
has been endangering the fame of this noble deviser of an orange-
hearted rose was becoming too rampant. Let me, therefore, who asked
the question, now answer it; for I know. By dint of careful pruning I
have removed the apocryphal, and the truth remains. William Allen
Richardson was—
But you must permit me first to narrate some of the experiences of
an essayist who has the temerity to indulge in interrogation marks.
The first letter I received—almost immediately after the publication
of the book—gave so lucid an account of William Allen Richardson that
I began to think I had made too much of the mystery. “Do you really
want to know about William Allen Richardson?” it began; and then this
story was told: “William Allen Richardson and his wife loved roses, and
the ambition of their lives was to raise an orange-coloured rose. At last
they succeeded, and they called the treasure ‘William and Ellen
Richardson,’ a rather cumbersome title, but meaning much to these
two. Alas, the printer would have none of this sentiment—hence
‘William Allen Richardson.’”
I cannot say that this narrative satisfied me; but there was nothing
in it to make one violently sceptical. Why should not William and Ellen
have lived this idyllic rose-growing life? Why should not their names
have been thus intertwined for ever, even if a little ungallantly? I had
seen barges on the Thames called “William and Ellen,” I was sure; why
not roses? I therefore went about saying that I now knew the whole
history of William Allen Richardson, and the story was not doubted.
But then arrived an anonymous post card with the Paddington
postmark: “I am of no importance and my brother is of no importance,
but William Allen Richardson was the brother of my brother’s handy
man. (At least he said so.)” What of William and Ellen after that? For
the time, at any rate, the narrative of their fragrant union passed from
my repertory.
That post card will give you an idea of the lightness with which this
matter can be approached. I do not mean that the communication in
itself is frivolous, for, though easy in tone, it yet states the case briefly
and clearly; the lightness that I complain of is in the attitude of the
writer’s brother towards this tremendous problem. Here he was, with
his brother’s handy man claiming to be the own brother of the great
William Allen Richardson, and yet doing no more (apparently) than
treating it as a myth—never investigating—never, in short, really
caring. Now if I had a brother whose handy man was—— But this is
boasting, self-approval; and complacent people conscious of their own
rectitude rarely get at the truth.
Other correspondents followed, all strangers to me, and each with
a pet theory. One had it that William Allen Richardson had been
gardener to a rose-loving duke. Another, that he was a Scotchman who
had gone to France, to manage the Ducher nursery. Another, that he
was the American editor of a horticultural journal. Then came another
more circumstantial story, from a lady in Yorkshire. “I was taught by a
dear old country vicar (himself an enthusiastic rose-grower and close
friend of Dean Hole) that W. A. Richardson was one of the Quaker firm
of Richardson, who had a place near Newry in the north of Ireland.”
This so chimed in with my own Quakerish suspicions, as expressed in
the original essay, that I was inclined to think we might really be at
home at last; but meanwhile an American missive was on its way from
Louisville, Kentucky, and when it arrived I saw at once that here was
Veritas naked and unashamed.
A certain statesman who had taken much interest in the matter will
be amused to read the Louisville communication. “I have often,” he
wrote to me, “wondered, and occasionally asked, who W. A. R. was,
and have been at times impatient that people should be content to live
on without knowing. Now I would almost rather not know, having been
disappointed for so long.” He went on to say that he suspected
W. A. R. to be an American. Well, he was right. Sagacious and far-
seeing as ever, he now has another opportunity of pointing to a fulfilled
conjecture; for there is no doubt (since I have had corroboration from
another transatlantic source) that the following letter is gospel.
The writer, Mr. W. R. Belknap, roundly states himself to be William
Allen Richardson’s nephew. He continues: “William Allen Richardson
was born in New Orleans, Louisiana, on February 20, 1819. When he
was but two years old, his father moved to Lexington, Kentucky, where
he resided until his death, in October, 1892. William Allen Richardson
married Miss Mary Short, daughter of Charles Wilkins Short, the
botanist, who pursued his favourite studies of botany and horticulture
at his country place, Hayfield, some five miles south-west of Louisville.
With this congenial companionship, Mr. W. A. Richardson established
himself in an adjoining place, Ivywood, and became much interested in
the cultivation and propagation of roses. He imported a good many,
and in this way became acquainted by correspondence with Madame
Ducher (or she may have been called Veuve Ducher), at Lyons,
France, who was especially interested in a rose which he sent her of a
pale yellow colour, and she wrote Mr. Richardson that she had a sport
from this rose in her own garden, which, if successful in propagation,
she would name for him; hence the name which has interested you as
applying to the beautiful copperish-yellow rose.... Mr. Richardson lived
until 1892 in his country home near here, and would have enjoyed, if
he might have foreseen, the interest which his namesake has
aroused....”
And now we know. The secret is out, and the rose will smell no
less sweet for it, nor climb less carelessly, nor refresh the eye less
graciously. But I adjure America to be more proud of this feather in her
cap. I do not suggest that William Allen Richardson should have a
monument, for he has one in every right garden more beautiful than
marble and very likely more enduring than bronze; but his name should
be so deeply cut upon the roll of honour that no one need ever have to
ask my question again.
But what a blow to that foolish romantic anecdote about Ellen!
Mr. Hastings

HAD it not been for the trenchant pen of his cousin, Anthony Ashley
Cooper, the first Lord Shaftesbury, we should know nothing of Mr.
Hastings; but as it happens, a portrait of Mr. Hastings being painted,
the Earl was amused to pit his pen against the brush of the artist and
append the result to the picture. So that Mr. Hastings used to hang on
the wall at Wimborne St. Giles’s, near Cranbourn, in Dorset (one of the
Shaftesbury seats), doubly limned. Where he is to-day I know not; but
the Earl’s words remain and are accessible. I take them in the form
which follows from the “Connoisseur” for Thursday, 14 August, 1755,
and I may in passing say that in turning over the leaves of this leisurely
little breakfast-table companion it was not a little disquieting to think
what good papers they had in London one hundred and fifty-six years
ago, before the days of amalgamation.
As to the portrait of Mr. Hastings, I have seen an engraving of it in
one of Hutchins’s Dorsetshire books, and it is a crude enough thing—a
little odd old man, with a pointed beard, sharp eyes, and a long staff in
his right hand—not so much a patriarch’s staff as a surveyor’s pole.
Nothing in it to suggest that he loved spaniels, for example, or knew
the best thing to do with a disused pulpit. Yet he did.
Now for the shrewd and cryptic statesman who first made the
admirable remark (since given to others) that “Wise men are of but one
religion,” adding to the lady who inquired what that was, “Wise men
never tell.” He begins thus: “In the year 1638 lived Mr. Hastings; by his
quality son, brother, and uncle to the Earls of Huntingdon. He was ...
low, very strong, and very active; of a reddish flaxen hair. His clothes
always green cloth, and never all worth (when new) five pounds. His
house was perfectly of the old fashion, in the midst of a large Park well
stocked with deer; and near the house rabbits to serve his kitchen;
many fish-ponds; great store of wood and timber; a bowling-green in it,
long but narrow, full of high ridges, it being never levell’d since it was
plough’d. They used round sand bowls; and it had a banqueting house
like a stand, built in a tree.”—The mansion no longer stands in its
entirety. It was pulled down, with the exception of two wings, at the
beginning of the last century. One of these wings, however, contains
the kitchen, and gives ample evidence of the hospitality which, as we
shall see, was practised there.
Mr. Hastings “kept all manner of sport hounds, that ran buck, fox,
hare, otter, and badger. And hawks, long and short winged. He had all
sorts of nets for fish. He had a walk in the New Forest, and the manor
of Christ Church. This last supplied him with red deer, sea and river
fish. And indeed all his neighbours’ grounds and royalties were free to
him, who bestowed all his time on these sports, but what he borrowed
to caress his neighbours’ wives and daughters; there being not a
woman in all his walks, of the degree of a yeoman’s wife or under, and
under the age of forty, but it was extremely her fault if he was not
intimately acquainted with her. This made him very popular; always
speaking kindly to the husband, father, or brother, who was, to boot,
very welcome to his house whenever he came.” (“Popular” is a good
word, so good, in this connexion, that one has to pause a little to
savour it.) Thinking of him thus occupied, if ever, you would say, an old,
whimsical bachelor was portrayed, he is portrayed here. But you would
be wrong, for Mr. Hastings was married. It was his wife who brought
him Woodlands, and she did not die till 1638, when he was eighty-
seven. They had, moreover, a son. Lord Shaftesbury, who was
something of a cynic, suppressed this detail. It amused him to
eliminate Mrs. Hastings.
His lordship goes on to describe the free-and-easy (and, on the
face of it, wifeless) character of Mr. Hastings’ house. “A house not so
neatly kept as to shame him or his dirty shoes: the great hall strow’d
with marrow bones, full of hawks’ perches, hounds, spaniels, and
terriers; the upper side of the hall hung with foxskins of this and the last
year’s killing; here and there a polecat intermixt; game-keepers’ and
hunters’ poles in great abundance. The parlour was a large room as
properly furnished. On a great hearth paved with brick lay some
terriers, and the choicest hounds and spaniels. Seldom but two of the
great chairs had litters of young cats in them, which were not to be
disturbed, he having always three or four attending him at dinner, and
a little white stick of fourteen inches lying by his trencher, that he might
defend such meat as he had no mind to part with to them.” (One does
not feel much room for a Mrs. Hastings here. She kept her own
quarters, I imagine.)
I should like to see a picture of old Mr. Hastings at his meals—with
all his animals about him and his hand holding his little white stick.
Steinlen, who designed that fine poster for Nestlé’s milk—the cats
clamouring for the little girl’s breakfast—could draw the animals; but for
the little old gentleman, with his red hair and green clothes and great
age, you would want a Dendy Sadler or Stacy Marks.
The description of the house continues: “The windows (which were
very large) served for places to lay his arrows, cross-bows, stone-
bows, and other such like accoutrements. The corners of the room full
of the best-chose hunting and hawking poles. An oyster table at the
lower end, which was of constant use twice a day all the year round,
for he never failed to eat oysters, before dinner and supper, through all
seasons; the neighbouring town of Poole supply’d him with them. The
upper part of the room had two small tables and a desk, on the one
side of which was a Church Bible, and on the other the Book of
Martyrs. On the tables were hawks’ hoods, bells, and such like; two or
three old green hats, with their crowns thrust in so as to hold ten or a
dozen eggs, which were of a pheasant kind of poultry, he took much
care of and fed himself. Tables, dice, cards, and boxes were not
wanting. In the hole of the desk were store of tobacco-pipes that had
been used.”—Mr. Hastings must have been one of the earliest of the
smokers, since he was born as far back as 1551.
“On one side of this end of the room was the door of a closet
wherein stood the strong beer and the wine, which never came thence
but in single glasses, that being the rule of the house exactly observ’d.
For he never exceeded in drink or permitted it.” In another account of
Mr. Hastings his iron rule with regard to liquor was suggested to have
caused much unhappiness to his guests. And I must admit that there
seems to be something wrong in a house where you may not see the
bottle, much less handle it. But, on the other hand, it is such
unexpected whims and unreasonableness that are the life-blood of
these old originals. Any dull creature can be reasonable.
Now comes a priceless touch: “On the other side was the door into
an old chapel, not used for devotion. The pulpit, as the safest place,
was never wanting of a cold chine of beef, venison pasty, gammon of
bacon, or great apple pye, with thick crust, extremely baked.” “Never
wanting” is splendid. One longs to know more of the service of this
house—of the cook who fell in so complacently with such a master’s
needs and ways. “Never wanting!”
Like Bishop Corbet’s fairies, Mr. Hastings was of the old
profession. “His table cost him not much, though it was good to eat at.
His sports supplied all but beef and mutton, except Fridays, when he
had the best salt fish (as well as other fish) he could get; and was the
day his neighbours of best quality most visited him. He never wanted a
London pudding, and always sung it in with ‘My part lies therein-a.’”
“He always sung it in.” Here lies an old custom indeed, dead, I
suppose, as Mr. Hastings himself and all his spaniels and kittens. Who
sings in a pudding to-day? And, indeed, what pudding is worth singing
in? Not the rice which I had yesterday, at any rate.
And so we come to the end: “He was well-natured but soon
angry.... He lived to be a hundred; never lost his eyesight, but always
wrote and read without spectacles; and got on horseback without help.
Until past four score he rode to the death of a stag as well as any.” He
was buried in Horton church in 1650 at the age of ninety and nine, and
England will never know anything like him again. Gone are such
spacious days and ways; gone such idiosyncrasy and humour. Only, I
imagine, on the bowling-greens are Mr. Hastings’ characteristics to be
still observed; for our old devotees of that leisurely contest, that most
pacific warfare, cannot in their attitudes, gestures and expressions
differ much from the Squire of Woodlands. Just so did he, three
hundred years ago, contort and twist his frame, as he watched his
bowl’s career and bent every nerve and fibre to influence it to swerve
at the last dying moment on the jack between his two rivals. These
elemental anxieties do not change.
Thoughts on Tan

IN my search for the curious, which I hope that nothing will ever
satiate, I came recently upon this advertisement at the end of a not too
respectable comic paper:—

HANDSOME MEN are slightly sun-burnt.


“Sunbronze” gives this tint. Harmless.
Detection impossible. Makes men really
handsome. Society Lady writes:
—“Sunbronze is wonderful, charming, and
genuine.” 1s. 1½d., etc.

When I read it first I laughed. Then I cut it out. Then I began not to
laugh; and I am not sure now that one ought not to weep....
We were considering earlier in this volume a certain kind of fool’s
paradise—the paradise which surrounds the collector-fool who
genuinely believes his geese to be swans. That amiable simpleton
deceived no one; he was merely soothingly and caressingly self-
deceived to the top of his bent through a heaven-sent want of true
taste. Compared with him the man who deliberately rubs a mixture on
his face in order to induce his friends to believe that he has been much
in the sun when he has not is complex indeed—for he is deceiving
every one else without for an instant deceiving himself at all. For that is
my reading of that advertisement. I do not accept its face value; I do
not believe that it is bought by men in order to render themselves more
attractive to the fair. My reading is that it is bought by men (and
perhaps by women too: you observe the testimony of the Society
Lady?) in order that it may lend colour to their assertion that they have
been fashionably or expensively holiday-making when they have not.
But why pretend? you say. Ah! you are perhaps well-to-do. Nothing
keeps you at home; or even if it did, it would not cause you shame. But
can you not believe that there are others?...
We feel that we are greater than we know
—as Wordsworth says. That is an exalted mood. A commoner
experience would perhaps be expressed thus:—
We hope you’ll think us greater than we are.
That aspiration, at any rate, is at the bottom of the success of such a
lotion as this; and it is prevalent.
A full inquiry into this foible of poor human nature would need a
volume; nor could I carry it out. Something of the minute scientific
method of Professor Sully would be needed, with a considerable
infusion of Thackeray added, and a leaven of pity, too.
Pity indeed. For though sheer brazen impudence and a determined
lady-killing may resort to this strange bottle, this phial of mockery, yet I
seem to see it being smuggled into simpler homes too. The poor clerk,
for example, who is forced by sheer poverty to spend his week or
fortnight in his London home, and by sheer shame to spend it almost
perdu; reading the paper in bed, smoking his pipe in his back yard,
helping with the children, playing pool at night over his glass in the
public-house at the corner—how would he feel when he returned to
work at the end of the period and had to confess that he had been
nowhere? That is the point to consider, for few of us are great, and he
is very small indeed. Amid triumphant stories of Margate and
Southend, Yarmouth and Southsea, Brighton and even Guernsey,
where would he be if he told the truth? Nowhere. And what fun is it not
to be anywhere? Don’t you see? And so do you blame him if he
spends 1s. 1½d., and anoints his countenance with a little of this
delusive fluid on the morning of his return, and, strong in its testimony,
talks vaguely but sufficiently of Herne Bay? Do you blame him? You
must be a devil of a fellow if you do.
In a way he is entirely justified, for there is no doubt that he is
gaining self-respect by losing it: that is to say, he would feel almost too
paltry if he had to confess to the real squalid economy of his fortnight.
And it is not good to feel too paltry.
But the wish to be thought more fashionable than one is, is not
confined to the respectable poor—the poor, that is, who are forced to
make something of a show: surely the least enviable class of all; the
poor, in other words, who have to forego all the privileges of being
poor. There is another class—Major Pendennis was at the head of it—
who must intrigue a little too, if they are not to be too miserable. I
remember a little man who had a room in Jermyn Street and lived in
his Club; it was his habit to disappear for a fortnight or so every 11th of
August, and reappear very brown and very vocal of the moors. His
colour was genuine—no 1s. 1½d. bottle, but the Lord of Light himself
had conferred it; yet not by beams that fell in Yorkshire or Scotland, but
on Brighton’s pier. How, then, did his narrative of triumph in the butts
carry conviction? What was his particular “Sunbronze”? He wore in the
ribbon of his hat a little row of grouse feathers.
And that possibly is what one has to remember—that “Sunbronze”
takes many forms—more than I know, or you know, or ever shall know,
however extensive our knowledge may be at this moment. For we all
“Sunbronze” a little; at least if not quite all, nearly all. We nearly all
hope you’ll think us greater than we are.
On Leaving One’s Beat

WHEN I am going for a long railway journey I always buy a number of


papers associated with walks of life as far as possible removed from
my own. Then the time passes easily. The ordinary papers one reads
too quickly; the exorbitant require attention—they open the door to new
worlds. I do not mean to suggest that one could go so far as to find
entertainment in the Financial Supplement to the “Times”—that is too
much; but the organs of dog-fancying, yachting, cricket, prize-fighting,
the police, estate agents, licensed victuallers—these are sufficiently
unusual and concentrated to be entertaining if they are really studied.
Their exclusiveness, their importance, I particularly like: the suggestion
they throw out that in this world all is vanity save their own affairs (as
indeed it is). Such self-centredness is very exhilarating.
But the best fun of all is to be found in the stage and variety-hall
papers. Not only are they the most amusing, but also the most human,
for the sock and buskin have a way of forcing the heart to the sleeve.
Limelight does more than all the sun of the tropics to bring emotion to
the surface without shame; and it thus comes about that the
periodicals of the players are full of refreshment to the cabined and
reserved. Reading one of them the other day, I found in the
advertisement columns (which should never be neglected) the
following rich feast of opportunity, on which I have been ruminating
ever since:—

“The Angel of His Dreams.”


WANTED,
Summer
to rehearse April 19th,
Tour, Autumn if suitable,
Dashing Leading Lady; must have power,
pathos, intensity, and be capable of strong
character work. Emotional Juvenile Lady,

You might also like