0% found this document useful (0 votes)
123 views

Introduction To Python For Science & Engineering: David J. Pine

Uploaded by

Wasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views

Introduction To Python For Science & Engineering: David J. Pine

Uploaded by

Wasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

David J.

Pine

Introduction to Python for


Science & Engineering
To Alex Pine
who introduced me to Python
Contents

Preface xi

About the Author xv

1 Introduction 1
1.1 Introduction to Python for Science and Engineering . 1

2 Launching Python 3
2.1 Interacting with Python . . . . . . . . . . . . . . . . . 3
2.2 Installing Python on Your Computer . . . . . . . . . . 4
2.3 The Spyder Window . . . . . . . . . . . . . . . . . . . 4
2.4 The IPython Pane . . . . . . . . . . . . . . . . . . . . . 4
2.4.1 Magic commands . . . . . . . . . . . . . . . . . 6
2.4.2 System shell commands . . . . . . . . . . . . . 8
2.4.3 Tab completion . . . . . . . . . . . . . . . . . . 8
2.4.4 Recap of commands . . . . . . . . . . . . . . . . 9
2.5 Interactive Python as a Calculator . . . . . . . . . . . 9
2.5.1 Binary arithmetic operations in Python . . . . . 10
2.5.2 Types of numbers . . . . . . . . . . . . . . . . . 10
2.5.3 Important note on integer division in Python . 12
2.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.6.1 Names and the assignment operator . . . . . . 13
2.6.2 Legal and recommended variable names . . . . 14
2.6.3 Reserved words in Python . . . . . . . . . . . . 15
2.7 Script Files and Programs . . . . . . . . . . . . . . . . 16
2.7.1 First scripting example: The Editor pane . . . . 16
2.8 Python Modules . . . . . . . . . . . . . . . . . . . . . . 18
2.8.1 Python modules and functions: A first look . . 20
2.8.2 Some NumPy functions . . . . . . . . . . . . . . 22
2.8.3 Scripting Example 2 . . . . . . . . . . . . . . . 23
2.8.4 Different ways of importing modules . . . . . . 24
2.9 Getting Help: Documentation in IPython . . . . . . . 26

iii
iv Contents

2.10 Stand-alone IPython . . . . . . . . . . . . . . . . . . . 26


2.10.1 Writing Python scripts in a text editor . . . . . 27
2.11 Programming Errors . . . . . . . . . . . . . . . . . . . 28
2.11.1 Pyflakes . . . . . . . . . . . . . . . . . . . . . . 28
2.11.2 Error checking . . . . . . . . . . . . . . . . . . . 29
2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3 Strings, Lists, Arrays, and Dictionaries 33


3.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2.1 Slicing lists . . . . . . . . . . . . . . . . . . . . . 37
3.2.2 The range function: Sequences of numbers . . . 38
3.2.3 Tuples . . . . . . . . . . . . . . . . . . . . . . . 39
3.2.4 Multidimensional lists and tuples . . . . . . . . 40
3.3 NumPy Arrays . . . . . . . . . . . . . . . . . . . . . . . 41
3.3.1 Creating arrays (1-d) . . . . . . . . . . . . . . . 41
3.3.2 Mathematical operations with arrays . . . . . . 43
3.3.3 Slicing and addressing arrays . . . . . . . . . . 46
3.3.4 Fancy indexing: Boolean masks . . . . . . . . . 47
3.3.5 Multi-dimensional arrays and matrices . . . . . 49
3.3.6 Differences between lists and arrays . . . . . . 52
3.4 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . 53
3.5 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4 Input and Output 61


4.1 Keyboard Input . . . . . . . . . . . . . . . . . . . . . . 61
4.2 Screen Output . . . . . . . . . . . . . . . . . . . . . . . 64
4.2.1 Formatting output with str.format() . . . . . 64
4.2.2 Printing arrays . . . . . . . . . . . . . . . . . . . 68
4.3 File Input . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.3.1 Reading data from a text file . . . . . . . . . . . 69
4.3.2 Reading data from an Excel file: CSV files . . . 71
4.4 File Output . . . . . . . . . . . . . . . . . . . . . . . . 73
4.4.1 Writing data to a text file . . . . . . . . . . . . . 73
4.4.2 Writing data to a CSV file . . . . . . . . . . . . 76
4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Contents v

5 Conditionals and Loops 81


5.1 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . 82
5.1.1 if, elif, and else statements . . . . . . . . . . 82
5.1.2 Logical operators . . . . . . . . . . . . . . . . . 86
5.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.2.1 for loops . . . . . . . . . . . . . . . . . . . . . . 87
5.2.2 while loops . . . . . . . . . . . . . . . . . . . . . 91
5.2.3 Loops and array operations . . . . . . . . . . . 93
5.3 List Comprehensions . . . . . . . . . . . . . . . . . . . 94
5.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 96

6 Plotting 99
6.1 An Interactive Session with PyPlot . . . . . . . . . . . 100
6.2 Basic Plotting . . . . . . . . . . . . . . . . . . . . . . . 102
6.2.1 Specifying line and symbol types and colors . . 106
6.2.2 Error bars . . . . . . . . . . . . . . . . . . . . . 108
6.2.3 Setting plotting limits and excluding data . . . 110
6.2.4 Subplots . . . . . . . . . . . . . . . . . . . . . . 113
6.3 Logarithmic Plots . . . . . . . . . . . . . . . . . . . . . 116
6.3.1 Semi-log plots . . . . . . . . . . . . . . . . . . . 116
6.3.2 Log-log plots . . . . . . . . . . . . . . . . . . . . 118
6.4 More Advanced Graphical Output . . . . . . . . . . . 118
6.4.1 An alternative syntax for a grid of plots . . . . 122
6.5 Plots with multiple axes . . . . . . . . . . . . . . . . . 125
6.6 Mathematics and Greek symbols . . . . . . . . . . . . 126
6.7 The Structure of matplotlib: OOP and All That . . . . 131
6.7.1 The backend layer . . . . . . . . . . . . . . . . . 132
6.7.2 The artist layer . . . . . . . . . . . . . . . . . . . 135
6.7.3 The PyPlot (scripting) layer . . . . . . . . . . . 137
6.8 Contour and Vector Field Plots . . . . . . . . . . . . . 139
6.8.1 Making a 2D grid of points . . . . . . . . . . . . 139
6.8.2 Contour plots . . . . . . . . . . . . . . . . . . . 140
6.8.3 Streamline plots . . . . . . . . . . . . . . . . . . 144
6.9 Three-Dimensional Plots . . . . . . . . . . . . . . . . . 149
6.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 152

7 Functions 155
7.1 User-Defined Functions . . . . . . . . . . . . . . . . . 156
7.1.1 Looping over arrays in user-defined functions . 158
vi Contents

7.1.2 Fast array processing for user-defined functions 160


7.1.3 Functions with more than one input or output 161
7.1.4 Positional and keyword arguments . . . . . . . 162
7.1.5 Variable number of arguments . . . . . . . . . . 163
7.1.6 Passing function names and parameters as argu-
ments . . . . . . . . . . . . . . . . . . . . . . . . 164
7.2 Passing data (objects) to and from functions . . . . . . 167
7.2.1 Variables and arrays created entirely within a
function . . . . . . . . . . . . . . . . . . . . . . 167
7.2.2 Passing lists and arrays to functions: Mutable
and immutable objects . . . . . . . . . . . . . . 169
7.3 Anonymous Functions: lambda Expressions . . . . . 171
7.4 NumPy Object Attributes: Methods and Instance Vari-
ables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
7.5 Example: Linear Least Squares Fitting . . . . . . . . . 175
7.5.1 Linear regression . . . . . . . . . . . . . . . . . 177
7.5.2 Linear regression with weighting: χ2 . . . . . . 179
7.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 182

8 Curve Fitting 187


8.1 Using Linear Regression for Fitting Nonlinear Func-
tions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
8.1.1 Linear regression for fitting an exponential
function . . . . . . . . . . . . . . . . . . . . . . 187
8.1.2 Linear regression for fitting a power-law func-
tion . . . . . . . . . . . . . . . . . . . . . . . . . 192
8.2 Nonlinear Fitting . . . . . . . . . . . . . . . . . . . . . 193
8.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 198

9 Numerical Routines: SciPy and NumPy 205


9.1 Special Functions . . . . . . . . . . . . . . . . . . . . . 206
9.2 Random Numbers . . . . . . . . . . . . . . . . . . . . . 209
9.2.1 Uniformly distributed random numbers . . . . 210
9.2.2 Normally distributed random numbers . . . . . 210
9.2.3 Random distribution of integers . . . . . . . . . 211
9.3 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . 212
9.3.1 Basic computations in linear algebra . . . . . . 212
9.3.2 Solving systems of linear equations . . . . . . . 213
9.3.3 Eigenvalue problems . . . . . . . . . . . . . . . 214
Contents vii

9.4 Solving Nonlinear Equations . . . . . . . . . . . . . . 216


9.4.1 Single equations of a single variable . . . . . . 217
9.4.2 Solving systems of nonlinear equations . . . . . 221
9.5 Numerical Integration . . . . . . . . . . . . . . . . . . 221
9.5.1 Single integrals . . . . . . . . . . . . . . . . . . 222
9.5.2 Double integrals . . . . . . . . . . . . . . . . . . 226
9.6 Solving ODEs . . . . . . . . . . . . . . . . . . . . . . . 227
9.7 Discrete (Fast) Fourier Transforms . . . . . . . . . . . 231
9.7.1 Continuous and discrete Fourier transforms . . 231
9.7.2 The SciPy FFT library . . . . . . . . . . . . . . . 232
9.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 234

10 Data Manipulation and Analysis: Pandas 239


10.1 Reading Data from Files Using Pandas . . . . . . . . . 240
10.1.1 Reading from Excel files saved as csv files . . . 240
10.1.2 Reading from text files . . . . . . . . . . . . . . 247
10.1.3 Reading from an Excel file . . . . . . . . . . . . 250
10.2 Dates and Times in Pandas . . . . . . . . . . . . . . . . 251
10.3 Data Structures: Series and DataFrame . . . . . . . . . 253
10.3.1 Series . . . . . . . . . . . . . . . . . . . . . . . . 253
10.3.2 DataFrame . . . . . . . . . . . . . . . . . . . . . 256
10.4 Getting Data from the Web . . . . . . . . . . . . . . . 261
10.5 Extracting Information from a DataFrame . . . . . . . 263
10.6 Plotting with Pandas . . . . . . . . . . . . . . . . . . . 267
10.7 Grouping and Aggregation . . . . . . . . . . . . . . . 272
10.7.1 The groupby method . . . . . . . . . . . . . . . 273
10.7.2 Iterating over groups . . . . . . . . . . . . . . . 274
10.7.3 Reformatting DataFrames . . . . . . . . . . . . 277
10.7.4 Custom aggregation of DataFrames . . . . . . . 280
10.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 281

11 Animation 287
11.1 Animating a Sequence of Images . . . . . . . . . . . . 287
11.1.1 Simple image sequence . . . . . . . . . . . . . . 288
11.1.2 Annotating and embellishing videos . . . . . . 292
11.2 Animating Functions . . . . . . . . . . . . . . . . . . . 294
11.2.1 Animating for a fixed number of frames . . . . 295
11.2.2 Animating until a condition is met . . . . . . . 300
11.3 Combining Videos with Animated Functions . . . . . 306
viii Contents

11.3.1 Using a single animation instance . . . . . . . . 307


11.3.2 Combining multiple animation instances . . . 308
11.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . 311

12 Python Classes and GUIs 315


12.1 Defining and Using a Class . . . . . . . . . . . . . . . 316
12.1.1 The __init__() method . . . . . . . . . . . . . 319
12.1.2 Defining methods for a class . . . . . . . . . . . 320
12.1.3 Calling methods from within a class . . . . . . 321
12.1.4 Updating instance variables . . . . . . . . . . . 322
12.2 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . 323
12.3 Graphical User Interfaces (GUIs) . . . . . . . . . . . . 326
12.3.1 Event-driven programming . . . . . . . . . . . 327
12.3.2 PyQt . . . . . . . . . . . . . . . . . . . . . . . . 328
12.3.3 A basic PyQt dialog . . . . . . . . . . . . . . . . 328
12.3.4 Summary of PyQt5 classes used . . . . . . . . . 337
12.3.5 GUI summary . . . . . . . . . . . . . . . . . . . 337

A Installing Python 339


A.1 Installing Python . . . . . . . . . . . . . . . . . . . . . 339
A.1.1 Setting preferences . . . . . . . . . . . . . . . . 340
A.1.2 Pyflakes . . . . . . . . . . . . . . . . . . . . . . 340
A.1.3 Updating your Python installation . . . . . . . 341
A.2 Testing Your Installation of Python . . . . . . . . . . . 341
A.3 Installing FFmpeg for Saving Animations . . . . . . . 343

B Jupyter Notebooks 345


B.1 Launching a Jupyter Notebook . . . . . . . . . . . . . 345
B.2 Running Programs in a Jupyter Notebook . . . . . . . 347
B.3 Annotating a Jupyter Notebook . . . . . . . . . . . . . 348
B.3.1 Adding headings and text . . . . . . . . . . . . 349
B.3.2 Comments with mathematical expressions . . . 350
B.4 Terminal commands in a Jupyter notebook . . . . . . 351
B.5 Plotting in a Jupyter Notebook . . . . . . . . . . . . . 351
B.6 Editing and Rerunning a Notebook . . . . . . . . . . . 353
B.7 Quitting a Jupyter Notebook . . . . . . . . . . . . . . . 353
B.8 Working with an Existing Jupyter Notebook . . . . . . 353

C Glossary 355
Contents ix

D Python Resources 359


D.1 Python Programs and Data Files Introduced in This
Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
D.2 Web Resources . . . . . . . . . . . . . . . . . . . . . . 359
D.3 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

Index 363
Preface

The aim of this book is to provide science and engineering students


a practical introduction to technical programming in Python. It grew
out of notes I developed for various undergraduate physics courses I
taught at NYU. While it has evolved considerably since I first put pen
to paper, it retains its original purpose: to get students with no previ-
ous programming experience writing and running Python programs
for scientific applications with a minimum of fuss.
The approach is pedagogical and “bottom up,” which means start-
ing with examples and extracting more general principles from that
experience. This is in contrast to presenting the general principles
first and then examples of how those general principles work. In my
experience, the latter approach is satisfying only to the instructor.
Much computer documentation takes a top-down approach, which
is one of the reasons it’s frequently difficult to read and understand.
On the other hand, once examples have been seen, it’s useful to ex-
tract the general ideas in order to develop the conceptual framework
needed for further applications.
In writing this text, I assume that the reader:
• has never programmed before;
• is not familiar with programming environments;
• is familiar with how to get around a Mac or PC at a very basic level;
and
• is competent in basic algebra, and for Chapters 8 and 9, calculus,
linear algebra, ordinary differential equations, and Fourier analy-
sis. The other chapters, including 10–12, require only basic algebra
skills.
This book introduces, in some depth, four Python packages that
are important for scientific applications:
NumPy, short for Numerical Python, provides Python with a multi-
dimensional array object (like a vector or matrix) that is at the cen-
ter of virtually all fast numerical processing in scientific Python.

xi
xii Introduction to Python for Science & Engineering

It is both versatile and powerful, enabling fast numerical compu-


tation that, in some cases, approaches speeds close to those of a
compiled language like C, C++, or Fortran.

SciPy, short for Scientific Python, provides access through a Python


interface to a very broad spectrum of scientific and numerical soft-
ware written in C, C++, and Fortran. These include routines to
numerically differentiate and integrate functions, solve differen-
tial equations, diagonalize matrices, take discrete Fourier trans-
forms, perform least-squares fitting, as well as many other numer-
ical tasks.

matplotlib is a powerful plotting package written for Python and


capable of producing publication-quality plots. While there are
other Python plotting packages available, matplotlib is the most
widely used and is the de facto standard.

Pandas is a powerful package for manipulating and analyzing data


formatted and labeled in a manner similar to a spreadsheet (think
Excel). Pandas is very useful for handling data produced in exper-
iments, and is particularly adept at manipulating large data sets
in different ways.

In addition, Chapter 12 provides a brief introduction to Python


classes and to PyQt5, which provides Python routines for building
graphical user interfaces (GUIs) that work on Macs, PCs, and Linux
platforms.
Chapters 1–7 provide the basic introduction to scientific Python
and should be read in order. Chapters 8–12 do not depend on each
other and, with a few mild caveats, can be read in any order.
As the book’s title implies, the text is focused on scientific uses of
Python. Many of the topics that are of primary importance to com-
puter scientists, such as object-oriented design, are of secondary im-
portance here. Our focus is on learning how to harness Python’s abil-
ity to perform scientific computations quickly and efficiently.
The text shows the reader how to interact with Python using
IPython, which stands for Interactive Python, through one of three
different interfaces, all freely available on the web: Spyder, an inte-
grated development environment, Jupyter Notebooks, and a simple
IPython terminal. Chapter 2 provides an overview of Spyder and an
introduction to IPython, which is a powerful interactive environment
Preface xiii

tailored to scientific use of Python. Appendix B provides an introduc-


tion to Jupyter notebooks.
Python 3 is used exclusively throughout the text with little refer-
ence to any version of Python 2. It’s been nearly 10 years since Python
3 was introduced and there is little reason to write new code in Python
2; all the major Python packages have been updated to Python 3.
Moreover, once Python 3 has been learned, it’s a simple task to learn
how Python 2 differs, which may be needed to deal with legacy code.
There are many lucid web sites dedicated to this sometimes necessary
but otherwise mind-numbing task.
The scripts, programs, and data files introduced in this book are
available at https://github.com/djpine/python-scieng-public.
Finally, I would like to thank Étienne Ducrot, Wenhai Zheng, and
Stefano Sacanna for providing some of the data and images used in
Chapter 11, and Mingxin He and Wenhai Zheng for their critical read-
ing of early versions of the text.
About the Author

David Pine has taught physics and chemical engineering for over 30
years at four different institutions: Cornell University (as a graduate
student), Haverford College, UCSB, and, at NYU, where he is a Profes-
sor of Physics, Mathematics, and Chemical & Biomolecular Engineer-
ing. He has taught a broad spectrum of courses, including numerical
methods. He does research in experimental soft-matter physics, which
is concerned with materials such as polymers, emulsions, and col-
loids. These materials constitute most of the material building blocks
of biological organisms.

xv
chapter 1
Introduction

1.1 Introduction to Python for Science and Engineering


This book is meant to serve as an introduction to the Python program-
ming language and its use for scientific computing. It’s ok if you have
never programmed a computer before. This book will teach you how
to do it from the ground up.
The Python programming language is useful for all kinds of sci-
entific and engineering tasks. You can use it to analyze and plot data.
You can also use it to numerically solve science and engineering prob-
lems that are difficult or even impossible to solve analytically.
While we want to marshal Python’s powers to address scientific
problems, you should know that Python is a general purpose com-
puter language that is widely used to address all kinds of comput-
ing tasks, from web applications to processing financial data on Wall
Street and various scripting tasks for computer system management.
Over the past decade it has been increasingly used by scientists and
engineers for numerical computations, graphics, and as a “wrapper”
for numerical software originally written in other languages, like For-
tran and C.
Python is similar to MATLAB® , another computer language that
is frequently used in science and engineering applications. Like
MATLAB® , Python is an interpreted language, meaning you can run
your code without having to go through an extra step of compiling, as
required for the C and Fortran programming languages. It is also a dy-
namically typed language, meaning you don’t have to declare variables
and set aside memory before using them.1
Don’t worry if you don’t know exactly what these terms mean.
Their primary significance for you is that you can write Python code,
test, and use it quickly with a minimum of fuss.
One advantage of Python compared to MATLAB® is that it is free.
It can be downloaded from the web and is available on all the stan-
dard computer platforms, including Windows, macOS, and Linux.
1 Appendix C contains a glossary of terms you may find helpful.

1
2 Introduction to Python for Science & Engineering

This also means that you can use Python without being tethered to
the internet, as required for commercial software that is tied to a re-
mote license server.
Another advantage is Python’s clean and simple syntax, including
its implementation of object-oriented programming. This should not
be discounted; Python’s rich and elegant syntax renders a number of
tasks that are difficult or arcane in other languages either simpler or
more understandable in Python.
An important disadvantage is that Python programs can be slower
than compiled languages like C. For large-scale simulations and other
demanding applications, there can be a considerable speed penalty in
using Python. In these cases, C, C++, or Fortran is recommended, al-
though intelligent use of Python’s array processing tools contained in
the NumPy module can greatly speed up Python code. Another dis-
advantage is that, compared to MATLAB® , Python is less well docu-
mented. This stems from the fact that it is public open source software
and thus is dependent on volunteers from the community of develop-
ers and users for documentation. The documentation is freely avail-
able on the web but is scattered among a number of different sites and
can be terse. This manual will acquaint you with the most commonly
used web sites. Search engines like Google can help you find others.
You are not assumed to have had any previous programming ex-
perience. However, the purpose of this manual isn’t to teach you the
principles of computer programming; it’s to provide a very practical
guide to getting started with Python for scientific computing. Perhaps
once you see some of the powerful tasks that you can accomplish with
Python, you will be inspired to study computational science and en-
gineering, as well as computer programming, in greater depth.

You might also like