Let's Go |
|
xiii | |
1 Getting Started with Python |
|
1 | (18) |
|
1.1 Algorithms and algorithmic thinking |
|
|
1 | (3) |
|
1.1.1 Algorithmic thinking |
|
|
1 | (1) |
|
|
2 | (1) |
|
1.1.3 What does a = a + 1 mean? |
|
|
3 | (1) |
|
1.1.4 Symbolic versus numerical |
|
|
3 | (1) |
|
|
4 | (7) |
|
|
6 | (4) |
|
|
10 | (1) |
|
|
10 | (1) |
|
1.2.4 Good practice: Keep a log |
|
|
11 | (1) |
|
|
11 | (3) |
|
|
12 | (1) |
|
|
12 | (1) |
|
|
13 | (1) |
|
|
14 | (5) |
|
|
14 | (1) |
|
1.4.2 Arithmetic operations and predefined functions |
|
|
14 | (1) |
|
1.4.3 Good practice: Variable names |
|
|
15 | (1) |
|
1.4.4 More about functions |
|
|
16 | (3) |
2 Organizing Data |
|
19 | (16) |
|
2.1 Objects and their methods |
|
|
19 | (2) |
|
2.2 Lists, tuples, and arrays |
|
|
21 | (8) |
|
2.2.1 Creating a list or tuple |
|
|
21 | (1) |
|
|
21 | (2) |
|
2.2.3 Filling an array with values |
|
|
23 | (1) |
|
2.2.4 Concatenation of arrays |
|
|
24 | (1) |
|
2.2.5 Accessing array elements |
|
|
25 | (1) |
|
2.2.6 Arrays and assignments |
|
|
26 | (1) |
|
|
27 | (1) |
|
2.2.8 Flattening an array |
|
|
28 | (1) |
|
|
28 | (1) |
|
2.2.10 TT2 Lists and arrays as indices |
|
|
29 | (1) |
|
|
29 | (6) |
|
|
31 | (1) |
|
2.3.2 Formatting strings with the format ( ) method |
|
|
31 | (1) |
|
2.3.3 T2 Formatting strings with % |
|
|
32 | (3) |
3 Structure and Control |
|
35 | (18) |
|
|
35 | (3) |
|
|
35 | (2) |
|
|
37 | (1) |
|
|
37 | (1) |
|
|
37 | (1) |
|
|
38 | (4) |
|
|
38 | (2) |
|
|
40 | (1) |
|
|
41 | (1) |
|
|
42 | (7) |
|
|
42 | (1) |
|
|
42 | (1) |
|
3.3.3 First steps to debugging |
|
|
43 | (2) |
|
3.3.4 Good practice: Commenting |
|
|
45 | (2) |
|
3.3.5 Good practice: Using named parameters |
|
|
47 | (1) |
|
3.3.6 Good practice: Units |
|
|
48 | (1) |
|
3.4 Contingent behavior: Branching |
|
|
49 | (4) |
|
|
50 | (1) |
|
3.4.2 Testing equality of floats |
|
|
51 | (1) |
|
|
52 | (1) |
4 Data In, Results Out |
|
53 | (18) |
|
|
53 | (4) |
|
|
54 | (1) |
|
4.1.2 Bringing data into Python |
|
|
54 | (3) |
|
|
57 | (3) |
|
|
57 | (1) |
|
|
58 | (2) |
|
|
60 | (11) |
|
4.3.1 The plot command and its relatives |
|
|
60 | (3) |
|
|
63 | (1) |
|
4.3.3 Manipulate and embellish |
|
|
63 | (2) |
|
|
65 | (1) |
|
4.3.5 T2 More about figures and their axes |
|
|
65 | (1) |
|
|
66 | (1) |
|
|
66 | (1) |
|
|
67 | (1) |
|
|
68 | (1) |
|
|
69 | (1) |
|
4.3.11 T2 Using figures in other applications |
|
|
70 | (1) |
5 First Computer Lab |
|
71 | (4) |
|
|
71 | (2) |
|
|
71 | (1) |
|
5.1.2 Fit experimental data |
|
|
72 | (1) |
|
|
73 | (2) |
|
|
73 | (1) |
|
5.2.2 Fit experimental data |
|
|
73 | (2) |
6 Random Number Generation and Numerical Methods |
|
75 | (28) |
|
6.1 Writing your own functions |
|
|
75 | (6) |
|
6.1.1 Defining functions in Python |
|
|
76 | (2) |
|
|
78 | (1) |
|
6.1.3 Arguments, keywords, and defaults |
|
|
78 | (1) |
|
|
79 | (1) |
|
6.1.5 Functional programming |
|
|
80 | (1) |
|
6.2 Random numbers and simulation |
|
|
81 | (2) |
|
6.2.1 Simulating coin flips |
|
|
82 | (1) |
|
6.2.2 Generating trajectories |
|
|
82 | (1) |
|
6.3 Histograms and bar graphs |
|
|
83 | (3) |
|
6.3.1 Creating histograms |
|
|
83 | (2) |
|
|
85 | (1) |
|
6.4 Contour plots, surface plots, and heat maps |
|
|
86 | (3) |
|
6.4.1 Generating a grid of points |
|
|
86 | (1) |
|
|
86 | (1) |
|
|
87 | (1) |
|
|
88 | (1) |
|
6.5 Numerical solution of nonlinear equations |
|
|
89 | (2) |
|
6.5.1 General real functions |
|
|
89 | (1) |
|
6.5.2 Complex roots of polynomials |
|
|
90 | (1) |
|
6.6 Solving systems of linear equations |
|
|
91 | (1) |
|
6.7 Numerical integration |
|
|
92 | (3) |
|
6.7.1 Integrating a predefined function |
|
|
92 | (1) |
|
6.7.2 Integrating your own function |
|
|
93 | (1) |
|
6.7.3 Oscillatory integrands |
|
|
94 | (1) |
|
6.7.4 T2 Parameter dependence |
|
|
94 | (1) |
|
6.8 Numerical solution of differential equations |
|
|
95 | (5) |
|
6.8.1 Reformulating the problem |
|
|
95 | (1) |
|
|
96 | (1) |
|
6.8.3 T2 Parameter dependence |
|
|
97 | (1) |
|
|
98 | (2) |
|
6.9 Vector fields and streamlines |
|
|
100 | (3) |
|
|
100 | (1) |
|
|
101 | (2) |
7 Second Computer Lab |
|
103 | (6) |
|
7.1 Generating and plotting trajectories |
|
|
103 | (1) |
|
7.2 Plotting the displacement distribution |
|
|
104 | (1) |
|
|
105 | (4) |
|
7.3.1 The Poisson distribution |
|
|
105 | (1) |
|
|
106 | (3) |
8 Images and Animation |
|
109 | (10) |
|
|
109 | (2) |
|
8.1.1 Images as NumPy arrays |
|
|
109 | (1) |
|
8.1.2 Saving and displaying images |
|
|
110 | (1) |
|
8.1.3 Manipulating images |
|
|
110 | (1) |
|
8.2 Displaying data as an image |
|
|
111 | (2) |
|
|
113 | (6) |
|
8.3.1 Creating animations |
|
|
113 | (1) |
|
|
114 | (3) |
|
|
115 | (2) |
|
|
117 | (1) |
|
|
117 | (2) |
9 Third Computer Lab |
|
119 | (6) |
|
|
119 | (3) |
|
9.1.1 Python tools for image processing |
|
|
120 | (1) |
|
|
121 | (1) |
|
9.1.3 Smoothing with a Gaussian |
|
|
121 | (1) |
|
|
122 | (1) |
|
|
122 | (1) |
|
9.4 T2 Image files and arrays |
|
|
123 | (2) |
10 Advanced Techniques |
|
125 | (32) |
|
10.1 Dictionaries and generators |
|
|
125 | (8) |
|
|
126 | (2) |
|
10.1.2 Special function arguments |
|
|
128 | (1) |
|
10.1.3 List comprehensions and generators |
|
|
129 | (4) |
|
10.2 Tools for data science |
|
|
133 | (5) |
|
10.2.1 Series and data frames with pandas |
|
|
133 | (2) |
|
10.2.2 Machine learning with scikit-learn |
|
|
135 | (3) |
|
|
138 | (1) |
|
|
138 | (10) |
|
|
139 | (2) |
|
|
141 | (3) |
|
10.3.3 Other alternatives |
|
|
144 | (1) |
|
10.3.4 First passage revisited |
|
|
144 | (4) |
|
10.4 Writing your own classes |
|
|
148 | (11) |
|
10.4.1 A random walk class |
|
|
148 | (7) |
|
10.4.2 When to use classes |
|
|
155 | (2) |
Get Going |
|
157 | (2) |
A Installing Python |
|
159 | (7) |
|
A.1 Install Python and Spyder |
|
|
159 | (3) |
|
A.1.1 Graphical installation |
|
|
160 | (1) |
|
A.1.2 Command line installation |
|
|
161 | (1) |
|
|
162 | (2) |
|
|
162 | (1) |
|
A.2.2 Interactive graphics |
|
|
163 | (1) |
|
|
163 | (1) |
|
|
164 | (1) |
|
|
164 | (1) |
|
|
164 | (1) |
|
A.5 Installing ImageMagick |
|
|
164 | (2) |
B Command Line Tools |
|
166 | (17) |
|
|
166 | (5) |
|
B.1.1 Navigating your file system |
|
|
167 | (2) |
|
B.1.2 Creating, renaming, moving, and removing files |
|
|
169 | (1) |
|
B.1.3 Creating and removing directories |
|
|
169 | (1) |
|
|
170 | (1) |
|
|
171 | (1) |
|
|
172 | (10) |
|
|
172 | (2) |
|
B.3.2 Installing and using Git |
|
|
174 | (3) |
|
B.3.3 Tracking changes and synchronizing repositories |
|
|
177 | (2) |
|
B.3.4 Summary of useful workflows |
|
|
179 | (2) |
|
|
181 | (1) |
|
|
182 | (1) |
C Jupyter Notebooks |
|
183 | (7) |
|
|
183 | (3) |
|
C.1.1 Launch Jupyter Notebooks |
|
|
183 | (1) |
|
|
184 | (1) |
|
|
184 | (1) |
|
|
185 | (1) |
|
C.1.5 n Setting the default directory |
|
|
185 | (1) |
|
|
186 | (2) |
|
|
186 | (1) |
|
|
187 | (1) |
|
|
187 | (1) |
|
C.2.4 Edit mode and command mode |
|
|
187 | (1) |
|
|
188 | (1) |
|
|
188 | (1) |
|
|
188 | (2) |
D Errors and Error Messages |
|
190 | (4) |
|
D.1 Python errors in general |
|
|
190 | (1) |
|
|
191 | (3) |
E Python 2 versus Python 3 |
|
194 | (3) |
|
|
194 | (1) |
|
|
195 | (1) |
|
|
195 | (1) |
|
|
196 | (1) |
F Under the Hood |
|
197 | (8) |
|
F.1 Assignment statements |
|
|
197 | (2) |
|
|
199 | (1) |
|
|
199 | (1) |
|
|
200 | (3) |
|
|
202 | (1) |
|
F.4.2 Variables passed as arguments |
|
|
203 | (1) |
|
|
203 | (2) |
G Answers to "Your Turn" Questions |
|
205 | (8) |
Acknowledgments |
|
213 | (2) |
Recommended Reading |
|
215 | (2) |
Index |
|
217 | |