Professional Documents
Culture Documents
Python for Chemists Christian Hill full chapter instant download
Python for Chemists Christian Hill full chapter instant download
https://ebookmass.com/product/learning-scientific-programming-
with-python-2nd-edition-christian-hill-2/
https://ebookmass.com/product/learning-scientific-programming-
with-python-2nd-edition-christian-hill/
https://ebookmass.com/product/modern-thermodynamics-for-chemists-
and-biochemists-2nd-edition-dennis-sherwood/
https://ebookmass.com/product/io-sono-jai-christian-hill/
Assassinio nella band Christian Hill
https://ebookmass.com/product/assassinio-nella-band-christian-
hill/
https://ebookmass.com/product/io-sono-jai-christian-hill-2/
https://ebookmass.com/product/learning-scientific-programming-
with-python-hill/
https://ebookmass.com/product/the-python-advantage-python-for-
excel-in-2024-hayden-van-der-post/
https://ebookmass.com/product/python-for-finance-a-crash-course-
modern-guide-learn-python-fast-bisette/
PYTHON FOR CHEMISTS
This accessible and self-contained guide provides a comprehensive introduction to the pop-
ular 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 in-
cluded 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.
CHRISTIAN HILL is a physicist and physical chemist with over 25 years’ experience
in scientific programming, data analysis, and database design in atomic and molecular
physics. Currently the Head of the Atomic and Molecular Data Unit at the International
Atomic Energy Agency, Vienna, he has previously held positions at the University of
Cambridge, the University of Oxford, and University College London.
PYTHON FOR CHEMISTS
CHRISTIAN HILL
International Atomic Energy Agency
Shaftesbury Road, Cambridge CB2 8EA, United Kingdom
One Liberty Plaza, 20th Floor, New York, NY 10006, USA
477 Williamstown Road, Port Melbourne, VIC 3207, Australia
314–321, 3rd Floor, Plot 3, Splendor Forum, Jasola District Centre,
New Delhi – 110025, India
103 Penang Road, #05–06/07, Visioncrest Commercial, Singapore 238467
www.cambridge.org
Information on this title: www.cambridge.org/9781009102049
DOI: 10.1017/9781009106696
© Christian Hill 2024
This publication is in copyright. Subject to statutory exception and to the provisions
of relevant collective licensing agreements, no reproduction of any part may take
place without the written permission of Cambridge University Press & Assessment.
First published 2024
A catalogue record for this publication is available from the British Library
A Cataloging-in-Publication data record for this book is available from the Library of Congress
ISBN 978-1-009-10204-9 Paperback
Cambridge University Press & Assessment has no responsibility for the persistence
or accuracy of URLs for external or third-party internet websites referred to in this
publication and does not guarantee that any content on such websites is, or will
remain, accurate or appropriate.
Contents
Acknowledgments page xi
1 Introduction 1
1.1 About This Book 1
1.2 About Python 2
1.3 Installing Python 3
1.4 Code Editors 4
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
v
vi Contents
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
14 LaTeX 134
14.1 Mathematics with LaTeX 134
14.2 Chemical Equations 143
14.3 Example 144
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
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
40 Solutions 479
Index 541
Acknowledgments
Many people have helped in many different ways in the preparation of this book.
In addition to my family, special thanks are due to Milo Shaffer, Alison Whiteley,
Chris Pickard, Helen Reynolds, Lianna Ishihara and Natalie Haynes, who are so
much better about deadlines than I am.
xi
1
Introduction
1
2 1 Introduction
2
These packages are sometimes collectively referred to as the Python scientific computing “stack.”
1.3 Installing Python 3
1.3.1 Windows
Windows users have a couple of further options for installing Python and its libraries:
Python(x,y) (https://python-xy.github.io) and WinPython (https://winpy
thon.github.io/). Both are free.
1.3.2 macOS
macOS, being based on Unix, comes with Python, usually an older version of
Python 3 accessible from the Terminal application as python3. You must not
delete or modify this installation (it’s needed by the operating system), but you
can follow the instructions above for obtaining a distribution with a more recent
version of Python 3. macOS does not have a native package manager (an applica-
tion for managing and installing software), but the two popular third-party pack-
age managers, Homebrew (https://brew.sh/) and MacPorts (www.macports
.org), can both supply the latest version of Python 3 and its packages if you prefer
this option.
1.3.3 Linux
Almost all Linux distributions these days come with Python 3 but the Anaconda
and Enthought distributions both have versions for Linux. Most Linux distributions
come with their own software package managers (e.g., apt in Debian and rpm for
RedHat). These can be used to install more recent versions of Python 3 and its
libraries, though finding the necessary package repositories may take some research
on the Internet. Be careful not to replace or modify your system installation as other
applications may depend on it.
In [x]: 10 / 4
Out[x]: 2.5
1
The name float refers to floating-point numbers, the approximate representation of real numbers used by
Python (and most other modern computer languages).
6
2.1 Python as a Calculator 7
The basic algebraic operators are listed in Table 2.1. In using them, it is important
to pay attention to their precedence: the order in which they are interpreted in an
expression (see Table 2.2). For example:
In [x]: 1 + 3 * 4
Out[x]: 13
Here, 3 * 4 is evaluated first, since the multiplication operator, *, has the higher
precedence out of + and *. The result, 12, is then added to 1. Where operators have
equal precedence, the parts of an expression are generally evaluated left-to-right.2
Precedence can be overridden using parentheses (“round brackets”):
In [x]: 6 / 3 ** 2 # the same as 6 / 9
Out[x]: 0.6666666666666666
Note that these expressions have resulted in a floating point number, even though
we are operating on integers. This is because the division operator, /, always re-
turns a float, even when its result is a whole number. There is a separate integer
division operator, //, which returns the quotient of the division (“how many times
does the second number go into the first”); the related modulus operator, %, returns
the remainder:
In [x]: 7 / 3
Out[x]: 2.3333333333333335
In [x]: 7 // 3
Out[x]: 2
In [x]: 7 % 3
Out[x]: 1
Something interesting has happened with the expression 7 / 3: The exact value,
2 13 cannot be represented in the way that Python stores real numbers (floating-point
numbers), which has a finite precision (of about 1 in 1016 ): The nearest float value
to the answer that can be represented is returned.
In addition to the basic algebraic operators of Table 2.1, there are many math-
ematical functions and the constants π and e provided by Python’s math library.
This is a built-in module that is provided with every Python installation (no extra
packages need to be installed to use it) but must be imported with the command:
import math
2
The exception is exponentiation, which is evaluated right-to-left (“top-down”).
Another random document with
no related content on Scribd:
vida de una cortesana andaluza en Roma, con todas
las obscenidades tomadas del natural, sin recato ni
ideal alguno; no siendo novela ni comedia, sino
narración, pero hablada, dialogada y viva, "en lengua
española muy claríssima". Conocía La Celestina, el
Asno de oro, de Apuleyo; pero no á Pedro Aretino,
como se ha dicho, pues sus obras son posteriores á
1528; antes, ya ha dicho Arturo Graf que, si imitación
hubo, lo fué del Aretino y no al revés, y cierto, el
parecido es grandísimo, aunque M. Pelayo y Farinelli
nieguen tal imitación. Menos tiene La Lozana del
Diálogo de las hetairas, de Luciano, del cual tomó lo
más indecente el Aretino. La Lozana no salió de los
libros, sino de la vida real de Roma, vivida por su
autor. Encierra muchos elementos folklóricos y
frases españolas; pero no menor mezcla de
italianismos, propios de la lengua franca que en
Roma usaban los españoles de baja estofa, de la
cual echó mano igualmente Torres Naharro, sin
dejar, con todo eso, de tener trozos y frases en
castizo y familiar lenguaje, con su propio color y brío.
58. El Ragionamento della Nanna e della Antonia, del Aretino, es de
1533; el Diálogo della Nanna é della Pippa sua figliuola, de 1536; el
Ragionamento del Zoppino fatto frate... dove contiensi la vita e
genealogia de tutte le cortegiane di Roma, no se publicó hasta 1539.
Acaban de ser elegantemente impresos Los Diálogos del divino
Pedro Aretino, por Joaquín López Barbadillo, Madrid, 1914, 2 vols.,
las dos primeras jornadas. Véase Graf, Giornale Storico della
letteratura italiana, Turín, 1880, t. XIII, pág. 317. A. Farinelli, en
Ressegna Bibliographica della letteratura Italiana, t. VII, pág. 281,
Pisa, 1900. M. Pelayo, Oríg. nov., t. III, pág. cxcvi. "Y si quisieren
reprender que por qué no van munchas palabras en perfeta lengua
castellana, digo que siendo andaluz y no letrado y escribiendo para
darme solacio y pasar mi fortuna, que en este tiempo el Señor me
había dado, conformaba mi hablar al sonido de mis orejas, que es la
lengua materna y el común hablar entre mugeres, y si dicen por qué
puse algunas palabras en italiano, púdelo hacer escribiendo en
Italia..., si me dicen que por qué no fuí más elegante, digo que soy
iñorante" (pág. 333).