Python for Chemists

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"

This accessible and self-contained guide provides a comprehensive introduction to the popular programming language Python, with a focus on applications in chemistry and chemical physics. Ideally suited to students and researchers of chemistry learning to employ Python for problem-solving in their research, this fast-paced primer first builds a solid foundation in the programming language before progressing to advanced concepts and applications in chemistry. The required syntax and data structures are established, and then applied to solve problems computationally. Popular numerical packages are described in detail, including NumPy, SciPy, Matplotlib, SymPy, and pandas. End of chapter problems are included throughout, with worked solutions available within the book. Additional resources, datasets, and Jupyter Notebooks are provided on a companion website, allowing readers to reinforce their understanding and gain confidence applying their knowledge through a hands-on approach.

Author(s): CHRISTIAN HILL
Publisher: Independently published
Year: 2023

Language: English
Pages: 559

Introduction 1
1.1 About This Book 1
1.2 About Python 2
1.3 Installing Python 3
1.4 Code Editors 4
2 Basic Python Usage 6
2.1 Python as a Calculator 6
2.2 Defining Numbers 10
2.3 Variables 11
2.4 Limitations and Pitfalls 13
2.5 Examples 14
2.6 Exercises 20
3 Strings 22
3.1 Defining Strings 22
3.2 String Indexing and Slicing 24
3.3 String Methods 26
3.4 String Formatting 27
3.5 Examples 28
3.6 Exercise 30
4 Lists and Loops 31
4.1 Definitions, Syntax and Usage 31
4.2 range and enumerate 33
4.3 Creating lists 34
v
vi Contents
4.4 split and join 34
4.5 zip 35
4.6 Examples 36
5 Comparisons and Flow Control 41
5.1 Comparisons and Logic 41
5.2 if ... elif ... else 43
5.3 while loops 44
5.4 More Control Flow: break, continue and pass 44
5.5 Exceptions 45
5.6 Examples 47
5.7 Exercises 53
6 Functions 54
6.1 Defining Functions 54
6.2 Keyword and Default Arguments 55
6.3 Docstrings 56
6.4 Scope 57
6.5 lambda (Anonymous) Functions 59
6.6 Examples 61
7 Data Structures 64
7.1 Lists 64
7.2 Tuples 67
7.3 Sets 68
7.4 Dictionaries 70
7.5 Examples 72
7.6 Exercises 75
8 File Input/Output 76
8.1 Writing Files 76
8.2 Reading Files 77
8.3 Character Encoding 78
8.4 Example 79
8.5 Exercises 80
9 Basic NumPy 82
9.1 Creating NumPy Arrays 82
9.2 Indexing and Slicing NumPy Arrays 87
9.3 NumPy Array Aggregation 89
Contents vii
9.4 NaN: Not a Number 91
9.5 Boolean Arrays and Indexing 91
9.6 Reading Data Files into a NumPy Array 92
9.7 Examples 95
9.8 Exercises 99
10 Graph Plotting with Matplotlib 102
10.1 Line Plots and Scatter Plots 102
10.2 Examples 108
10.3 Exercise 113
11 The Steady-State Approximation 114
12 Liquid–Vapor Equilibrium 118
13 Jupyter Notebook 122
13.1 Jupyter Notebook Basics 122
13.2 Markdown Cells in Jupyter Notebook 125
14 LaTeX 134
14.1 Mathematics with LaTeX 134
14.2 Chemical Equations 143
14.3 Example 144
15 Chemistry Databases and File Formats 147
15.1 Formats 147
15.2 Online Services 154
15.3 Example 156
15.4 Exercises 162
16 More NumPy and Matplotlib 163
16.1 NumPy 163
16.2 Physical Constants (SciPy) 172
16.3 More Matplotlib 174
16.4 Example 181
17 Thermodynamic Cycles 184
17.1 Internal Energy and the First Law 184
17.2 Example 187
17.3 Exercise 189
viii Contents
18 Vectors, Matrices and Linear Algebra 191
18.1 NumPy Arrays as Vectors 191
18.2 NumPy Arrays as Matrices 193
18.3 Linear Algebra 197
18.4 Examples 202
18.5 Exercises 211
19 Linear Least Squares Fitting I 213
19.1 Background 213
19.2 Fitting a Line of Best Fit 215
19.3 numpy.linalg.lstsq 216
19.4 Examples 218
19.5 Exercises 227
20 Linear Least Squares Fitting II 229
20.1 Parameter Uncertainties 229
20.2 Example 230
20.3 Exercises 233
21 Numerical Integration 236
21.1 Integrals of a Single Variable 236
21.2 Integrals of Two and Three Variables 237
21.3 Examples 240
21.4 Exercise 246
22 Optimization with scipy.optimize 247
22.1 Multivariate Minimization and Maximization 247
22.2 Univariate Minimization and Maximization 250
22.3 Example 252
22.4 Exercises 258
23 Vibrational Spectroscopy 260
23.1 The Harmonic Oscillator Model 260
23.2 Example 261
23.3 Exercise 266
24 The Morse Oscillator 267
24.1 Example 268
Contents ix
25 Solving Ordinary Differential Equations 272
25.1 A Single First-Order ODE 272
25.2 Coupled First-Order ODEs 275
25.3 A Single Second-Order ODE 277
25.4 Example 279
26 The Oregonator 282
26.1 Theory and Analysis 282
26.2 Exercise 289
27 Root-Finding with scipy.optimize 291
27.1 Root-Finding Algorithms 291
27.2 Example 295
28 Rotational Spectroscopy 300
28.1 Diatomic Molecules: The Rigid Rotor 300
28.2 Centrifugal Distortion 305
28.3 Polyatomic Molecules 308
29 Peak Finding 315
29.1 Simple Peak-Finding 315
29.2 scipy.signal.find_peaks 318
29.3 Example 322
30 Fitting the Vibrational Spectrum of CO 325
30.1 Analyzing a Rovibrational Spectrum 325
30.2 Fitting the Spectrum 329
31 pandas 333
31.1 Series 333
31.2 DataFrame 338
31.3 Reading and Writing Series and DataFrames 346
31.4 Examples 348
31.5 Exercise 358
32 Simulating a Powder Diffraction Spectrum 360
33 The Hückel Approximation 367
33.1 Theory 367
33.2 Examples 369
33.3 Exercises 376
x Contents
34 Nonlinear Fitting and Constrained Optimization 378
34.1 scipy.optimize.least_squares 378
34.2 scipy.optimize.curve_fit 379
34.3 scipy.optimize.minimize with Constraints 379
34.4 Examples 381
34.5 Exercises 395
35 SymPy 397
35.1 Algebra and Mathematical Functions 398
35.2 Equation Solving 403
35.3 Calculus 407
35.4 Example 413
35.5 Exercises 418
36 Molecular Orbital Theory for H+
2 420
37 Approximations of the Helium Atom Electronic Energy 429
37.1 Theory 429
37.2 Attempt 1: Ignore Electron–Electron Repulsion 430
37.3 Attempt 2: One-Parameter Variational Approach 431
37.4 Attempt 3: Hartree–Fock with a Minimal Basis 433
38 Computational Chemistry with Psi4 and Python 442
38.1 Installing Psi4 442
38.2 Examples 442
38.3 Exercise 455
39 Atomic Structure 457
39.1 One-Electron Atoms 457
39.2 Many-Electron Atoms 468
40 Solutions 479
Index