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