Python for Scientists (3rd Edition) John M. Stewart 2024 scribd download
Python for Scientists (3rd Edition) John M. Stewart 2024 scribd download
com
https://ebookmeta.com/product/python-for-scientists-3rd-
edition-john-m-stewart/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/python-for-the-life-sciences-a-gentle-
introduction-to-python-for-life-scientists-1st-edition-alexander-
lancaster/
ebookmeta.com
https://ebookmeta.com/product/applied-numerical-methods-with-python-
for-engineers-and-scientists-1st-edition-steven-chapra/
ebookmeta.com
https://ebookmeta.com/product/an-introduction-to-python-programming-
for-scientists-and-engineers-johnny-wei-bing-lin/
ebookmeta.com
https://ebookmeta.com/product/two-oceans-a-guide-to-the-marine-life-
of-southern-africa-4th-edition-george-branch/
ebookmeta.com
Claimed Crown Caruso Crime Cartel 6 1st Edition Autumn
Reign Reign Autumn
https://ebookmeta.com/product/claimed-crown-caruso-crime-cartel-6-1st-
edition-autumn-reign-reign-autumn/
ebookmeta.com
https://ebookmeta.com/product/criminal-law-elements-sixth-edition-
edition-penny-crofts/
ebookmeta.com
https://ebookmeta.com/product/designing-transformation-jews-and-
cultural-identity-in-central-european-modernism-1st-edition-elana-
shapira/
ebookmeta.com
https://ebookmeta.com/product/hateful-liar-saint-juliet-academy-1-1st-
edition-a-j-logan/
ebookmeta.com
https://ebookmeta.com/product/half-yard-christmas-easy-sewing-
projects-using-leftover-pieces-of-fabric-debbie-shore/
ebookmeta.com
Arihant NTA CUET UG 2022 Section 2 Domain Humanities
First Edition Arihant Experts
https://ebookmeta.com/product/arihant-nta-cuet-
ug-2022-section-2-domain-humanities-first-edition-arihant-experts/
ebookmeta.com
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,
1.2 Scientific Software 3
compiled languages are not always ideal for quickly trying out new ideas or writing
short scripts to support you in your daily work. Thus, for the intended readers of this
book, the direct use of compilers is likely to be unattractive, unless their use is manda-
tory. We therefore look at the other type of programming language, the so-called in-
terpreted languages, which include the previously mentioned scientific tools based on
interpreters. Interpreted languages lack the speed of compiled languages, but they typi-
cally are much more intuitive and easier to learn.
Let us summarize our position. There are prebuilt software tools, some of which are
proprietary and some of which are open-source software, that provide various degrees
of flexibility (interpreters typically offer more flexibility than tools that feature GUIs)
and usually focus on specific tasks. On a more basic level, there are traditional compiled
languages for numerics that are very general, very fast, rather difficult to learn, and do
not interact readily with graphical or algebraic processes. Finally, there are interpreted
languages that are typically much easier to learn than compiled languages and offer a
large degree of flexibility but are less performant.
So, what properties should an ideal scientific software have? A short list might contain:
a mature programming language that is both easy to understand and has extensive
expressive ability,
integration of algebraic, numerical, and graphical functions, and the option to import
functionality from an almost endless list of supplemental libraries,
the ability to generate numerical algorithms running with speeds within an order of
magnitude of the fastest of those generated by compiled languages,
an extensive range of textbooks from which the curious reader can develop greater
understanding of the concepts,
You might have guessed it: we are talking about Python here.
The purpose of this intentionally short book is to introduce the Python programming
language and to provide an overview of scientifically relevant packages and how they
can be utilized. This book is written for first-semester students and faculty members,
graduate students and emeriti, high-school students and post-docs – or simply for ev-
eryone who is interested in using Python for scientific analysis.
However, this book by no means claims to be a complete introduction to Python. We
leave the comprehensive treatment of Python and all its details to others who have
done this with great success (see, e.g., Lutz, 2013). We have quite deliberately pre-
ferred brevity and simplicity over encyclopedic coverage in order to get the inquisitive
reader up and running as soon as possible.
Furthermore, this book will not serve as the “Numerical Recipes for Python,” meaning
that we will not explain methods and algorithms in detail: we will simply showcase how
they can be used and applied to scientific problems. For an in-depth discussion of these
algorithms, we refer to the real Numerical Recipes – Press et al. (2007) and all following
releases that were adapted to different programming languages – as well as other works.
Given the dynamic environment of software development, details on specific packages
are best retrieved from online documentation and reference websites. We will provide
references, links, and pointers in order to guide interested readers to the appropriate
places. In order to enable an easy entry into the world of Python, we provide all code
snippets presented in this book in the form of Jupyter Notebooks on the CoCalc cloud
computing platform. These Notebooks can be accessed, run, and modified online for a
more interactive learning experience.
We aim to leave the reader with a well-founded framework to handle many basic, and
not so basic, tasks, as well as the skill set to find their own way in the world of scientific
programming and Python.
1.4 References
Print Resources
Lutz, Mark. Learning Python: Powerful Object-Oriented Programming. O’Reilly Me-
dia, 2013.
Press, William H, et al. Numerical Recipes: The Art of Scientific Computing. 3rd ed.,
Cambridge University Press, 2007.
2 About Python
Python is currently the most popular programming language among scientists and other
programmers. There are a number of reasons leading to its popularity and fame, es-
pecially among younger researchers. This chapter introduces the Python programming
language and provides an overview on how to install and use the language most effi-
ciently.
Based on various recent reports and statistics, Python is currently the most popular
programming language among researchers and professional software developers for a
wide range of applications and problems. This popularity largely stems from the ease
of learning Python, as well as the availability of a large number of add-on packages
that supplement basic Python and provide easy access to tasks that would otherwise be
cumbersome to implement.
6 About Python
But there is also a downside: Python is an interpreted language, which makes it slower
than compiled languages. However, Python provides some remedies for this issue as we
will see in Chapter 9.
Van Rossum is considered the principal author of Python and has played a central role
in its development until 2018. Since 2001, the Python Software Foundation, a nonprofit
organization focusing on the development of the core Python distribution, managing
intellectual rights, and organizing developer conferences, has played an increasingly
important role in the project. Major design decisions within the project are made by a
five-person steering council and documented in Python Enhancement Protocols (PEPs).
PEPs mainly discuss technical proposals and decisions, but we will briefly look at two
PEPs that directly affect users: the Zen of Python (PEP 20, Section 2.1.2) and the Python
Style Guide (PEP 8, Section 3.13).
We would also like to note that in 2012, NumFOCUS was founded as a nonprofit or-
ganization that supports the development of a wide range of scientific Python packages
including, but not limited to, NumPy (see Chapter 4), SciPy (see Chapter 5), Matplotlib
(see Chapter 6), SymPy (see Chapter 7), Pandas (see Chapter 8), Project Jupyter, and
IPython. The support through NumFOCUS for these projects includes funding that is
based on donations to NumFOCUS; for most of these open-source projects, donations
are their only source of funding.
2.1 What Is Python? 7
One detail we have skipped so far is why Van Rossum named his new programming
language after a snake. Well, he did not. Python is actually named after the BBC comedy
TV show Monty Python’s Flying Circus, of which Van Rossum is a huge fan. In case you
were wondering, this is also the reason why the words “spam” and “eggs” are oftentimes
used as metasyntactic variables in Python example code in a reference to their famous
“Spam” sketch from 1970.
Please note that these guidelines focus on the design of the Python programming lan-
guage, not necessarily the design of code written in Python. Nevertheless, you are free to
follow these guidelines when writing your own code to create truly pythonic code. The
term pythonic is often used within the Python community to refer to code that follows
the guiding principles mentioned here.
8 About Python
These guiding principles are numerous and some of them might not be immediately
clear to the reader, especially if you are new to Python programming. We would sum-
marize the most important Python concepts as follows.
Simplicity Simple code is easier to write and read; it improves readability, shareability,
and maintainability, and therefore helps you and others in the short term and
long term.
Explicitness We will explain this idea with an example. Consider you are writing code
that is able to read data from different file formats. A decision you have to
make is the following: will you create a single function that is able to read
all the different file formats, or do you create a number of individual functions,
each of which is able to read only a single file format? The pythonic way would
be the latter: each function that you create will explicitly be able to deal with
only a single file format in contrast to a single function that implicitly deals
with all file formats. Why is this solution favored? Generally, explicit code is
easier to understand and less prone to confusion.
Naturally, these concepts are entangled and closely related to each other. However, there
is no need to memorize these concepts. You will internalize those concepts that are
relevant to you by writing code and reading code written by others. And, of course,
nobody can force you to follow these principles in your own coding; but we hope that
this section provides you a better understanding of the Python programming language
and its design.
Depending on the operating system you are using, there are several ways to install
Python on your computer, some of which are simpler than others. The easiest and at
the same time safest way to install Python is to use the Anaconda environment as de-
tailed below.
Alternatively, you can also install Python from scratch on your computer – unless it is
already installed. In the latter case, you should be careful not to interfere with the native
Python already installed as it might be required by your operating system. This process
might be a bit more complicated, but there are detailed installation guides for all oper-
ating systems available online. To be on the safe side, we recommend the installation of
Anaconda, which comes with Conda, a tool to set up and utilize virtual environments,
2.2 Installing Python 9
in order to prevent interference with other versions of Python that might be installed on
your computer. Once Python is installed, additional packages can also be installed using
Conda and the Package installer for Python, pip.
Installing Anaconda
Installing Anaconda is simple and straightforward. All that is required is to download
the respective Anaconda Individual Edition installer (see Section 2.6) for your operating
system and run it. The installer will walk you through the installation process. Note that
you will need to agree to the Anaconda license agreement. At the end of the installa-
tion routine, you will be asked whether to make Anaconda Python your default Python
version, which is a good idea in most cases. If you now start the Python interpreter (see
Section 2.4.1), you will be greeted by Anaconda Python. Congratulations, you have
successfully installed Anaconda Python on your computer.
Conda
One advantage of using Anaconda is the availability of Conda, an open-source pack-
age and environment manager that was originally developed by Anaconda Inc., but has
subsequently been released separately under an open-source license. Although, for a
beginner, the simple installation process for Anaconda Python is most likely its most
important feature, Conda also solves two problems in the background. As a package
manager, it allows you to easily install Python packages with a single command on
your command line, e.g.,
conda install numpy
Almost all major Python packages are available through Conda. Packages are available
through Conda-Forge (see Section 2.6), a GitHub (see Section 10.3.1) organization that
contains repositories of “Conda recipes” for a wide range of packages. Conda-Forge
contains more detailed information on how to install packages through Conda, as well
as a list of all packages that are available through Conda.
As an environment manager, Conda allows you to define different environments, each
of which can have its own Python installation. Although this is an advanced feature and
becomes important when you are dealing with specific versions of your Python pack-
ages, there is still some benefit for the Python beginner. Some operating systems use
10 About Python
native Python installation to run crucial services; meddling with these Python installa-
tions can seriously harm your system. By default, Anaconda creates a base environment
for the user. Since this environment is independent from your system, there is no danger
in meddling with your system Python installation. Thus using Anaconda is safer than
using your system Python installation.
It is not complicated to define new Conda environments and to switch between them.
However, due to the advanced nature of dealing with different environments, we refer
to the Conda documentation to learn more about how to do this.
Everybody can publish their code via PyPI; in Section 10.3.2 we will show how this can
be achieved. Since PyPI is the official repository of Python packages, pretty much all
available packages are installable using pip.
Pip or Conda?
After learning about Conda and pip you might be confused which of these tools you
should use to install Python packages. The short answer is, in most cases it does not
matter. Especially for beginners, it is perfectly fine and typically also safe to install
packages using pip. Pip is typically faster than Conda in installing packages.
This faster installation process comes at a (small) price that won’t matter to most users.
The price is that Conda is generally safer in installing new packages. Before Conda in-
stalls a new package, it will check the version numbers of all packages that are already
installed in your current Conda environment and it will check whether these packages
in the present versions are compatible with the new package and vice versa. Pip sim-
ply checks whether the versions of the installed packages are compatible with the new
package – and it will update the already present packages, to make them compatible
with the new package. However, pip disregards that there might be requirements by
other packages that will break by updating these existing packages. As a result, pip may
break packages that were previously installed.
This happens very rarely, since most Python packages are compatible over many dif-
ferent versions. However, in the case of quickly developing projects it is mandatory to
use specific versions of packages. In those cases, it is much safer to use Conda to install
new packages. For most other users, especially on the beginner level, there should be
no major issues.
2.3 How Python Works 11
The following sections will detail how to directly provide code to the interpreter in
different ways.
There are different ways to use Python, the most important of which we will introduce
in the following sections. Which of these options you should use depends on your pref-
erences and the problem you are trying to solve.
In the remainder of this book, we assume that you are using Jupyter Notebooks. This
choice is mainly driven by the opportunity to publish all code elements from this book
as readily accessible Jupyter Notebooks. You can run these Notebooks (as well as your
own Notebooks) online in the cloud, or locally on your computer as detailed below.
However, we would like to point out that it is not a requirement for the reader to use
these Notebooks in order to follow this book in any way. Feel free to use whichever
interface to Python you feel most comfortable with.
While this might be useful to quickly try something out, it is not really suited to write
long scripts or other more or less complex pieces of code. The interpreter also provides
only a bare minimum in terms of support and usability.
The Python interpreter also offers a different way to run Python code that is much better
suited for running longer pieces of code. Instead of writing your code line by line into
the interpreter, you can simply write your code into an ordinary text file and pass that
12 About Python
file to the interpreter in your terminal window or on the command line. You can give
your code file any name you want, but by convention, you should use the file ending
“.py.” You can use the most basic text editor for this purpose: Emacs, Vim, Nano or
Gedit on Linux, TextEdit or Sublime on a Mac, or NotePad on Windows. It is important
that the resulting Python code file does not contain any fancy formatting, just clean
text.
For example, you could create a file named “hello.py” with the following single line
of content:
print(’Hello World!’)
You can then run this script in a terminal window or powershell by using
python hello.py
Make sure that Python is properly installed on your system (see Section 2.2) and that
you run this command in the same directory where the hello.py file resides. If suc-
cessful, the output that you receive should look like this:
Hello World!
The first version of IPython was published in 2001. Project Jupyter evolved from IPython
around 2014 as a nonprofit, open-source project to support interactive data science
and scientific computing. The Notebook interface was subsequently outsourced from
IPython and implemented as part of Jupyter, where it was perfected and extended in
different ways. Most notably, Jupyter Notebooks are language agnostic and can be used
with different programming languages using so-called kernels. The Python kernel is
provided and still maintained by the IPython project.
2.4 How to Use Python 13
Figure 2.1 A newly created Jupyter Notebook containing a single, empty code cell.
Jupyter Notebooks
The most relevant feature of Jupyter for you will most likely be the Jupyter Notebook,
which is an enhanced version of the IPython Notebook. All programming examples
are presented in the form of Jupyter Notebooks and imitate their appearance (see Fig-
ure 2.1). Furthermore, all code elements shown in this book are available as Jupyter
Notebooks online at CoCalc, and also at www.cambridge.org/9781009014809.
Jupyter Notebooks are documents that consist of distinct cells that can contain and run
code, formatted text, mathematical equations, and other media. Notebooks are run in
your browser through a server that is either hosted locally on your computer or in the
cloud (see Section 2.4.4).
To start a Jupyter Notebook server locally, you simply have to run
jupyter notebook
in a terminal window or powershell. This will run a server in the background that is
typically accessible through http://localhost:8888 (you need to type this into the
URL field of your browser to access the Notebook server). You will see a list of files and
directories located in the directory you started the server from. From here you can nav-
igate your file system, and open existing Jupyter Notebooks or create new ones. Note-
book files use the filename ending .ipynb, indicating that they are using the IPython
kernel. To open a Notebook, simply click on the file and you will see something that
looks like Figure 2.1.
Cloud services hosting Jupyter Notebook servers are a different avenue that allow you to
utilize Notebooks without the (minor) hassle of having to install the necessary software
on your computer. As a result, Notebooks that run on cloud services (see Section 2.4.4)
might look a little bit different to what is shown in Figure 2.1, but rest assured that they
can be used in the same way as described in this book.
Notebooks consist of cells that are either code cells that contain Python code or mark-
down cells that contain text or other media utilizing the markdown language. Mark-
down is a lightweight markup language (pun intended) that enables you to quickly
format text and even supports LaTeX inline math. A markdown cheat sheet contain-
ing some formatting basics is provided in Table 2.1 for your convenience; for more
14 About Python
Markdown Appearance
information on how to use the markdown language, please consult your favorite internet
search engine.
To run any cell, i.e., to run the code in a code cell or to render the text in a markdown cell,
you can click the corresponding “run” button or simply use the keyboard shortcut shift
+ enter. If your code cell generates output, this output will be displayed underneath
the code cell. Note that each executed code cell will be numbered (e.g., [1] for the
code cell that was executed first) at the beginning of that cell and that the corresponding
output will carry the same number. This number is stored in the history of the Notebook
and can be utilized and indexed during runtime. Note that in the case of markdown cells,
the raw input that you provided is simply replaced by the rendered text upon execution.
For as long as a Notebook is open and running, the memory is shared between all cells.
That means that if you define an object in one cell and execute that cell, you can access
that object from any other cell. This also means that if you change the object in one cell,
its state changes in all other cells, too. Thus the order in which cells are executed might
be important.
There is no rule for how many lines of code should go into a single code cell. When
testing code or performing explorative data analysis, you might end up with a number
of single-line code cells. If you develop large-scale numerical models, you might end
2.4 How to Use Python 15
up with cells containing hundreds of lines of code. The same applies to the number of
cells in a Notebook.
We encourage the reader to take full advantage of the features of a Notebook: com-
bine code cells and markdown cells in such a way as to treat the Notebook as a self-
explanatory document that contains runnable Python code.
While code cells generally expect to receive valid Python commands, they can also
execute commands on the underlying operating system’s terminal or command line en-
vironment. Magic commands provide the user with a way to interact with the operating
system and file system from within a Jupyter Notebook. A very long, very detailed de-
scription of magic commands can be found by typing %magic, and a compact list of
available commands is given by typing %lsmagic. Note that there are two types of
magic: line magic and cell magic. Line magic commands are prefixed by % and operate
only on a single line of a cell. Cell magic, on the other hand, is prefixed by %% and
operates on the entire cell; cell magic should always appear at the beginning of a cell.
A harmless line magic example is pwd, which comes from the Unix operating system
and prints the name of the current directory (present working directory). With magic,
pwd can be called by invoking
%pwd
Magic commands provide a useful set of commands, but this set is limited. There is
also the possibility to execute commands directly on the operating system level without
having to leave a running Notebook environment. This can be achieved in code cells
by prepending an exclamation mark (!). For instance, you can use this mechanism to
install missing Python packages from within a Notebook:
!pip install <package name>
Please be aware that the output that you might receive on your computer may differ from
the output provided in this book. This is especially true for code elements that rely on
random numbers, but also other examples. Finally, please be aware that we had to edit
16 About Python
the output provided by Python manually in a few cases to have it displayed properly in
this book.
JupyterLab
JupyterLab is an advanced version of the Jupyter Notebook. It provides you with an
interface to arrange multiple files – e.g., documents, Jupyter Notebooks, text editors,
consoles – on a single screen. The idea behind the JupyterLab environment is to sup-
port data analysis by combining different tools into a single environment. To start a
JupyterLab server locally, all you need to do is to run
jupyter lab
in your Linux or Mac terminal or your Windows powershell. The combination of Note-
books and data visualization tools makes JupyterLabs powerful for tasks involving the
analysis of data and other tasks. We encourage readers to experiment with this system,
but we will not require its use in the following.
JupyterHub
JupyterHub runs a multi-user server for Jupyter Notebooks. This means that multiple
users can log into a server to run and share Notebooks. Some research institutes maintain
their own JupyterHub to provide their researchers a collaborative work environment. A
detailed discussion of JupyterHub is outside the scope of this book, but we would like
the reader to be aware of its existence.
PyCharm and VSCode (Visual Studio Code) are two rather professional IDEs provid-
ing all of the aforementioned features plus a wide range of plugins for a variety of use
cases. While VSCode, although provided by Microsoft, is completely free of charge,
PyCharm has two different versions: a free-to-use Community Edition that comes with
all the bells and whistles for Python programming, and a Professional Version that is
not free, but comes with additional support for the development of scientific software
and web applications.
Finding the right IDE that fits your needs is mostly a matter of taste, habit, and expecta-
tions. Feel free to try all of these IDEs and pick the one that suits your needs. Be aware
that especially the more professional environments will typically feel less comfortable
in the beginning and that it takes some time to get used to setting them up and working
with them. However, at some point, you will get used to them and enjoy some of their
more advanced features.
However, always keep in mind that there is no requirement to use an IDE to become
a good programmer. There are plenty of people out there that write excellent and ex-
tremely complex code, using simple text editors like Vim or Emacs (even those can
be customized into very efficient programming tools by installing a few extensions) or
Jupyter Notebooks. Our bottom line is this: feel free to use whatever tool you feel most
comfortable with!
Before we get started on actual programming with Python, we would like to share a few
words on how to get help when you are stuck with an issue. First of all, do not panic:
there are many ways for you to get help, depending on your situation.
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of "Strictly
Business"
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
“A book such as this restores the hope that the spirit of humor
has not wholly perished from the earth.”
—Philadelphia Ledger.
“STRICTLY BUSINESS”
BY
F. MORTON HOWARD
Author of “Happy Rascals,” etc.
NEW YORK
E. P. DUTTON & COMPANY
681 Fifth Avenue
Copyright, 1923
By E. P. DUTTON & COMPANY
TO
David Whitelaw
CONTENTS
EPISODE PAGE
I. “Strictly Business” 1
II. A Watching Brief 27
III. Circumstantial Evidence 53
IV. Black Cats are Always Lucky 75
V. A Matter of Advertisement 101
VI. “All’s Fair—” 124
VII. Hidden Treasure 144
VIII. A Special Performance 167
IX. The Green Eyes of the Little Birmingham God 192
X. The Girl He Left Behind Him 215
EPISODE I
“STRICTLY BUSINESS”