A primer on scientific programming with Python_Third Edition 2012
The aim of this book is to teach computer programming using examples from mathematics and the natural sciences. We have chosen to use the Python programming language because it combines remarkable expressive power with very clean, simple, and compact syntax. Python is easy to learn and very well suited for an introduction to computer programming.Python is also quite similar to Matlab and a good language for doing mathematical computing. It is easy to combine Python with compiled languages, like Fortran, C, and C++, which are widely used languages for scientific computations. A seamless integration of Python with Java is offered by a special version of Python called Jython.
1 Computing with Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 The First Programming Encounter: A Formula . . . . . . . . 1
1.2 Computer Science Glossary . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.3 Another Formula: Celsius-Fahrenheit Conversion . . . . . . 19
1.4 Evaluating Standard Mathematical Functions . . . . . . . . . 23
1.5 Interactive Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1.6 Complex Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2 LoopsandLists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.1 While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.3 Alternative Implementations with Lists and Loops . . . . . 60
2.4 Nested Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.5 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3 Functions and Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.2 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.3 Mixing Loops, Branching, and Functions in
3.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
3.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
4 Input Data and Error Handling . . . . . . . . . . . . . . . . . . . . . 137
4.1 Asking Questions and Reading Answers . . . . . . . . . . . . . . 138
4.2 Reading from the Command Line . . . . . . . . . . . . . . . . . . . 145
4.3 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.4 A Glimpse of Graphical User Interfaces . . . . . . . . . . . . . . 158
4.5 Making Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
4.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
5 Array Computing and Curve Plotting . . . . . . . . . . . . . . . 187
5.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
5.2 Arrays in Python Programs . . . . . . . . . . . . . . . . . . . . . . . . 193
5.3 Curve Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
5.4 Plotting Difficulties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
5.5 More Advanced Vectorization of Functions . . . . . . . . . . . 219
5.6 More on Numerical Python Arrays . . . . . . . . . . . . . . . . . . 226
5.7 Higher-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 231
5.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
6 Files, Strings, and Dictionaries . . . . . . . . . . . . . . . . . . . . . . 257
6.1 Reading Data from File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
6.2 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
6.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
6.4 Reading Data from Web Pages . . . . . . . . . . . . . . . . . . . . . . 291
6.5 Writing Data to File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
6.6 Examples from Analyzing DNA . . . . . . . . . . . . . . . . . . . . . 305
6.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
6.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
7 Introduction to Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
7.1 Simple Function Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
7.2 More Examples on Classes. . . . . . . . . . . . . . . . . . . . . . . . . . 356
7.3 Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
7.4 Example: Class for Vectors in the Plane . . . . . . . . . . . . . . 375
7.5 Example: Class for Complex Numbers . . . . . . . . . . . . . . . 379
7.6 Static Methods and Attributes . . . . . . . . . . . . . . . . . . . . . . 387
7.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
7.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
8 Random Numbers and Simple Games . . . . . . . . . . . . . . . 413
8.1 Drawing Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 414
8.2 Drawing Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
8.3 Computing Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
8.4 Simple Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
8.5 Monte Carlo Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
8.6 Random Walk in One Space Dimension . . . . . . . . . . . . . . 450
8.7 Random Walk in Two Space Dimensions . . . . . . . . . . . . . 456
8.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
8.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
9 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . 483
9.1 Inheritance and Class Hierarchies . . . . . . . . . . . . . . . . . . . 483
9.2 Class Hierarchy for Numerical Differentiation . . . . . . . . . 492
9.3 Class Hierarchy for Numerical Integration . . . . . . . . . . . . 504
9.4 Class Hierarchy for Making Drawings . . . . . . . . . . . . . . . . 513
9.5 Classes for DNA Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 534
9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
附件列表