Python for Scientists (3rd Edition)

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

The third edition of this practical introduction to Python has been thoroughly updated, with all code migrated to Jupyter notebooks. The notebooks are available online with executable versions of all of the book's content (and more). The text starts with a detailed introduction to the basics of the Python language, without assuming any prior knowledge. Building upon each other, the most important Python packages for numerical math (NumPy), symbolic math (SymPy), and plotting (Matplotlib) are introduced, with brand new chapters covering numerical methods (SciPy) and data handling (Pandas). Further new material includes guidelines for writing efficient Python code and publishing code for other users. Simple and concise code examples, revised for compatibility with Python 3, guide the reader and support the learning process throughout the book. Readers from all of the quantitative sciences, whatever their background, will be able to quickly acquire the skills needed for using Python effectively.

Author(s): JOHN M. STEWART, MICHAEL MOMMERT
Edition: 3
Publisher: Cambridge University Press
Year: 2023

Language: English
Pages: 301

1 Introduction 1
1.1 Python for Scientists 1
1.2 Scientific Software 1
1.3 About This Book 4
1.4 References 4
2 About Python 5
2.1 What Is Python? 5
2.1.1 A Brief History of Python 6
2.1.2 The Zen of Python 7
2.2 Installing Python 8
2.2.1 Anaconda and Conda 9
2.2.2 Pip and PyPI 10
2.3 How Python Works 11
2.4 How to Use Python 11
2.4.1 The Python Interpreter 11
2.4.2 IPython and Jupyter 12
2.4.3 Integrated Development Environments 16
2.4.4 Cloud Environments 17
2.5 Where to Find Help? 17
2.6 References 19
3 Basic Python 22
3.1 Typing Python 22
3.2 Objects and Identifiers 23
3.3 Namespaces and Modules 26
3.4 Numbers 28
3.4.1 Integers 28
3.4.2 Real Numbers 28
3.4.3 Booleans 29
3.4.4 Complex Numbers 30
vi Contents
3.5 Container Objects 31
3.5.1 Lists 32
3.5.2 List Indexing 33
3.5.3 List Slicing 34
3.5.4 List Mutability 35
3.5.5 List Functions 36
3.5.6 Tuples 38
3.5.7 Strings 39
3.5.8 Dictionaries 41
3.5.9 Sets 42
3.6 Python if Statements 42
3.7 Loop Constructs 44
3.7.1 The for Loop 44
3.7.2 The while Loop 46
3.7.3 The continue Statement 46
3.7.4 The break Statement 47
3.7.5 List Comprehensions 48
3.8 Functions 49
3.8.1 Syntax and Scope 49
3.8.2 Positional Arguments 54
3.8.3 Keyword Arguments 54
3.8.4 Arbitrary Number of Positional Arguments 54
3.8.5 Arbitrary Number of Keyword Arguments 55
3.8.6 Anonymous Functions 55
3.9 Python Input/Output 56
3.9.1 Keyboard Input 56
3.9.2 The print() Function 56
3.9.3 File Input/Output 59
3.10 Error Handling 60
3.10.1 Traceback 60
3.10.2 Errors, Exceptions, and Warnings 62
3.11 Introduction to Python Classes 63
3.12 The Structure of Python 66
3.13 A Python Style Guide 67
3.14 References 68
4 NumPy: Numerical Math 72
4.1 Arrays 72
4.1.1 One-Dimensional Arrays 73
4.1.2 Basic Array Arithmetic 77
4.1.3 Two (and More)-Dimensional Arrays 79
4.1.4 Broadcasting 84
4.1.5 Array Manipulations 86
Contents vii
4.2 Working with Arrays 94
4.2.1 Mathematical Functions and Operators 94
4.2.2 Sums and Products 96
4.2.3 Comparing Arrays 96
4.2.4 Advanced Array Indexing 99
4.2.5 Sorting and Searching 100
4.3 Constants 101
4.4 Random Numbers 102
4.5 Simple Statistics 104
4.6 Polynomials 107
4.6.1 Converting Data to Coecients 107
4.6.2 Converting Coecients to Data 107
4.6.3 Manipulating Polynomials in Coecient Form 108
4.7 Linear Algebra 108
4.7.1 Basic Operations on Matrices 108
4.7.2 Matrix Arithmetic 110
4.7.3 Solving Linear Systems of Equations 111
4.8 File Input/Output 112
4.8.1 Text File Input/Output 112
4.8.2 Binary File Input/Output 115
4.9 Special Array Types 116
4.9.1 Masked Arrays 116
4.9.2 Structured Arrays 117
4.10 References 118
5 SciPy: Numerical Methods 120
5.1 Special Functions 120
5.2 Constants 121
5.3 Numerical Integration 122
5.3.1 Integrating over Functions 122
5.3.2 Integrating over Sampled Values 124
5.4 Optimization and Root Search 124
5.4.1 Local Univariate Optimization 124
5.4.2 Local Multivariate Optimization 125
5.4.3 Function Fitting 127
5.4.4 Root Search 129
5.5 Numerical Interpolation 131
5.5.1 Univariate Interpolation 131
5.5.2 Multivariate Interpolation 133
5.6 Linear Algebra 134
5.6.1 Matrix Operations 135
5.7 Statistics 138
5.7.1 Univariate Continuous Probability Distributions 139
5.7.2 Multivariate and Discrete Probability Distributions 141
viii Contents
5.7.3 Correlation Tests 144
5.7.4 Distribution Tests 145
5.8 Ordinary Di erential Equations 147
5.8.1 Initial Value Problems 148
5.8.2 Boundary Value Problems 151
5.9 SciKits: A Whole New World 153
5.10 References 153
6 Matplotlib: Plotting 156
6.1 Getting Started: Simple Figures 156
6.1.1 Frontends 156
6.1.2 Backends 157
6.1.3 A Simple Figure 157
6.2 Object-Oriented Matplotlib 159
6.3 Customizing Plots 161
6.3.1 Figure Size 162
6.3.2 Axis Range and Scaling 162
6.3.3 Ticks 163
6.3.4 Grid 164
6.3.5 Legend 164
6.3.6 Transparency 164
6.3.7 Text and Annotations 164
6.3.8 Mathematical Formulae 165
6.3.9 Colors 166
6.4 Cartesian Plots 169
6.4.1 Line Plots 169
6.4.2 Scatter Plots 170
6.4.3 Error Bars 171
6.4.4 Plotting Filled Areas 172
6.4.5 Bar Plots 173
6.5 Polar Plots 174
6.6 Plotting Images 175
6.7 Contour Plots 178
6.8 Compound Figures 179
6.9 Multidimensional Visualization 181
6.9.1 The Reduction to Two Dimensions 181
6.9.2 3D Plots 181
6.10 References 185
7 SymPy: Symbolic Math 187
7.1 Symbols and Functions 187
7.2 Conversions from Python to SymPy and Vice Versa 190
Contents ix
7.3 Matrices and Vectors 191
7.4 Some Elementary Calculus 193
7.4.1 Di erentiation 193
7.4.2 Integration 193
7.4.3 Series and Limits 196
7.5 Equality, Symbolic Equality and Simplification 197
7.6 Solving Equations 199
7.6.1 Equations with One Independent Variable 200
7.6.2 Linear Equations with More than One Independent Variable 201
7.6.3 More General Equations 203
7.6.4 Solving Ordinary Di erential Equations 204
7.6.5 Solving Partial Di erential Equations 207
7.7 Plotting from within SymPy 207
7.8 References 212
8 Pandas: Data Handling 214
8.1 Series 214
8.2 DataFrames 216
8.2.1 Axis Labels and Indexes 218
8.2.2 Accessing Data 220
8.2.3 Modifying Data 224
8.2.4 Dealing with Missing Data 231
8.3 Specific Types of Data 234
8.3.1 Categorical Data 234
8.3.2 Textual Data 235
8.3.3 Dates and Times 237
8.4 Functions 238
8.5 Data Visualization 241
8.6 File Input/Output 243
8.7 Pandas Hints 246
8.8 References 247
9 Performance Python 250
9.1 How to Write Ecient Python Code 250
9.1.1 Measuring performance 250
9.1.2 Optimization Starts before Coding 253
9.1.3 Optimizing Basic Python 254
9.1.4 NumPy 256
9.2 Parallelization 259
9.2.1 Multithreading 260
9.2.2 Multiprocessing 263
9.3 What Else? 264
9.4 References 265
x Contents
10 Software Development Tools 267
10.1 Version Control 267
10.1.1 git 268
10.2 Create Your Own Python Module 274
10.3 Publish Your Code 276
10.3.1 GitHub 277
10.3.2 Python Package Index 277
10.4 References 278
Index 280