Introduction to Python for Science and Engineering offers a quick and incisive introduction to the Python programming language for use in any science or engineering discipline. The approach is pedagogical and “bottom up,” which means starting with examples and extracting more general principles from that experience. No prior programming experience is assumed. Readers will learn the basics of Python syntax, data structures, input and output, conditionals and loops, user-defined functions, plotting, animation, and visualization. They will also learn how to use Python for numerical analysis, including curve fitting, random numbers, linear algebra, solutions to nonlinear equations, numerical integration, solutions to differential equations, and fast Fourier transforms. Readers learn how to interact and program with Python using JupyterLab and Spyder, two simple and widely used integrated development environments. All the major Python libraries for science and engineering are covered, including NumPy, SciPy, Matplotlib, and Pandas. Other packages are also introduced, including Numba, which can render Python numerical calculations as fast as compiled computer languages such as C but without their complex overhead. David J. Pine has taught physics and chemical engineering for over 40 years at four different institutions: Cornell University (as a graduate student), Haverford College, UCSB, and NYU, where he is a Professor of Physics, Mathematics, and Chemical & Biomolecular Engineering. He has taught a broad spectrum of courses, including numerical methods. He does research on optical materials and in experimental soft-matter physics, which is concerned with materials such as polymers, emulsions, and colloids. Preface to First Edition Preface to Second Edition About the Author CHAPTER 1 ▪ Introduction 1.1 Introduction to Python for Science and Engineering 1.2 Installing Python CHAPTER 2 ▪ Launching Python 2.1 Interacting with Python: The IPython Shell 2.2 The IPython Shell 2.3 Interactive Python as a Calculator 2.3.1 Binary Arithmetic Operations in Python 2.3.2 Types of Numbers 2.3.3 Numbers as Objects 2.4 Variables and Assignment 2.4.1 Names and the Assignment Operator 2.4.2 Legal and Recommended Variable Names 2.4.3 Reserved Words in Python 2.5 Script Files and Programs 2.5.1 Editors for Python Scripts 2.5.2 First Scripting Example 2.6 Python Modules 2.6.1 Python Modules and Functions: A First Look 2.6.2 Some NumPy Functions 2.6.3 Scripting Example 2 2.6.4 Different Ways of Importing Modules 2.7 Getting Help: Documentation in IPython 2.8 Performing System Tasks with IPython 2.8.1 Magic Commands 2.8.2 Tab Completion 2.8.3 Recap of Commands 2.9 Programming Errors 2.9.1 Error Checking 2.10 Exercises CHAPTER 3 ▪ Integrated Development Environments 3.1 Programming and Interacting with Python 3.2 Programming Style and Coding Errors: PEP 8 and Linters 3.3 The Spyder IDE 3.3.1 Autoformatting and Linting in Spyder 3.3.2 Running Python Code in Spyder 3.4 The JupyterLab IDE 3.4.1 Jupyter Extensions 3.5 Jupyter Notebooks 3.6 Launching a Jupyter Notebook 3.7 Running Programs in a Jupyter Notebook 3.8 Annotating a Jupyter Notebook 3.8.1 Adding Headings and Text 3.8.2 Saving a Jupyter Notebook 3.8.3 Editing and Rerunning a Notebook 3.8.4 Quitting a Jupyter Notebook 3.8.5 Working with an Existing Jupyter Notebook CHAPTER 4 ▪ Strings, Lists, Arrays, and Dictionaries 4.1 Strings 4.1.1 Unicode Characters 4.2 Lists 4.2.1 Slicing Lists 4.2.2 Multidimensional Lists 4.2.3 Appending to Lists 4.2.4 Tuples 4.3 Dictionaries 4.4 NumPy Arrays 4.4.1 Creating Arrays (1-d) 4.4.2 Mathematical Operations with Arrays 4.4.3 Slicing and Addressing Arrays 4.4.4 Fancy Indexing: Boolean Indexing 4.4.5 Multidimensional Arrays and Matrices 4.4.6 Broadcasting 4.4.7 Differences Between Lists and Arrays 4.5 Objects 4.6 Exercises CHAPTER 5 ▪ Input and Output 5.1 Keyboard Input 5.2 Screen Output 5.2.1 Formatting Output with str.format() 5.2.2 Formatting with f-strings 5.2.3 Printing Arrays 5.3 File Input 5.3.1 Reading Data from a Text File 5.3.2 Reading Data from an Excel File: CSV Files 5.4 File Output 5.4.1 Writing Data to a Text File 5.4.2 Writing Data to a CSV File 5.5 Exercises CHAPTER 6 ▪ Conditionals and Loops 6.1 Conditionals 6.1.1 if, elif, and else Statements 6.1.2 More about Boolean Variables, Operators, and Expressions 6.2 Loops 6.2.1 while Loops 6.2.2 for Loops 6.2.3 Loop Control Statements 6.2.4 Loops and Array Operations 6.3 List Comprehensions 6.4 Handling Exceptions 6.5 Exercises CHAPTER 7 ▪ Functions 7.1 User-Defined Functions 7.1.1 Looping Over Arrays in User-Defined Functions 7.1.2 Fast Array Processing for User-Defined Functions 7.1.3 Functions with More than One Input or Output 7.1.4 Type Hints 7.1.5 Positional and Keyword Arguments 7.1.6 Variable Number of Arguments 7.1.7 Passing a Function Name and Its Parameters as Arguments 7.2 Namespace and Scope in Python 7.2.1 Scope: Four Levels of Namespaces in Python 7.2.2 Variables and Arrays Created Entirely Within a Function 7.2.3 Passing Lists and Arrays to Functions: Mutable and Immutable Objects 7.3 Anonymous Functions: lambda Expressions 7.4 NumPy Object Attributes: Methods and Instance Variables 7.5 Example: Linear Least Squares Fitting 7.5.1 Linear Regression 7.5.2 Linear Regression with Weighting: χ2 7.6 Exercises CHAPTER 8 ▪ Plotting 8.1 An Interactive Session with PyPlot 8.2 Basic Plotting 8.2.1 Specifying Line and Symbol Types and Colors 8.2.2 Error Bars 8.2.3 Setting Plotting Limits and Excluding Data 8.2.4 Subplots 8.3 Logarithmic Plots 8.3.1 Semi-Log Plots 8.3.2 Log-Log Plots 8.4 More Advanced Graphical Output 8.4.1 An Alternative Syntax for a Grid of Plots 8.5 Plots with Multiple Axes 8.5.1 Plotting Quantities that Share One Axis but not the Other 8.5.2 Two Separate Scales for a Data Set 8.6 Plots with Insets 8.7 Mathematics and Greek Symbols 8.7.1 Manual Axis Labeling 8.8 The Structure of Matplotlib: OOP and All That 8.8.1 The Backend Layer 8.8.2 The Artist Layer 8.8.3 The PyPlot (scripting) Layer 8.9 Contour and Vector Field Plots 8.9.1 Making a 2D Grid of Points 8.9.2 Contour Plots 8.9.3 Streamline Plots 8.9.4 Vector Field (quiver) Plots 8.10 Three-Dimensional Plots 8.10.1 Cartesian Coordinates 8.10.2 Polar Coordinates 8.11 Exercises CHAPTER 9 ▪ Numerical Routines: SciPy and NumPy 9.1 Special Functions 9.1.1 Important Note on Importing SciPy Subpackages 9.2 Spline Fitting, Smoothing, and Interpolation 9.2.1 Interpolating Splines 9.2.2 Smoothing Splines 9.2.3 Finding Roots (zero crossings) of Numerical Data 9.3 Curve Fitting 9.3.1 Linear Fitting Functions 9.3.2 Polynomial Fitting Functions 9.3.3 Nonlinear Fitting Functions 9.4 Random Numbers 9.4.1 Initializing NumPy's Random Number Generator 9.4.2 Uniformly Distributed Random Numbers 9.4.3 Normally Distributed Random Numbers 9.4.4 Random Distribution of Integers 9.4.5 Poisson Distribution of Random Integers 9.5 Linear Algebra 9.5.1 Basic Computations in Linear Algebra 9.5.2 Solving Systems of Linear Equations 9.5.3 Eigenvalue Problems 9.6 Solving Nonlinear Equations 9.6.1 Single Equations of a Single Variable 9.6.2 Solving Systems of Nonlinear Equations 9.7 Numerical Integration 9.7.1 Single Integrals of Functions 9.7.2 Double Integrals 9.7.3 Integrating Numerical Data 9.8 Solving ODEs 9.8.1 A First-Order ODE 9.8.2 A Second-Order ODE 9.9 Discrete (Fast) Fourier Transforms 9.9.1 Continuous and Discrete Fourier Transforms 9.9.2 The SciPy FFT Library 9.10 Exercises CHAPTER 10 ▪ Python Classes: Encapsulation 10.1 A Very Simple Class 10.2 A Brief Introduction to Modules and Packages 10.2.1 Pythonpath 10.3 A Class for Reading and Processing Data 10.3.1 The Data 10.3.2 The Class 10.3.3 The Code 10.4 A Class of Related Functions 10.5 Inheritance 10.6 Exercises CHAPTER 11 ▪ Data Manipulation and Analysis: Pandas 11.1 Data Structures: Series and DataFrame 11.1.1 Series 11.1.2 DataFrame 11.2 Indexing DataFrames 11.2.1 Pandas iloc Indexing 11.2.2 Pandas loc Indexing 11.3 Reading Data from Files Using Pandas 11.3.1 Reading from Excel Files Saved as CSV Files 11.3.2 Reading from an Excel File 11.3.3 Getting Data from the Web 11.4 Extracting Information from a DataFrame 11.5 Plotting with Pandas 11.6 Grouping and Aggregation 11.6.1 The groupby Method 11.6.2 Iterating Over Groups 11.6.3 Reformatting DataFrames 11.6.4 Custom Aggregation of DataFrames 11.7 Exercises CHAPTER 12 ▪ Animation 12.1 Animating a Sequence of Images 12.1.1 Simple Image Sequence 12.1.2 Annotating and Embellishing Videos 12.2 Animating Functions 12.2.1 Animating for a Fixed Number of Frames 12.2.2 Animating until a Condition is Met 12.3 Combining Videos with Animated Functions 12.3.1 Using a Single Animation Instance 12.3.2 Combining Multiple Animation Instances 12.4 Exercises CHAPTER 13 ▪ Speeding Up Numerical Calculations 13.1 Numba's Basic Functions 13.1.1 Faster Loops and NumPy Functions 13.1.2 Vectorizing Functions with Numba 13.1.3 Numba Signatures 13.2 Simulations 13.2.1 A Brownian Dynamics Simulation 13.2.2 Nondimensional Simulation Variables and Parameters 13.2.3 Simulation with the Numba Decorator 13.2.4 Performance and Saving/Reading Large Data Files 13.2.5 Isolating Numerical Code for Numba 13.3 Using Numba with classes 13.4 Other Features of Numba 13.5 Exercises APPENDIX A ▪ Maintaining Your Python Installation A.1 Updating Python A.2 Testing Your Python Installation A.3 Installing FFmpeg for Saving Animations A.4 Adding Folders/Directories to Your Python Path A.4.1 Spyder A.4.2 macOS A.4.3 Windows A.4.4 Linux APPENDIX B ▪ Glossary APPENDIX C ▪ Python Resources C.1 Python Programs and Data Files Introduced in This Text C.2 Web Resources C.3 Books Index