A Student's Guide to Python for Physical Modeling: Second Edition

A fully updated tutorial on the basics of the Python programming language for science students

Python is a computer programming language that has gained popularity throughout the sciences. This fully updated second 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 guide introduces a wide range of useful tools, including:

  • Basic Python programming and scripting
  • Numerical arrays
  • Two- and three-dimensional graphics
  • Animation
  • Monte Carlo simulations
  • Numerical methods, including solving ordinary differential equations
  • Image processing


Numerous code samples and exercises—with solutions—illustrate new ideas as they are introduced. This guide also includes supplemental online resources: code samples, data sets, tutorials, and more. This edition includes new material on symbolic calculations with SymPy, an introduction to Python libraries for data science and machine learning (pandas and sklearn), and a primer on Python classes and object-oriented programming. A new appendix also introduces command line tools and version control with Git.

1121864017
A Student's Guide to Python for Physical Modeling: Second Edition

A fully updated tutorial on the basics of the Python programming language for science students

Python is a computer programming language that has gained popularity throughout the sciences. This fully updated second 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 guide introduces a wide range of useful tools, including:

  • Basic Python programming and scripting
  • Numerical arrays
  • Two- and three-dimensional graphics
  • Animation
  • Monte Carlo simulations
  • Numerical methods, including solving ordinary differential equations
  • Image processing


Numerous code samples and exercises—with solutions—illustrate new ideas as they are introduced. This guide also includes supplemental online resources: code samples, data sets, tutorials, and more. This edition includes new material on symbolic calculations with SymPy, an introduction to Python libraries for data science and machine learning (pandas and sklearn), and a primer on Python classes and object-oriented programming. A new appendix also introduces command line tools and version control with Git.

22.49 In Stock
A Student's Guide to Python for Physical Modeling: Second Edition

A Student's Guide to Python for Physical Modeling: Second Edition

A Student's Guide to Python for Physical Modeling: Second Edition

A Student's Guide to Python for Physical Modeling: Second Edition

eBook

$22.49  $29.95 Save 25% Current price is $22.49, Original price is $29.95. You Save 25%.

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

A fully updated tutorial on the basics of the Python programming language for science students

Python is a computer programming language that has gained popularity throughout the sciences. This fully updated second 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 guide introduces a wide range of useful tools, including:

  • Basic Python programming and scripting
  • Numerical arrays
  • Two- and three-dimensional graphics
  • Animation
  • Monte Carlo simulations
  • Numerical methods, including solving ordinary differential equations
  • Image processing


Numerous code samples and exercises—with solutions—illustrate new ideas as they are introduced. This guide also includes supplemental online resources: code samples, data sets, tutorials, and more. This edition includes new material on symbolic calculations with SymPy, an introduction to Python libraries for data science and machine learning (pandas and sklearn), and a primer on Python classes and object-oriented programming. A new appendix also introduces command line tools and version control with Git.


Product Details

ISBN-13: 9780691223667
Publisher: Princeton University Press
Publication date: 08/03/2021
Sold by: Barnes & Noble
Format: eBook
Pages: 240
Sales rank: 300,175
File size: 11 MB
Note: This product may take a few minutes to download.

About the Author

Jesse M. Kinder is associate 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), Physical Models of Living Systems, and Biological Physics.

Table of Contents

Let's Go xiii

1 Getting Started with Python 1

1.1 Algorithms and algorithmic thinking 1

1.1.1 Algorithmic thinking 1

1.1.2 States 2

1.1.3 What does a = a + 1 mean? 3

1.1.4 Symbolic versus numerical 3

1.2 Launch Python 4

1.2.1 IPython console 6

1.2.2 Error messages 10

1.2.3 Sources of help 10

1.2.4 Good practice: Keep a log 11

1.3 Python modules 11

1.3.1 Import 12

1.3.2 From … import 12

1.3.3 NumPy and PyPlot 13

1.4 Python expressions 14

1.4.1 Numbers 14

1.4.2 Arithmetic operations and predefined functions 14

1.4.3 Good practice: Variable names 15

1.4.4 More about functions 16

2 Organizing Data 19

2.1 Objects and their methods 19

2.2 Lists, tuples, and arrays 21

2.2.1 Creating a list or tuple 21

2.2.2 NumPy arrays 21

2.2.3 Filling an array with values 23

2.2.4 Concatenation of arrays 24

2.2.5 Accessing array elements 25

2.2.6 Arrays and assignments 26

2.2.7 Slicing 27

2.2.8 Flattening an array 28

2.2.9 Reshaping an array 28

2.2.10 T2 Lists and arrays as indices 29

2.3 Strings 29

2.3.1 Raw strings 31

2.3.2 Formatting strings with the format () method 31

2.3.3 T2 Formatting strings with % 32

3 Structure and Control 35

3.1 Loops 35

3.1.1 For loops 35

3.1.2 While loops 37

3.1.3 Very long loops 37

3.1.4 Infinite loops 37

3.2 Array operations 38

3.2.1 Vectorizing math 38

3.2.2 Matrix math 40

3.2.3 Reducing an array 41

3.3 Scripts 42

3.3.1 The Editor 42

3.3.2 T2 Other editors 42

3.3.3 First steps to debugging 43

3.3.4 Good practice: Commenting 45

3.3.5 Good practice: Using named parameters 47

3.3.6 Good practice: Units 48

3.4 Contingent behavior: Branching 49

3.4.1 The if statement 50

3.4.2 Testing equality of floats 51

3.5 Nesting 52

4 Data In, Results Out

4.1 Importing data 53

4.1.1 Obtaining data 54

4.1.2 Bringing data into Python 54

4.2 Exporting data 57

4.2.1 Scripts 57

4.2.2 Data files 58

4.3 Visualizing data 60

4.3.1 The plot command and its relatives 60

4.3.2 Log axes 63

4.3.3 Manipulate and embellish 63

4.3.4 Replacing curves 65

4.3.5 T2 More about figures and their axes 65

4.3.6 T2 Error bars 66

4.3.7 3D graphs 66

4.3.8 Multiple plots 67

4.3.9 Subplots 68

4.3.10 Saving figures 69

4.3.11 T2 Using figures in other applications 70

5 First Computer Lab 71

5.1 HIV example 71

5.1.1 Explore the model 71

5.1.2 Fit experimental data 72

5.2 Bacterial example 73

5.2.1 Explore the model 73

5.2.2 Fit experimental data 73

6 Random Number Generation and Numerical Methods 75

6.1 Writing your own functions 75

6.1.1 Defining functions in Python 76

6.1.2 Updating functions 78

6.1.3 Arguments, keywords, and defaults 78

6.1.4 Return values 79

6.1.5 Functional programming 80

6.2 Random numbers and simulation 81

6.2.1 Simulating coin flips 82

6.2.2 Generating trajectories 82

6.3 Histograms and bar graphs 83

6.3.1 Creating histograms 83

6.3.2 Finer control 85

6.4 Contour plots, surface plots, and heat maps 86

6.4.1 Generating a grid of points 86

6.4.2 Contour plots 86

6.4.3 Surface plots 87

6.4.4 Heat maps 88

6.5 Numerical solution of nonlinear equations 89

6.5.1 General real functions 89

6.5.2 Complex roots of polynomials 90

6.6 Solving systems of linear equations 91

6.7 Numerical integration 92

6.7.1 Integrating a predefined function 92

6.7.2 Integrating your own function 93

6.7.3 Oscillatory integrands 94

6.7.4 T2 Parameter dependence 94

6.8 Numerical solution of differential equations 95

6.8.1 Reformulating the problem 95

6.8.2 Solving an ODE 96

6.8.3 T2 Parameter dependence 97

6.8.4 Other ODE solvers 98

6.9 Vector fields and streamlines 100

6.9.1 Vector fields 100

6.9.2 Streamlines 101

7 Second Computer Lab 103

7.1 Generating and plotting trajectories 103

7.2 Plotting the displacement distribution 104

7.3 Rare events 105

7.3.1 The Poisson distribution 105

7.3.2 Waiting times 106

8 Images and Animation 109

8.1 Image processing 109

8.1.1 Images as NumPy arrays 109

8.1.2 Saving and displaying images 110

8.1.3 Manipulating images 110

8.2 Displaying data as an image 111

8.3 Animation 113

8.3.1 Creating animations 113

8.3.2 Saving animations 114

HTML movies 115

T2 Using an encoder 117

8.3.3 Conclusion 117

9 Third Computer Lab 119

9.1 Convolution 119

9.1.1 Python tools for image processing 120

9.1.2 Averaging 121

9.1.3 Smoothing with a Gaussian 121

9.2 Denoising an image 122

9.3 Emphasizing features 122

9.4 T2 Image files and arrays 123

10 Advanced Techniques 125

10.1 Dictionaries and generators 125

10.1.1 Dictionaries 126

10.1.2 Special function arguments 128

10.1.3 List comprehensions and generators 129

10.2 Tools for data science 133

10.2.1 Series and data frames with pandas 133

10.2.2 Machine learning with scikit-learn 135

10.2.3 Next steps 138

10.3 Symbolic computing 138

10.3.1 Wolfram Alpha 139

10.3.2 The SymPy library 141

10.3.3 Other alternatives 144

10.3.4 First passage revisited 144

10.4 Writing your own classes 148

10.4.1 A random walk class 148

10.4.2 When to use classes 155

Get Going 157

A Installing Python 159

A.1 Install Python and Spyder 159

A.1.1 Graphical installation 160

A.1.2 Command line installation 161

A.2 Setting up Spyder 162

A.2.1 Working directory 162

A.2.2 Interactive graphics 163

A.2.3 Script template 163

A.2.4 Restart 164

A.3 Keeping up to date 164

A.4 Installing FFmpeg 164

A.5 Installing ImageMagick 164

B Command Line Tools 166

B.1 The command line 166

B.1.1 Navigating your file system 167

B.1.2 Creating, renaming, moving, and removing files 169

B.1.3 Creating and removing directories 169

B.1.4 Python and Conda 170

B.2 Text editors 171

B.3 Version control 172

B.3.1 How Git works 172

B.3.2 Installing and using Git 174

B.3.3 Tracking changes and synchronizing repositories 177

B.3.4 Summary of useful workflows 179

B.3.5 Troubleshooting 181

B.4 Conclusion 182

C. Jupyter Notebooks 183

C.1 Getting started 183

C.1.1 Launch Jupyter Notebooks 183

C.1.2 Open a notebook 184

C.1.3 Multiple notebooks 184

C.1.4 Quitting Jupyter 185

C.1.5 T2 Setting the default directory 185

C.2 Cells 186

C.2.1 Code cells 186

C.2.2 Graphics 187

C.2.3 Markdown cells 187

C.2.4 Edit mode and command mode 187

C.3 Sharing 188

C.4 More details 188

C.5 Pros and cons 188

D Errors and Error Messages 190

D.1 Python errors in general 190

D.2 Some common errors 191

E Python 2 versus Python 3 194

E.1 Division 194

E.2 Print command 195

E.3 User input 195

E.4 More assistance 196

F Under the Hood 197

F.1 Assignment statements 197

F.2 Memory management 199

F.3 Functions 199

F.4 Scope 200

F.4.1 Name collisions 202

F.4.2 Variables passed as arguments 203

F.5 Summary 203

G Answers to "Your Turn" Questions 205

Acknowledgments 213

Recommended Reading 215

Index 217

What People are Saying About This

From the Publisher

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



"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

"Kinder and Nelson's engaging introduction to scientific programming in Python is careful and thorough, and focuses on actual essentials. Bread-and-butter concepts and techniques, belonging in every computational scientist's toolbox, are presented with well-thought-out examples drawn from daily research practice. This is a clever text, inviting students to take that most important step: to dive right in and start coding."—Cornelis Storm, Eindhoven University of Technology

"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

"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

"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

"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."Željko Ivezić , University of Washington

"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

From the B&N Reads Blog

Customer Reviews