Python for Scientists (3rd Edition) John M. Stewart instant download
Python for Scientists (3rd Edition) John M. Stewart instant download
Stewart download
https://ebookmeta.com/product/python-for-scientists-3rd-edition-
john-m-stewart/
https://ebookmeta.com/product/python-for-the-life-sciences-a-
gentle-introduction-to-python-for-life-scientists-1st-edition-
alexander-lancaster/
https://ebookmeta.com/product/applied-numerical-methods-with-
python-for-engineers-and-scientists-1st-edition-steven-chapra/
https://ebookmeta.com/product/an-introduction-to-python-
programming-for-scientists-and-engineers-johnny-wei-bing-lin/
https://ebookmeta.com/product/making-vintage-1940s-clothes-for-
women-1st-edition-sarah-magill/
Fortress America How We Embraced Fear and Abandoned
Democracy Elaine Tyler May
https://ebookmeta.com/product/fortress-america-how-we-embraced-
fear-and-abandoned-democracy-elaine-tyler-may/
https://ebookmeta.com/product/a-history-of-science-in-
society-2nd-edition-andrew-ede-lesley-b-cormack/
https://ebookmeta.com/product/sport-injury-prevention-anatomy-
potach-david/
https://ebookmeta.com/product/the-mysteries-of-all-nations-3rd-
edition-james-grant/
https://ebookmeta.com/product/his-holiday-cheer-a-curvy-woman-
holiday-romance-naughty-nice-book-3-1st-edition-c-l-cruz-liz-fox/
Mind Style and Cognitive Grammar Language and Worldview
in Speculative Fiction 1st Edition Louise Nuttall
https://ebookmeta.com/product/mind-style-and-cognitive-grammar-
language-and-worldview-in-speculative-fiction-1st-edition-louise-
nuttall/
Python for Scientists
Third Edition
The third edition of this practical introduction to Python has been thoroughly updated,
with all code migrated to Python 3 and made available as Jupyter notebooks. The note-
books are available online with executable versions of all 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 the quan-
titative sciences, whatever their background, will be able to quickly acquire the skills
needed for using Python effectively.
Third Edition
JOHN M . S TE WA RT
University of Cambridge
MI C HAE L MO MM E RT
University of St. Gallen, Switzerland
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/9781009014809
DOI: 10.1017/9781009029728
First and Second editions © John M. Stewart 2014, 2017
Third edition © Cambridge University Press & Assessment 2023
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 2014
Second edition 2017
Third edition 2023
Printed in the United Kingdom by CPI Group Ltd, Croydon CR0 4YY
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-01480-9 Paperback
Additional resources for this publication at www.cambridge.org/9781009014809.
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
Preface page xi
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
I bought a copy of Python for Scientists at a conference booth in 2016, looking for an
affordable and easily readable textbook for a Python course I was teaching at that time.
I was intrigued by how straightforwardly even complex things were explained in this
book. It was a perfect match for my course and my students, despite my impression that
the book was rather heavily focused on mathematical applications.
It has been five years since the second edition of Python for Scientists was released.
This is a long time in the life cycle of a programming language that is still under active
development. It was definitely time for an update.
Unfortunately, John is no longer with us to provide this update himself. Instead, I was
honored that this task was offered to me, and I could not decline.
Besides updating the Python code examples shown in the book, I took the opportunity
to also update the content of the book with the goal of making it accessible to a broader
audience of scientists, especially those with a quantitative focus in their work. This in-
cludes a more in-depth discussion of numerical mathematics with NumPy (Chapter 4)
and SciPy (Chapter 5), plotting capabilities with Matplotlib (Chapter 6), and, for the
first time, data handling with Pandas (Chapter 8), performance computing with Python
(Chapter 9), and an outline of software development techniques that are useful to scien-
tists (Chapter 10). However, in order to keep the book reasonably short and affordable,
other content, such as the detailed treatment of ordinary and partial differential equa-
tions, had to be significantly shortened or removed altogether – Python packages for
dealing with such problems exist, but their discussion is beyond the scope of this begin-
ner book.
I sincerely hope the third edition of Python for Scientists will be a useful companion on
your long journey to becoming a scientific programmer.
Michael Mommert
St. Gallen, November 2022
1 Introduction
The title of this book is Python for Scientists, but what does that mean? The dictionary
defines “Python” as either (a) a nonvenomous snake from Asia or Saharan Africa or (b)
a computer programming language, and it is the second option that is intended here. By
“scientist,” we mean anyone who uses quantitative models either to obtain conclusions
by processing precollected experimental data or to model potentially observable results
from a more abstract theory, and who asks “what if?” What if I analyze the data in a
different way? What if I change the model?
Given the steady progress in the development of evermore complex experiments that
explore the inner workings of nature and generate vast amounts of data, as well as the
necessity to describe these observations with complex (nonlinear) theoretical models,
the use of computers to answer these questions is mandatory. Luckily, advances in com-
puter hardware and software development mean that immense amounts of data or com-
plex models can be processed at increasingly rapid speeds. It might seem a given that
suitable software will also be available so that the “what if” questions can be answered
readily. However, this turns out not always to be the case. A quick pragmatic reason
is that while there is a huge market for hardware improvements, scientists form a very
small fraction of it and so there is little financial incentive to improve scientific soft-
ware. But for scientists, specialized, yet versatile, software tools are key to unraveling
complex problems.
Since wages and taxes are recurrent expenditures, the company needs to issue frequent
charged-for updates and improvements (the Danegeld effect).
Open-source software, on the other hand, is available for free. It is usually developed
by computer-literate individuals, often working for universities or similar organizations,
who provide the service for their colleagues. It is distributed subject to anti-copyright
licenses, which give nobody the right to copyright it or to use it for commercial gain.
Conventional economics might suggest that the gamut of open-source software should
be inferior to its proprietary counterpart, or else the commercial organizations would
lose their market. As we shall see, this is not necessarily the case.
Next we need to differentiate between two different types of scientific software. The eas-
iest approach to extracting insight from data or modeling observations utilizes prebuilt
software tools, which we refer to as “scientific software tools.” Proprietary examples
include software tools and packages like Matlab, Mathematica, IDL, Tableau, or even
Excel and open-source equivalents like R, Octave, SciLab, and LibreOffice. Some of
these tools provide graphical user interfaces (GUIs) enabling the user to interact with
the software in an efficient and intuitive way. Typically, such tools work well for stan-
dard tasks, but they do offer only a limited degree of flexibility, making it hard if not
impossible to adapt these packages to solve some task they were not designed for. Other
software tools provide more flexibility through their own idiosyncratic programming
language in which problems are entered into a user interface. After a coherent group
of statements, often just an individual statement, has been typed, the software writes
equivalent core language code and compiles it on the fly. Thus errors and/or results can
be reported back to the user immediately. Such tools are called “interpreters” as they in-
terpret code on the fly, thus offering a higher degree of flexibility compared to software
tools with shiny GUIs.
On a more basic level, the aforementioned software tools are implemented in a pro-
gramming language, which is a somewhat limited subset of human language in which
sequences of instructions are written, usually by humans, to be read and understood by
computers. The most common languages are capable of expressing very sophisticated
mathematical concepts, albeit often with a steep learning curve. Although a myriad of
programming languages exist, only a handful have been widely accepted and adopted
for scientific applications. Historically, this includes C and Fortran, as well as their de-
scendants. In the case of these so-called compiled languages, compilers translate code
written by humans into machine code that can be optimized for speed and then pro-
cessed. As such, they are rather like Formula 1 racing cars. The best of them are capable
of breathtakingly fast performance, but driving them is not intuitive and requires a great
deal of training and experience. This experience is additionally complicated by the fact
that compilers for the same language are not necessarily compatible and need to be sup-
plemented by large libraries to provide functionality for seemingly basic functionality.
Since all scientific software tools are built upon compiled programming languages,
why not simply write your own tools? Well, a racing car is not usually the best choice
for a trip to the supermarket, where speed is not of paramount importance. Similarly,
Exploring the Variety of Random
Documents with Different Content
SUGGESTED REFERENCES
Books
Radiation, Genes, and Man, Bruce Wallace and Theodosius
Dobzhansky, Holt, Rinehart and Winston, Inc., New York 10017,
1963, 205 pp., $5.00 (hardback); $1.28 (paperback).
Radiation: What It Is and How It Affects You. Ralph E. Lapp and Jack
Schubert, The Viking Press, New York 10022, 1957, 314 pp.,
$4.50 (hardback); $1.45 (paperback).
Articles
Genetic Hazards of Nuclear Radiations, Bentley Glass, Science, 126:
241 (August 9, 1957).
Motion Pictures
Radiation and the Population, 29 minutes, sound, black and white,
1962. Produced by the Argonne National Laboratory. This film
explains how radiation causes mutations and how these
mutations are passed on to succeeding generations. Mutation
research is illustrated with results of experimentation on
generations of mice. A discussion of work with fruit flies and
induced mutations is also included. This film is available for loan
without charge from the AEC Headquarters Film Library, Division
of Public Information, U. S. Atomic Energy Commission,
Washington, D. C. 20545 and from other AEC film libraries.
These three films are 30 minutes long, have sound, are in black and
white, and were released in 1960. They are part of a 48-film series
that is correlated with the textbook, Principles of Genetics, (fifth
edition), Edmund W. Sinnott, L. C. Dunn, and Theodosius
Dobzhansky, McGraw-Hill Book Company, 1958, 459 pp., $8.50.
[2]
This is more commonly known as “Mongolism” or “Mongolian
idiocy” though it has nothing to do with the Mongolian people.
[3]
Actually, all waves have some of the characteristics of particles and
all particles have some of the characteristics of waves. Usually,
however, the radiation is predominantly one or the other and little
confusion arises under ordinary circumstances in speaking of
waves and particles as though they were separate phenomena.
[4]
For more about this subject, see Radioisotopes in Industry and
Radioisotopes in Medicine, companion booklets in this series.
[5]
For more about this subject, see Fallout from Nuclear Tests,
another booklet in this series.
[6]
For details on somatic effects of radiation, see Your Body and
Radiation, a companion booklet in this series.
[7]
Estimated average exposures to the gonads, based on 1963 report
of Federal Radiation Council.
[8]
One thousandth of a rem.
[9]
Nevertheless, it should be pointed out that the precautions taken in
the atomic energy industry are such that absorption of radiation is
not as severe a problem as one might suspect. Fully 95% of those
engaged in this work receive less than 1 rem a year. Only 1%
receive more than 5 rems.
ONE OF A SERIES ON
UNDERSTANDING THE ATOM
Nuclear energy is playing a vital role in the life of every man, woman,
and child in the United States today. In the years ahead it will affect
increasingly all the peoples of the earth. It is essential that all
Americans gain an understanding of this vital force if they are to
discharge thoughtfully their responsibilities as citizens and if they are
to realize fully the myriad benefits that nuclear energy offers them.
Requests for more than three titles generally can not be honored.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.