A fully updated tutorial on the basics of the Python programming language for science students Python is a computer programming language that is rapidly gaining popularity throughout the sciences. This fully updated edition of A Student's Guide to Python for Physical Modeling aims to help you, the student, teach yourself enough of the Python programming language to get started with physical modeling. You will learn how to install an open-source Python programming environment and use it to accomplish many common scientific computing tasks: importing, exporting, and visualizing data; numerical analysis; and simulation. No prior programming experience is assumed.
This tutorial focuses on fundamentals and introduces a wide range of useful techniques, including:
Basic Python programming and scripting Numerical arrays Two- and three-dimensional graphics Monte Carlo simulations Numerical methods, including solving ordinary differential equations Image processing Animation Numerous code samples and exercises--with solutions-illustrate new ideas as they are introduced. Web-based resources also accompany this guide and include code samples, data sets, and more. This current edition brings the discussion of the Python language, Spyder development environment, and Anaconda distribution up to date. In addition, a new appendix introduces Jupyter notebooks.
Jesse M. Kinder
, Philip Nelson
Princeton University Pres
Country of Publication:
Professional and scholarly
Let's Go xiii1 Getting Started with Python 11.1 Algorithms and algorithmic thinking 11.1.1 Algorithmic thinking 11.1.2 States 21.1.3 What does a = a + 1 mean? 31.1.4 Symbolic versus numerical 41.2 Launch Python 41.2.1 IPython console 51.2.2 Error messages 91.2.3 Sources of help 91.2.4 Good practice: Keep a log 111.3 Python modules 111.3.1 import 111.3.2 from ... import 121.3.3 NumPy and PyPlot 121.4 Python expressions 131.4.1 Numbers 131.4.2 Arithmetic operations and predefined functions 131.4.3 Good practice: Variable names 151.4.4 More about functions 152 Organizing Data 172.1 Objects and their methods 172.2 Lists, tuples, and arrays 192.2.1 Creating a list or tuple 192.2.2 NumPy arrays 192.2.3 Filling an array with values 212.2.4 Concatenation of arrays 222.2.5 Accessing array elements 232.2.6 Arrays and assignments 242.2.7 Slicing 242.2.8 Flattening an array 262.2.9 Reshaping an array 262.2.10 T2 Lists and arrays as indices 262.3 Strings 272.3.1 Formatting strings with the format() method 292.3.2 T2 Formatting strings with % 303 Structure and Control 313.1 Loops 313.1.1 for loops 313.1.2 while loops 333.1.3 Very long loops 333.1.4 Infinite loops 333.2 Array operations 343.2.1 Vectorizing math 343.2.2 Matrix math 363.2.3 Reducing an array 363.3 Scripts 373.3.1 The Editor 373.3.2 T2 Other editors 383.3.3 First steps to debugging 383.3.4 Good practice: Commenting 403.3.5 Good practice: Using named parameters 433.3.6 Good practice: Units 443.4 Contingent behavior: Branching 443.4.1 The if statement 453.4.2 Testing equality of floats 463.5 Nesting 474 Data In, Results Out 484.1 Importing data 484.1.1 Obtaining data 494.1.2 Bringing data into Python 494.2 Exporting data 524.2.1 Scripts 524.2.2 Data les 524.3 Visualizing data 544.3.1 The plot command and its relatives 554.3.2 Manipulate and embellish 574.3.3 T2 More about gures and their axes 594.3.4 T2 Error bars 604.3.5 3D graphs 604.3.6 Multiple plots 614.3.7 Subplots 624.3.8 Saving gures 624.3.9 T2 Using gures in other applications 635 First Computer Lab 645.1 HIV example 645.1.1 Explore the model 645.1.2 Fit experimental data 655.2 Bacterial example 665.2.1 Explore the model 665.2.2 Fit experimental data 666 More Python Constructions 686.1 Writing your own functions 686.1.1 Defining functions in Python 696.1.2 Updating functions 716.1.3 Arguments, keywords, and defaults 716.1.4 Return values 726.1.5 Functional programming 736.2 Random numbers and simulation 746.2.1 Simulating coin flips 746.2.2 Generating trajectories 756.3 Histograms and bar graphs 766.3.1 Creating histograms 766.3.2 Finer control 776.4 Contour plots and surfaces 776.4.1 Generating a grid of points 786.4.2 Contour plots 786.4.3 Surface plots 796.5 Numerical solution of nonlinear equations 796.5.1 General real functions 806.5.2 Complex roots of polynomials 816.6 Solving systems of linear equations 816.7 Numerical integration 826.7.1 Integrating a predefined function 826.7.2 Integrating your own function 836.7.3 Oscillatory integrands 846.7.4 T2 Parameter dependence 846.8 Numerical solution of differential equations 846.8.1 Reformulating the problem 856.8.2 Solving an ODE 866.8.3 T2 Parameter dependence 876.9 Vector fields and streamlines 886.9.1 Vector fields 886.9.2 Streamlines 897 Second Computer Lab 917.1 Generating and plotting trajectories 917.2 Plotting the displacement distribution 917.3 Rare events 937.3.1 The Poisson distribution 937.3.2 Waiting times 948 Still More Techniques 968.1 Image processing 968.1.1 Images as NumPy arrays 968.1.2 Saving and displaying images 978.1.3 Manipulating images 978.2 Displaying Data as an Image 988.3 Animation 998.3.1 Creating animations 998.3.2 Saving animations 100HTML movies 100T2 Using an encoder 1028.4 Analytic calculations 1038.4.1 The SymPy library 1038.4.2 Wolfram Alpha 1049 Third Computer Lab 1069.1 Convolution 1069.1.1 Python tools for image processing 1079.1.2 Averaging 1089.1.3 Smoothing with a Gaussian 1089.2 Denoising an image 1099.3 Emphasizing features 109Get Going 111A Installing Python 113A.1 Install Python and Spyder 113A.1.1 Graphical installation 114A.1.2 Command line installation 115A.2 Setting up Spyder 116A.2.1 Working directory 116A.2.2 Interactive graphics 117A.2.3 Script template 117A.2.4 Restart 118A.3 Keeping up to date 118A.4 Installing FFmpeg 118A.5 Installing ImageMagick 119B Jupyter Notebooks 120B.1 Getting Started 120B.1.1 Launch Jupyter Notebooks 120B.1.2 Open a Notebook 121B.1.3 Multiple Notebooks 121B.1.4 Quitting Jupyter 122B.1.5 T2 Setting the Default Directory 123B.2 Cells 123B.2.1 Code cells 123B.2.2 Graphics 124B.2.3 Markdown cells 124B.2.4 Edit mode and command mode 125B.3 Sharing 125B.4 More details 125B.5 Pros and Cons 125C Errors and Error Messages 127C.1 Python errors in general 127C.2 Some common errors 128D Python 2 versus Python 3 131D.1 Division 131D.2 Print command 131D.3 User input 132D.4 More assistance 133E Under the Hood 134E.1 Assignment statements 134E.2 Memory management 135E.3 Functions 135E.4 Scope 137E.4.1 Name collisions 138E.4.2 Variables passed as arguments 139E.5 Summary 140F Answers to \Your Turn Questions 141Acknowledgments 145References 147Index 149
Jesse M. Kinder is assistant professor of physics at the Oregon Institute of Technology. Philip Nelson is professor of physics at the University of Pennsylvania. His books include From Photon to Neuron (Princeton) and Physical Models of Living Systems.
Praise for the previous edition: The text serves as an excellent stepping stone into the world of using Python in computational science for undergraduate students with a strong background in mathematics. --Kevin Thielen and Vivienne Tien, Computing in Science & Engineering Praise for the previous edition: Kinder and Nelson have written a friendly and succinct, yet surprisingly comprehensive, introduction to scientific programming in Python. It's written not just for computational scientists, but for anyone who needs to plot and analyze experimental data, numerically solve equations, or learn the basics of programming. Even students who have experience in programming will appreciate the thought-provoking exercises and guidelines for getting the most out of Python. --Vinothan N. Manoharan, Harvard University Praise for the previous edition: This is an excellent introductory text, aimed at those with little to no experience in programming. In a clear and concise manner, the authors cover or touch upon all the important aspects of computational science in Python. They guide readers by explaining how to best perform certain common tasks in scientific computing. The book's examples and user exercises are well selected. --Quentin Caudron, Princeton University Praise for the previous edition: This book covers the basics of Python programming language, with an emphasis on physical modeling. It provides a very useful introduction to Python for undergraduate students and others who have never programmed before. --Zeljko Ivezic, University of Washington Praise for the previous edition: Like patient driving instructors, Kinder and Nelson guide the hands of novice programming students from the get-go, helping them to avoid obstacles and crashes. By the end of the book, students should be racing around confidently like pros, using Python to solve scientific problems of data analysis, modeling, and visualization. A great textbook for a first course in modern scientific programming in any context, and one that I'll be using myself. --Garnet Kin-Lic Chan, Princeton University Praise for the previous edition: This book is tailor-made for physical scientists beginning to do computation. More than in any other programming book I've read, the authors are conscientious--they anticipate and troubleshoot the areas of confusion readers might encounter. Kinder and Nelson carefully and effectively guide readers toward the goal of formulating a computational problem and solving it. --Justin Bois, California Institute of Technology Praise for the previous edition: At a brief 160 pages, it is quite possible for a motivated student to complete it in just a few sittings. . . . [A] fine introduction. --American Journal of Physics