100% found this document useful (1 vote)
35 views79 pages

Python for Scientists 2nd Edition John M. Stewart - The ebook in PDF/DOCX format is ready for download now

The document provides information about various eBooks available for instant download on ebookgate.com, including titles such as 'Python for Scientists' and 'Management for Engineers.' It highlights the accessibility of digital formats like PDF, ePub, and MOBI for readers. The document also includes details about the contents and structure of the 'Python for Scientists' book by John M. Stewart, covering topics from basic Python tutorials to advanced scientific computing techniques.

Uploaded by

mayacuchyou35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
35 views79 pages

Python for Scientists 2nd Edition John M. Stewart - The ebook in PDF/DOCX format is ready for download now

The document provides information about various eBooks available for instant download on ebookgate.com, including titles such as 'Python for Scientists' and 'Management for Engineers.' It highlights the accessibility of digital formats like PDF, ePub, and MOBI for readers. The document also includes details about the contents and structure of the 'Python for Scientists' book by John M. Stewart, covering topics from basic Python tutorials to advanced scientific computing techniques.

Uploaded by

mayacuchyou35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Instant Ebook Access, One Click Away – Begin at ebookgate.

com

Python for Scientists 2nd Edition John M. Stewart

https://ebookgate.com/product/python-for-scientists-2nd-
edition-john-m-stewart/

OR CLICK BUTTON

DOWLOAD EBOOK

Get Instant Ebook Downloads – Browse at https://ebookgate.com


Click here to visit ebookgate.com and download ebook now
Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Management for Engineers Scientists and Technologists 2nd


Edition John V. Chelsom

https://ebookgate.com/product/management-for-engineers-scientists-and-
technologists-2nd-edition-john-v-chelsom/

ebookgate.com

Fuzzy Mathematics An Introduction for Engineers and


Scientists 2nd Edition Professor John N. Mordeson

https://ebookgate.com/product/fuzzy-mathematics-an-introduction-for-
engineers-and-scientists-2nd-edition-professor-john-n-mordeson/

ebookgate.com

Python for Data Science For Dummies 1st Edition John Paul
Mueller

https://ebookgate.com/product/python-for-data-science-for-dummies-1st-
edition-john-paul-mueller/

ebookgate.com

Python for Bioinformatics 2nd Edition Sebastian Bassi

https://ebookgate.com/product/python-for-bioinformatics-2nd-edition-
sebastian-bassi/

ebookgate.com
Python for Everyone 2nd Edition Cay Horstmann

https://ebookgate.com/product/python-for-everyone-2nd-edition-cay-
horstmann/

ebookgate.com

Shared Care For Prostatic Diseases 2nd Edition John M


Fitzpatrick

https://ebookgate.com/product/shared-care-for-prostatic-diseases-2nd-
edition-john-m-fitzpatrick/

ebookgate.com

Introduction to Machine Learning with Python A Guide for


Data Scientists 1st Edition Andreas C. Müller

https://ebookgate.com/product/introduction-to-machine-learning-with-
python-a-guide-for-data-scientists-1st-edition-andreas-c-muller/

ebookgate.com

CISSP 7th Edition James M. Stewart

https://ebookgate.com/product/cissp-7th-edition-james-m-stewart/

ebookgate.com

Great Scientists Speak Again 2nd printing, Reprint 2020


Edition Richard M. Eakin

https://ebookgate.com/product/great-scientists-speak-again-2nd-
printing-reprint-2020-edition-richard-m-eakin/

ebookgate.com
Python for Scientists
Second Edition

J O H N M . S T E WA RT
Department of Applied Mathematics & Theoretical Physics
University of Cambridge
www.cambridge.org
Information on this title: www.cambridge.org/9781316641231
DOI: 10.1017/9781108120241

c John M. Stewart 2014, 2017

First published 2014


Second edition 2017
Printed in the United Kingdom by TJ International Ltd. Padstow Cornwall
A catalogue record for this publication is available from the British Library.
Library of Congress Cataloging-in-Publication Data
Names: Stewart, John, 1943 July 1–
Title: Python for scientists / John M. Stewart, Department of Applied, Mathematics & Theoretical
Physics, University of Cambridge.
Description: Second edition. | Cambridge, United Kingdom ; New York, NY, USA : Cambridge
University Press, [2017] | Includes bibliographical references and index.
Identifiers: LCCN 2016049298 | ISBN 9781316641231 (paperback)
Subjects: LCSH: Science–Data processing. | Python (Computer program language)
Classification: LCC Q183.9 .S865 2017 | DDC 005.13/3–dc23
LC record available at https://lccn.loc.gov/2016049298
ISBN 978-1-316-64123-1 Paperback
Additional resources for this publication at www.cambridge.org/9781316641231
Contents

Preface to the Second Edition page xiii


Preface to the First Edition xv

1 Introduction 1
1.1 Scientific Software 1
1.2 The Plan of This Book 4
1.3 Can Python Compete with Compiled Languages? 8
1.4 Limitations of This Book 9
1.5 Installing Python and Add-ons 9

2 Getting Started with IPython 11


2.1 Tab Completion 11
2.2 Introspection 12
2.3 History 14
2.4 Magic Commands 14
2.5 IPython in Action: An Extended Example 15
2.5.1 An IPython terminal workflow 17
2.5.2 An IPython notebook workflow 17

3 A Short Python Tutorial 21


3.1 Typing Python 21
3.2 Objects and Identifiers 22
3.3 Numbers 24
3.3.1 Integers 24
3.3.2 Real numbers 24
3.3.3 Boolean numbers 26
3.3.4 Complex numbers 26
3.4 Namespaces and Modules 27
3.5 Container Objects 28
3.5.1 Lists 29
3.5.2 List indexing 30
3.5.3 List slicing 30
3.5.4 List mutability 31
3.5.5 Tuples 32
3.5.6 Strings 33
3.5.7 Dictionaries 33
3.6 Python if Statements 34
3.7 Loop Constructs 35
3.7.1 The Python for loop 35
3.7.2 The Python continue statement 37
3.7.3 The Python break statement 37
3.7.4 List comprehensions 38
3.7.5 Python while loops 39
3.8 Functions 39
3.8.1 Syntax and scope 40
3.8.2 Positional arguments 43
3.8.3 Keyword arguments 43
3.8.4 Variable number of positional arguments 43
3.8.5 Variable number of keyword arguments 44
3.8.6 Python input/output functions 44
3.8.7 The Python print function 45
3.8.8 Anonymous functions 47
3.9 Introduction to Python Classes 47
3.10 The Structure of Python 50
3.11 Prime Numbers: A Worked Example 51

4 NumPy 55
4.1 One-Dimensional Arrays 57
4.1.1 Ab initio constructors 57
4.1.2 Look-alike constructors 58
4.1.3 Arithmetical operations on vectors 59
4.1.4 Ufuncs 60
4.1.5 Logical operations on vectors 62
4.2 Two-Dimensional Arrays 65
4.2.1 Broadcasting 65
4.2.2 Ab initio constructors 66
4.2.3 Look-alike constructors 68
4.2.4 Operations on arrays and ufuncs 69
4.3 Higher-Dimensional Arrays 69
4.4 Domestic Input and Output 69
4.4.1 Discursive output and input 70
4.4.2 NumPy text output and input 71
4.4.3 NumPy binary output and input 72
4.5 Foreign Input and Output 73
4.5.1 Small amounts of data 73
4.5.2 Large amounts of data 73
4.6 Miscellaneous Ufuncs 74
4.6.1 Maxima and minima 74
4.6.2 Sums and products 75
4.6.3 Simple statistics 75
4.7 Polynomials 75
4.7.1 Converting data to coefficients 76
4.7.2 Converting coefficients to data 76
4.7.3 Manipulating polynomials in coefficient form 76
4.8 Linear Algebra 76
4.8.1 Basic operations on matrices 76
4.8.2 More specialized operations on matrices 78
4.8.3 Solving linear systems of equations 79
4.9 More NumPy and Beyond 79
4.9.1 SciPy 80
4.9.2 SciKits 81

5 Two-Dimensional Graphics 82
5.1 Introduction 82
5.2 Getting Started: Simple Figures 83
5.2.1 Front-ends 83
5.2.2 Back-ends 83
5.2.3 A simple figure 84
5.2.4 Interactive controls 86
5.3 Object-Oriented Matplotlib 87
5.4 Cartesian Plots 88
5.4.1 The Matplotlib plot function 88
5.4.2 Curve styles 89
5.4.3 Marker styles 90
5.4.4 Axes, grid, labels and title 90
5.4.5 A not-so-simple example: partial sums of Fourier series 91
5.5 Polar Plots 93
5.6 Error Bars 94
5.7 Text and Annotations 95
5.8 Displaying Mathematical Formulae 96
5.8.1 Non-LATEX users 96
5.8.2 LATEX users 97
5.8.3 Alternatives for LATEX users 98
5.9 Contour Plots 98
5.10 Compound Figures 101
5.10.1 Multiple figures 101
5.10.2 Multiple plots 102
5.11 Mandelbrot Sets: A Worked Example 104

6 Multi-Dimensional Graphics 109


6.1 Introduction 109
6.1.1 Multi-dimensional data sets 109
6.2 The Reduction to Two Dimensions 109
6.3 Visualization Software 110
6.4 Example Visualization Tasks 111
6.5 Visualization of Solitary Waves 111
6.5.1 The interactivity task 112
6.5.2 The animation task 113
6.5.3 The movie task 115
6.6 Visualization of Three-Dimensional Objects 116
6.7 A Three-Dimensional Curve 118
6.7.1 Visualizing the curve with mplot3d 118
6.7.2 Visualizing the curve with mlab 120
6.8 A Simple Surface 121
6.8.1 Visualizing the simple surface with mplot3d 121
6.8.2 Visualizing the simple surface with mlab 123
6.9 A Parametrically Defined Surface 124
6.9.1 Visualizing Enneper’s surface using mplot3d 124
6.9.2 Visualizing Enneper’s surface using mlab 125
6.10 Three-Dimensional Visualization of a Julia Set 126

7 SymPy : A Computer Algebra System 129


7.1 Computer Algebra Systems 129
7.2 Symbols and Functions 130
7.3 Conversions from Python to SymPy and Vice Versa 132
7.4 Matrices and Vectors 133
7.5 Some Elementary Calculus 134
7.5.1 Differentiation 134
7.5.2 Integration 134
7.5.3 Series and limits 136
7.6 Equality, Symbolic Equality and Simplification 136
7.7 Solving Equations 138
7.7.1 Equations with one independent variable 138
7.7.2 Linear equations with more than one independent variable 139
7.7.3 More general equations 141
7.8 Solving Ordinary Differential Equations 142
7.9 Plotting from within SymPy 144

8 Ordinary Differential Equations 150


8.1 Initial Value Problems 150
8.2 Basic Concepts 150
8.3 The odeint Function 153
8.3.1 Theoretical background 153
8.3.2 The harmonic oscillator 155
8.3.3 The van der Pol oscillator 158
8.3.4 The Lorenz equations 159
8.4 Two-Point Boundary Value Problems 161
8.4.1 Introduction 161
8.4.2 Formulation of the boundary value problem 162
8.4.3 A simple example 164
8.4.4 A linear eigenvalue problem 165
8.4.5 A non-linear boundary value problem 167
8.5 Delay Differential Equations 171
8.5.1 A model equation 172
8.5.2 More general equations and their numerical solution 173
8.5.3 The logistic equation 174
8.5.4 The Mackey–Glass equation 176
8.6 Stochastic Differential Equations 179
8.6.1 The Wiener process 179
8.6.2 The Itô calculus 181
8.6.3 Itô and Stratonovich stochastic integrals 184
8.6.4 Numerical solution of stochastic differential equations 185

9 Partial Differential Equations: A Pseudospectral Approach 192


9.1 Initial Boundary Value Problems 192
9.2 Method of Lines 193
9.3 Spatial Derivatives via Finite Differencing 193
9.4 Spatial Derivatives by Spectral Techniques 194
9.5 The IVP for Spatially Periodic Problems 196
9.6 Spectral Techniques for Non-Periodic Problems 199
9.7 An Introduction to f2py 201
9.7.1 Simple examples with scalar arguments 201
9.7.2 Vector arguments 203
9.7.3 A simple example with multi-dimensional arguments 204
9.7.4 Undiscussed features of f2py 206
9.8 A Real-Life f2py Example 206
9.9 Worked Example: Burgers’ Equation 208
9.9.1 Boundary conditions: the traditional approach 208
9.9.2 Boundary conditions: the penalty approach 209

10 Case Study: Multigrid 213


10.1 The One-Dimensional Case 214
10.1.1 Linear elliptic equations 214
10.1.2 Smooth and rough modes 215
10.2 The Tools of Multigrid 215
10.2.1 Relaxation methods 215
10.2.2 Residual and error 218
10.2.3 Prolongation and restriction 219
10.3 Multigrid Schemes 220
10.3.1 The two-grid algorithm 221
10.3.2 The V-cycle scheme 222
10.3.3 The full multigrid (FMG) scheme 223
10.4 A Simple Python Multigrid Implementation 224
10.4.1 Utility functions 225
10.4.2 Smoothing functions 226
10.4.3 Multigrid functions 228

Appendix A Installing a Python Environment 235


A.1 Installing Python Packages 235
A.2 Communication with IPython Using the Jupyter Notebook 237
A.2.1 Starting and stopping the notebook 237
A.2.2 Working in the notebook 238
A.2.2.1 Entering headers 239
A.2.2.2 Entering Markdown text 239
A.2.2.3 Converting notebooks to other formats 240
A.3 Communication with IPython Using Terminal Mode 240
A.3.1 Editors for programming 240
A.3.2 The two-windows approach 241
A.3.3 Calling the editor from within IPython 242
A.3.4 Calling IPython from within the editor 242
A.4 Communication with IPython via an IDE 242
A.5 Installing Additional Packages 243

Appendix B Fortran77 Subroutines for Pseudospectral Methods 244

References 250

Hints for Using the Index 252

Index 253
Preface to the Second Edition

The motivation for writing this book, and the acknowledgements of the many who have
assisted in its production, are included in the topics of the Preface to the first edition,
which is reprinted after this one. Here I also need to adjoin thanks to the many readers
who provided constructive criticisms, most of which have been incorporated in this
revision. The purpose here is to explain why a second edition is needed. Superficially it
might appear that very little has changed, apart from a new Chapter 7 which discusses
SymPy, Python’s own computer algebra system.
There is, however, a fundamental change, which permeates most of the latest version
of this book. When the first edition was prepared, the reliable way to use the enhanced
interpreter IPython was via the traditional “terminal mode”. Preparations were under
way for an enhanced “notebook mode”, which looked then rather like the Mathemat-
ica notebook concept, except that it appeared within one’s default web browser.1 That
project has now morphed into the Jupyter notebook. The notebook allows one to con-
struct and distribute documents containing computer code (over forty languages are
supported), equations, explanatory text, figures and visualizations. Since this is also
perhaps the easiest software application for a beginner to develop Python experience,
much of the book has been rewritten for the notebook user. In particular there is now
a lightning course on how to use the notebook in Appendix A, and Chapter 2 has been
extensively rewritten to demonstrate its properties. All of the material in the book now
reflects, where appropriate, its use. For example, it allows SymPy to produce algebraic
expressions whose format is unsurpassed by other computer algebra systems.
This change also affects the areas of interactive graphics and visual animations. Their
demands are such that the standard Python two-dimensional graphics package Mat-
plotlib is having difficulty in producing platform-independent results. Indeed, because
of “improved” software upgrades, the code suggested for immediate on-screen anima-
tions in the first edition no longer works. However, the notebook concept has a subtle
solution to resolve this impasse. Recall that the notebook window is your browser win-
dow, which uses modern HTML graphics. The consequent benefits are introduced in
Chapter 6.
As a final enhancement, all but the most trivial code snippets listed in this book are
now available in electronic form, as a notebook of course, but the website includes

1 Internet access is neither required nor used.


HTML and PDF versions, see Section 1.2. The explanatory text surrounding the text is
not included. For that you have to read the book, in hard copy or ebook format!

Note added in proof:


John died shortly after the completion of the Second Edition, and is much missed by
colleagues, friends and family, especially the “Python widow”.
Preface to the First Edition

I have used computers as an aid to scientific research for over 40 years. During that
time, hardware has become cheap, fast and powerful. However, software relevant to the
working scientist has become progressively more complicated. My favourite textbooks
on Fortran90 and C++ run to 1200 and 1600 pages respectively. And then we need doc-
umentation on mathematics libraries and graphics packages. A newcomer going down
this route is going to have to invest significant amounts of time and energy in order to
write useful programmes. This has led to the emergence of “scientific packages” such
as Matlab® or Mathematica® which avoid the complications of compiled languages,
separate mathematics libraries and graphics packages. I have used them and found them
very convenient for executing the tasks envisaged by their developers. However, I also
found them very difficult to extend beyond these boundaries, and so I looked for alter-
native approaches.
Some years ago, a computer science colleague suggested that I should take a look at
Python. At that time, it was clear that Python had great potential but a very flaky imple-
mentation. It was, however, free and open-source, and was attracting what has turned
out to be a very effective army of developers. More recently, their efforts have coordi-
nated to produce a formidable package consisting of a small core language surrounded
by a wealth of add-on libraries or modules. A select group of these can and do replicate
the facilities of the conventional scientific packages. More importantly an informed, in-
telligent user of Python and its modules can carry out major projects usually entrusted
to dedicated programmers using Fortran, C etc. There is a marginal loss of execution
speed, but this is more than compensated for by the vastly telescoped development time.
The purpose of this book is to explain to working scientists the utility of this relatively
unknown resource.
Most scientists will have some computer familiarity and programming awareness,
although not necessarily with Python, and I shall take advantage of this. Therefore,
unlike many books which set out to “teach” a language, this one is not just a brisk trot
through the reference manuals. Python has many powerful but unfamiliar facets, and
these need more explanation than the familiar ones. In particular, if you encounter in
this text a reference to the “beginner” or the “unwary”, it signifies a point which is not
made clear in the documentation, and has caught out this author at least once.
The first seven chapters, plus Appendix A, cover almost everything the working sci-
entist needs to know in order to get started in using Python effectively. My editor and
some referees suggested that I should devote the second half of the book to problems in
a particular field. This would have led to a series of books, “Python for Biochemists”,
“Python for Crystallographers”, . . . , all with a common first half. Instead I have cho-
sen to cover just three topics, which, however, should be far more widely applicable in
many different fields. Chapter 8 covers four radically different types of ordinary differ-
ential equations and shows how to use the various relevant black boxes, which are often
Python wrappers around tried and trusted Fortran codes. The next chapter while os-
tensibly about pseudospectral approaches to evolutionary partial differential equations,
actually covers a topic of great utility to many scientists, namely how to reuse legacy
code, usually written in Fortran77, within Python at Fortran-like speeds, without under-
standing Fortran. The final chapter about solving very large linear systems via multigrid
is also a case history in how to use object-oriented programming meaningfully in a sci-
entific context. If readers look carefully and critically at these later chapters, they should
gain the practical expertise to handle problems in their own field.
Acknowledgments are due to the many Python developers who have produced and
documented a very useful tool, and also to the very many who have published code
snippets on the web, a great aid to the tyro, such as this author. Many of my colleagues
have offered valuable advice. Des Higham generously consented to my borrowing his
ideas for the last quarter of Chapter 8. I am especially grateful to Oliver Rinne who read
carefully and critically an early draft. At Cambridge University Press, my Production
Editor, Jessica Murphy and my Copy Editor, Anne Rix have exhibited their customary
expertise. Last but not least I thank the Department of Applied Mathematics and Theo-
retical Physics, Cambridge for continuing to offer me office space after my retirement,
which has facilitated the production of this book.

Writing a serious book is not a trivial task and so I am rather more than deeply
grateful for the near-infinite patience of Mary, the “Python-widow”, which made this
book possible!
1 Introduction

The title of this book is “Python for Scientists”, but what does that mean? The dictio-
nary defines “Python” as either (a) a non-venomous snake from Asia or Saharan Africa
or (b) a computer scripting language, and it is the second option which is intended here.
(What exactly this second definition means will be explained later.) By “scientist”, I
mean anyone who uses quantitative models either to obtain conclusions by processing
pre-collected experimental data or to model potentially observable results from a more
abstract theory, and who asks “what if?”. What if I analyse the data in a different way?
What if I change the model? Thus the term also includes economists, engineers and
mathematicians among others, as well as the usual concept of scientists. Given the vol-
ume of potential data or the complexity (non-linearity) of many theoretical models, the
use of computers to answer these questions is fast becoming mandatory.
Advances in computer hardware mean that immense amounts of data or ever more
complex models can be processed at increasingly rapid speeds. These advances also
mean reduced costs so that today virtually every scientist has access to a “personal
computer”, either a desktop work station or a laptop, and the distinction between the
two is narrowing quickly. It might seem to be 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 software. But for scientists, this
issue is important and we need to examine it in more detail.

1.1 Scientific Software

Before we discuss what is available, it is important to note that all computer software
comes in one of two types: proprietary and open-source. The first is supplied by a com-
mercial firm. Such organizations have both to pay wages and taxes and to provide a re-
turn for their shareholders. Therefore, they have to charge real money for their products,
and, in order to protect their assets from their competitors, they do not tell the customer
how their software works. Thus, the end-users have little chance of being able to adapt
or optimize the product for their own use. Since wages and taxes are recurrent expendi-
tures, the company needs to issue frequent charged-for updates and improvements (the
Danegeld effect). Open-source software is available for free or at nominal cost (media,
2 Introduction

postage etc.). 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 licences, which give nobody the right to copy-
right 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. Com-
puters operate according to a very limited and obscure set of instructions. A program-
ming language is a somewhat less 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 with a steep learning curve. Only a few language families, e.g., C and
Fortran, have been widely accepted, but they come with many different dialects, e.g.,
Fortran77, Fortran90, Ansi C, C++ etc. Compilers then translate code written by humans
into machine code which can be optimized for speed and then processed. 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. Note that compilers need to be supplemented by libraries of software pack-
ages which implement frequently used numerical algorithms, and graphics packages
will usually be needed. Fast versatile library packages are usually expensive, although
good public domain packages are starting to appear.
A racing car is not usually the best choice for a trip to the supermarket, where speed
is not of paramount importance. Similarly, compiled languages are not always ideal for
trying out new mathematical ideas. Thus for the intended readers of this book the direct
use of compilers is likely to be unattractive, unless their use is mandatory. We there-
fore look at the other type of software, usually called “scientific packages”. Proprietary
packages include Mathematica and Matlab, and open-source equivalents include Max-
ima, Octave, R and SciLab. They all operate in a similar fashion. Each provides its own
idiosyncratic programming language in which problems are entered at a user interface.
After a coherent group of statements, often just an individual statement, has been typed,
the package writes equivalent core language code and compiles it on the fly. Thus errors
and/or results can be reported immediately back to the user. Such packages are called
“interpreters”, and older readers may remember, perhaps with mixed feelings, the BA-
SIC language. For small projects, the slow operation compared with a fully compiled
code is masked by the speed of current microprocessors, but it does become apparent
on larger jobs.
These packages are attractive for at least two reasons. The first is their ability to post-
process data. For example, suppose that x is a real variable and there exists a (possibly
unknown) function y(x). Suppose also that for an ordered set X of discrete instances of
x we have computed a corresponding set Y of instances of y. Then a command similar to
plot(X,Y) will display instantly a nicely formatted graph on the screen. Indeed, those
generated by Matlab in particular can be of publication quality. A second advantage is
the apparent ability of some of the proprietary packages to perform in addition some
1.1 Scientific Software 3

algebraic and analytic processes, and to integrate all of them with their numerical and
graphical properties. A disadvantage of all of these packages is the quirky syntax and
limited expressive ability of their command languages. Unlike the compiled languages,
it is often extremely difficult to program a process which was not envisaged by the
package authors.
The best of the proprietary packages are very easy to use with extensive on-line help
and coherent documentation, which has not yet been matched by all of the open-source
alternatives. However, a major downside of the commercial packages is the extremely
high prices charged for their licences. Most of them offer a cut down “student version”
at reduced price (but usable only while the student is in full-time education) so as to
encourage familiarity with the package. This largesse is paid for by other users.
Let us summarize the position. On the one hand, we have the traditional compiled
languages for numerics which are very general, very fast, very difficult to learn and do
not interact readily with graphical or algebraic processes. On the other, we have standard
scientific packages which are good at integrating numerics, algebra and graphics, but are
slow and limited in scope.
What properties should an ideal scientific package have? A short list might contain:
1. a mature programming language which is both easy to understand and which has
extensive expressive ability,
2. integration of algebraic, numerical and graphical functions,
3. the ability to generate numerical algorithms running with speeds within an order of
magnitude of the fastest of those generated by compiled languages,
4. a user interface with adequate on-line help, and decent documentation,
5. an extensive range of textbooks from which the curious reader can develop greater
understanding of the concepts,
6. open-source software, freely available,
7. implementation on all standard platforms, e.g., Linux, Mac OS X, Unix, Windows,
8. a concise package, and so implementable on even modest hardware.
The bad news is that no single “scientific package” quite satisfies all of these criteria.
Consider, e.g., the requirement of algebraic capability. There are two mature open-
source packages, wx-Maxima and Reduce, with significant algebraic capabilities worthy
of consideration, but Reduce fails requirement 4 and both fail criteria 3 and 5. They are,
however, extremely powerful tools in the hands of experienced users. Python, via the
add-on SymPy, see Chapter 7, almost achieves a high standard of algebraic capability.
SageMath fulfils all but the last of the criteria listed above. It is completely based on
Python and its add-ons, and also includes wx-Maxima. For further details see Chapter
7. Thus a rational strategy is to first master Python. If its, admirably few, weaknesses are
crucial for your work, then investigate SageMath. The vast majority of scientists will
find plenty of utility in Python.
In 1991, Guido van Rossum created Python as an open-source platform-independent
general purpose programming language. It is basically a very simple language sur-
rounded by an enormous library of add-on modules, including complete access to the
underlying operating system. This means that it can manage and manipulate programs
4 Introduction

built from other complete (even compiled) packages, i.e., it is a scripting language. This
versatility has ensured both its adoption by power users such as Google, and a real army
of developers. It means also that it can be a very powerful tool for the scientist. Of
course, there are other scripting languages, e.g., Java™ and Perl®, but none has the
versatility or user-base to meet criteria 3–5 above.
Ten years ago it would not have been possible to recommend Python for scientific
work. The size of the army of developers meant that there were several mutually incom-
patible add-on packages for numerical and scientific applications. Fortunately, reason
has prevailed and there is now a single numerical add-on package, NumPy, and a single
scientific one, SciPy, around which the developers have united. When the first edition
of this book was written SymPy, the Python approach to algebraic manipulation, was
still in a phase of rapid development, and so it was not included. While SymPy has yet
to achieve the capabilities of wx-Maxima and Reduce, it now handles many algebraic
tasks reliably

1.2 The Plan of This Book

The purpose of this intentionally short book is to show how easy it is for the working
scientist to implement and test non-trivial mathematical algorithms using Python. We
have quite deliberately preferred brevity and simplicity to encyclopaedic coverage in
order to get the inquisitive reader up and running as soon as possible. We aim to leave
the reader with a well-founded framework to handle many basic, and not so basic, tasks.
Obviously, most readers will need to dig further into techniques for their particular
research needs. But after reading this book, they should have a sound basis for this.
This chapter and Appendix A discuss how to set up a scientific Python environment.
While the original Python interpreter was pretty basic, its replacement IPython is so
easy to use, powerful and versatile that Chapter 2 is devoted to it, adopting a hands-on
approach.
We now describe the subsequent chapters. As each new feature is described, we try
to illustrate it first by essentially trivial examples and, where appropriate, by more ex-
tended problems. This author cannot know the mathematical sophistication of potential
readers, but in later chapters we shall presume some familiarity with basic calculus,
e.g., the Taylor series in one dimension. However, for these extended problems we shall
sketch the background needed to understand them, and suitable references for further
reading will be given.
Chapter 3 gives a brief but reasonably comprehensive survey of those aspects of the
core Python language likely to be of most interest to scientists. Python is an object-
oriented language, which lends itself naturally to object-oriented programming (OOP),
which may well be unfamiliar to most scientists. We shall adopt an extremely light
touch to this topic. We should perhaps point out that the container objects introduced
in Section 3.5 do not all have precise analogues in, say, C or Fortran. Again the brief
introduction to Python classes in Section 3.9 may be unfamiliar to users of those two
families of languages. The chapter concludes with two implementations of the sieve
1.2 The Plan of This Book 5

of Eratosthenes, which is a classical problem: enumerate all of the prime numbers1


less than a given integer n. A straightforward implementation takes 17 lines of code,
but takes inordinately long execution times once n > 105 . However, a few minutes of
thought and using already described Python features suggests a shorter 13-line program
which runs 3000 times faster and runs out of memory (on my laptop) once n > 108 .
The point of this exercise is that choosing the right approach (and Python often offers
so many) is the key to success in Python numerics.
Chapter 4 extends the core Python language via the add-on module NumPy, to give
a very efficient treatment of real and complex numbers. In the background lurk C/C++
routines to execute repetitive tasks with near-compiled-language speeds. The empha-
sis is on using structures via vectorized code rather than the traditional for-loops or
do-loops. Vectorized code sounds formidable, but, as we shall show, it is much eas-
ier to write than the old-fashioned loop-based approach. Here too we discuss the input
and output of data. First, we look at how NumPy can read and write text files, human-
readable data and binary data. Secondly, we look, very superficially, at data analysis.
We summarize also miscellaneous functions and give a brief introduction to Python’s
linear algebra capabilities. Finally, we review even more briefly a further add-on module
SciPy, which greatly extends the scope of NumPy.
Chapter 5 gives an introduction to the add-on module Matplotlib. This was inspired
by the striking graphics performance of the Matlab package and aspires to emulate or
improve on it for two-dimensional (x, y)-plots. Indeed, almost all of the figures in the
later chapters of the book were produced using Matplotlib. The original figures were
produced in colour using the relevant code snippets. The exigencies of book publish-
ing have required conversion to black, white and many shades of grey. After giving a
range of examples to illustrate its capabilities, we conclude the chapter with a slightly
more extended example, a fully functional 49-line code to compute and produce high-
definition plots of Mandelbrot sets.
The difficulties of extending the discussion to three-dimensional graphics, e.g., rep-
resentations of the surface z = z(x, y) are discussed in Chapter 6. Some aspects of this
can be handled by the Matplotlib module, but for more generality we need to invoke the
Mayavi add-on module, which is given a brief introduction together with some exam-
ple codes. If the use of such graphics is a major interest for you, then you will need to
investigate further these modules.
The final introductory chapter, Chapter 7, is an introduction to the algebraic capabil-
ities of SymPy, and, despite its limitations, you may be pleasantly surprised.
If you already have some Python experience, you can of course omit parts of these
chapters. However, their ethos is a hands-on approach. You are encouraged strongly to
try out the relevant code snippets.2 Once you have understood them, you can deepen
your understanding by modifying them. These “hacking” experiments replace the exer-
cises traditionally included in textbooks.
1 The restriction to integer arithmetic in this chapter is because our exposition of Python has yet to deal
with serious calculations involving real or complex numbers efficiently.
2 All but the shortest code snippets in this book (but not the explanatory surrounding text) are freely
available from http://www.cambridge.org/PfS2.
6 Introduction

These first chapters cover the basic tools that Python provides to enhance the scien-
tist’s computer experience. How should we proceed further?
A notable omission is that apart, from a brief discussion in Section 4.5, the vast
subject of data analysis will not be covered. There are three main reasons for this.
(a) Recently an add-on module called Pandas has appeared. This uses NumPy and Mat-
plotlib to tackle precisely this issue. It comes with comprehensive documentation,
which is described in Section 4.5.
(b) One of the authors of Pandas has written a book, McKinney (2012), which reviews
IPython, NumPy and Matplotlib and goes on to treat Pandas applications in great
detail.
(c) I do not work in this area, and so would simply have to paraphrase the sources
above.
Instead, I have chosen to concentrate on the modelling activities of scientists. One
approach would be to target problems in bioinformatics or cosmology or crystallogra-
phy or engineering or epidemiology or financial mathematics or . . . etc. Indeed, a whole
series of books with a common first half could be produced called “Python for Bioin-
formatics” etc. A less profligate and potentially more useful approach would be to write
a second half applicable to all of these fields, and many more. I am relying here on the
unity of mathematics. Problems in one field when reduced to a core dimensionless form
often look like a similarly reduced problem from another field.
This property can be illustrated by the following example. In population dynamics,
we might study a single species whose population N(T ) depends on time T . Given a
plentiful food supply, we might expect exponential growth, dN/dT = kN(T ), where the
growth constant k has dimension 1/time. However, there are usually constraints limiting
such growth. A simple model to include these is the “logistic equation”
dN
(T ) = kN(T ) (N0 − N(T )) (1.1)
dT
which allows for a stable constant population N(T ) = N0 . The biological background to
this equation is discussed in many textbooks, e.g., Murray (2002).
In (homogeneous spherically symmetric) cosmology, the density parameter Ω de-
pends on the scale factor a via
dΩ (1 + 3w)
= Ω(1 − Ω), (1.2)
da a
where w is usually taken to be a constant.
Now mathematical biology and cosmology do not have a great deal in common, but
it is easy to see that (1.1) and (1.2) represent the same equation. Suppose we scale the
independent variable T in (1.1) by t = kN0 T , which renders the new time coordinate
t dimensionless. Similarly, we introduce the dimensionless variable x = N/N0 so that
(1.1) becomes the logistic equation
dx
= x(1 − x). (1.3)
dt
In a general relativistic theory, there is no reason to prefer any one time coordinate to
1.2 The Plan of This Book 7

any other. Thus we may choose a new time coordinate t via a = et/(1+3w) , and then,
on setting x = Ω, we see that (1.2) also reduces to (1.3). Thus the same equations
can arise in a number of different fields.3 In Chapters 8–10, we have, for brevity and
simplicity, used minimal equations such as (1.3). If the minimal form for your problem
looks something like the one being treated in a code snippet, you can of course hack the
snippet to handle the original long form for your problem.
Chapter 8 looks at four types of problems involving ordinary differential equations.
We start with a very brief introduction to techniques for solving initial value problems
and then look at a number of examples, including two classic non-linear problems, the
van der Pol oscillator and the Lorenz equations. Next we survey two-point boundary
value problems and examine both a linear Sturm–Liouville eigenvalue problem and an
exercise in continuation for the non-linear Bratu problem. Problems involving delay dif-
ferential equations arise frequently in control theory and in mathematical biology, e.g.,
the logistic and Mackey–Glass equations, and a discussion of their numerical solution
is given in the next section. Finally in this chapter we look briefly at stochastic calcu-
lus and stochastic ordinary differential equations. In particular, we consider a simple
example closely linked to the Black–Scholes equation of financial mathematics.
There are two other major Python topics relevant to scientists that I would like to
introduce here. The first is the incorporation of code written in other languages. There
are two aspects of this: (a) the reuse of pre-existing legacy code, usually written in
Fortran, (b) if one’s code is being slowed down seriously by a few Python functions, as
revealed by the profiler, how do we recode the offending functions in Fortran or C? The
second topic is how can a scientific user make worthwhile use of the object-oriented
programming (OOP) features of Python?
Chapter 9 addresses the first topic via an extended example. We look first at how
pseudospectral methods can be used to attack a large number of evolution problems
governed by partial differential equations, either initial value or initial-boundary value
problems. For the sake of brevity, we look only at problems with one time and one
spatial dimension. Here, as we explain, problems with periodic spatial dependence can
be handled very efficiently using Fourier methods, but for problems which are more
general, the use of Chebyshev transforms is desirable. However, in this case there is
no satisfactory Python black box available. It turns out that the necessary tools have
already been written in legacy Fortran77 code. These are listed in Appendix B, and we
show how, with an absolutely minimal knowledge of Fortran77, we can construct ex-
tremely fast Python functions to accomplish the required tasks. Our approach relies on
the NumPy f2py tool which is included in all of the recommended Python distributions.
If you are interested in possibly reusing pre-existing legacy code, it is worthwhile study-
ing this chapter even if the specific example treated there is not the task that you have
in mind. See also Section 1.3 for other uses for f2py.
One of the most useful features of object-oriented programming (OOP) from the
point of view of the scientist is the concept of classes. Classes exist in C++ (but not
3 This example was chosen as a pedagogic example. If the initial value x(0) = x0 is specified, then the exact
solution is x(t) = x0 /[x0 + (1 − x0 )e−t ]. In the current context, x0  0. If x0  1, then all solutions tend
monotonically towards the constant solution x = 1 as t increases. See also Section 8.5.3.
8 Introduction

C) and Fortran90 and later (but not Fortran77). However, both implementations are
complicated and so are usually shunned by novice programmers. In contrast, Python’s
implementation is much simpler and more user-friendly, at the cost of omitting some of
the more arcane features of other language implementations. We give a very brief intro-
duction to the syntax in Section 3.9. However, in Chapter 10 we present a much more
realistic example: the use of multigrid to solve elliptic partial differential equations in
an arbitrary number of dimensions, although for brevity the example code is for two di-
mensions. Multigrid is by now a classical problem which is best defined recursively, and
we devote a few pages to describing it, at least in outline. The pre-existing legacy code
is quite complicated because the authors needed to simulate recursion in languages,
e.g., Fortran77, which do not support recursion. Of course, we could implement this
code using the f2py tool outlined in Chapter 9. Instead, we have chosen to use Python
classes and recursion to construct a simple clear multigrid code. As a concrete example,
we use the sample problem from the corresponding chapter in Press et al. (2007) so
that the inquisitive reader can compare the non-recursive and OOP approaches. If you
have no particular interest in multigrid, but do have problems involving linked math-
ematical structures, and such problems arise often in, e.g., bioinformatics, chemistry,
epidemiology and solid-state physics among others, then you should certainly peruse
this final chapter to see how, if you state reasonably mathematically precisely what your
problems are, then it is easy to construct Python code to solve them.

1.3 Can Python Compete with Compiled Languages?

The most common criticism of Python and the scientific software packages is that they
are far too slow, in comparison with compiled code, when handling complicated realistic
problems. The speed-hungry reader might like to look at a recent study4 of a straight-
forward “number-crunching” problem treated by various methods. Although the figures
given in the final section refer to one particular problem treated on a single processor,
they do give a “ball park” impression of performance. As a benchmark, they use the
speed of a fully compiled C++ program which solves the problem. A Python solution
using the technique of Chapter 3, i.e., core Python, is about 700 times slower. Once
you use the floating-point module NumPy and the techniques described in Chapter 4
the code is only about ten times slower, and the Matlab performance is estimated to be
similar. However, as the study indicates, there are a number of ways to speed up Python
to about 80% of the C++ performance. Some of these are very rewarding exercises in
computer science.
One in particular, though, is extremely useful for scientists: the f2py tool. This is
discussed in detail in Chapter 9, where we show how we can reuse legacy Fortran code.
It can also be used to access standard Fortran libraries, e.g., the NAG libraries.5 Yet
another use is to speed up NumPy code and so improve performance! To see how this
works, suppose we have developed a program such as those outlined in the later sections
4 See http://wiki.scipy.org/PerformancePython.
5 See, e.g., http://www.nag.co.uk/doc/TechRep/pdf/TR1_08.pdf.
1.4 Limitations of This Book 9

of the book, which uses a large number of functions, each of which carries out a simple
task. The program works correctly, but is unacceptably slow. Note that getting detailed
timing data for Python code is straightforward. Python includes a “profiler” which can
be run on the working program. This outputs a detailed list of the functions ordered by
the time spent executing them. It is very easy to use, and this is described in Section 2.5.
Usually, there are one or two functions which take a very long time to execute simple
algorithms.
This is where f2py comes into its own. Because the functions are simple, even begin-
ners can soon create equivalent code in, say, Fortran77 or Ansi C. Also, because what
we are coding is simple, there is no need for the elaborate (and laborious to learn) fea-
tures of, say, Fortran95 or C++. Next we encapsulate the code in Python functions using
the f2py tool, and slot them into the Python program. With a little experience, we can
achieve speeds comparable to that of a program written fully in, say, Fortran95.

1.4 Limitations of This Book

A comprehensive treatment of Python and its various branches would occupy several
large volumes and would be out of date before it reached the bookshops. This book
is intended to offer the reader a starting point which is sufficient to be able to use the
fundamental add-on packages. Once the reader has a little experience with what Python
can do, it is time to explore further those areas which interest the reader.
I am conscious of the fact that I have not even mentioned vitally important concepts,
e.g., finite-volume methods for hyperbolic problems,6 parallel programming and real-
time graphics to name but a few areas in which Python is very useful. There is a very
large army of Python developers working at the frontiers of research, and their endeav-
ours are readily accessed via the internet. Please think of this little book as a transport
facility towards the front line.

1.5 Installing Python and Add-ons

Users of Matlab and Mathematica are used to a customized Integrated Development


Environment (IDE). From the start-up screen, you can investigate code, write, edit and
save segments using the built-in editor, and run actual programs. Since the operating
systems Mac OS X and most flavours of Linux include a version of core Python as a
matter of course, many computer officers and other seasoned hackers will tell you that
it is simple to install the additional packages, and you can be up and coding within the
hour, thus ameliorating the difference.
Unfortunately, the pundits are wrong. The Python system being advocated in this
book runs the language to its extreme limits, and all of the add-ons must be compatible
with each other. Like many others, this author has endured hours of frustration trying to
6 The well-regarded Clawpack package http://depts.washington.edu/clawpack, which is
Fortran-based, has switched from Matlab to Python Matplotlib for its graphics support.
10 Introduction

pursue the pundits’ policy. Please save your energy, sanity etc., and read Appendix A,
which I have quite deliberately targeted at novices, for the obvious reason!
Admittedly, there is an amount, albeit slight and low-level, of hassle involved here.
So what’s the payoff? Well, if you follow the routes suggested in Appendix A, you
should end up with a system which works seamlessly. While it is true that the original
Python interpreter was not terribly user-friendly, which caused all of the established IDE
purveyors to offer a “Python mode”, the need which they purported to supply has been
overtaken by the enhanced interpreter IPython. Indeed, in its latest versions IPython
hopes to surpass the facilities offered by Matlab, Mathematica and the Python-related
features of commercial IDEs. In particular, it allows you to use your favourite editor,
not theirs, and to tailor its commands to your needs, as explained in Appendix A and
Chapter 2.
2 Getting Started with IPython

This sounds like software produced by Apple®, but it is in fact a Python interpreter on
steroids. It has been designed and written by scientists with the aim of offering very fast
exploration and construction of code with minimal typing effort, and offering appro-
priate, even maximal, on-screen help when required. Documentation and much more is
available on the website.1 This chapter is a brief introduction to the essentials of using
IPython. A more extended discursive treatment can be found in, e.g., Rossant (2015).
In this chapter we shall concentrate on notebook and terminal modes, and we assume
that the reader has set up the environments as described in Sections A.2 and A.3. Before
we get to realistic examples, I must ask for the impatient reader’s forbearance. Tab com-
pletion, Section 2.1, is an unusual but effective method for minimizing key-strokes, and
the introspection feature, Section 2.2 shows how to generate relevant inline information
quickly, without pausing to consult the manual.

2.1 Tab Completion

While using the IPython interpreter, tab completion is always present. This means that,
whenever we start typing a Python-related name on a line or in a cell, we can pause
and press the tab key, to see a list of names valid in this context, which agree with the
characters already typed.
As an example, suppose we need to type import matplotlib.2 Typing itab re-
veals 15 possible completions. By inspection, only one of them has second letter m, so
that imtab will complete to import. Augmenting this to import mtab shows 30 pos-
sibilities, and by inspection we see that we need to complete the command by import
matptab to complete the desired line.
That example was somewhat contrived. Here is a more compulsive reason for using
tab completion. When developing code, we tend, lazily, to use short names for vari-
ables, functions etc. (In early versions of Fortran, we were indeed restricted to six or
eight characters, but nowadays the length can be arbitrary.) Short names are not always
meaningful ones, and the danger is that if we revisit the code in six months, the intent of
the code may no longer be self-evident. By using meaningful names of whatever length

1 It can be found at www.ipython.org.


2 matplotlib is essential to scientific graphics and forms the main topic of Chapter 5.
12 Getting Started with IPython

is needed, we can avoid this trap. Because of tab completion, the full long name only
need be typed once.

2.2 Introspection

IPython has the ability to inspect just about any Python construct, including itself, and
to report whatever information its developers have chosen to make available. This fa-
cility is called introspection. It is accessed by the single character ?. The easiest way to
understand it is to use it, so you are recommended to fire up the interpreter.
Which mode, terminal or notebook, should one use? Beginners should start with ter-
minal mode, as described in Section A.3, thus removing one level of complexity. This
persists throughout this chapter until Section 2.5, where the code snippets are extremely
short. If you choose to use terminal mode, type ipython followed by a ret on the com-
mand line. IPython will respond with quite a lengthy header, followed by an input line
labelled In [1]:. Now that you are in IPython, you can try out introspection by typing
? (followed by ret) on the input line. (Note that within IPython terminal mode the ret
actually executes the command in the current line.) IPython responds by issuing in pager
mode3 a summary of all of the facilities available. If you exit this, then the command
quickref (hint: use tab completion) gives a more concise version. Very careful study
of both documents is highly recommended.
IPython notebook users need to use slightly different commands. After invoking the
notebook, see Section A.2.2 for details, they will be confronted with an unnumbered
single-line blank cell. Now they can try out introspection by typing ? on the input line.
Typing ret at this point merely adds a new line to the cell. In order to execute the
command(s) in the cell one needs to add either shift+ret (which also creates a new cell
below the current one), or ctl+ret which just executes the command(s). The output, the
lengthy facilities summary, appears in a scrollable window at the bottom of the screen.
It can be killed by clicking on the x-button at the top right of the window. The command
quickref (hint: use tab completion) followed by ctl-ret gives a more concise version.
Again very careful study of both documents is highly recommended.
However, scientists are impatient folk, and the purpose of this chapter is to get them
up and running with the most useful features. Therefore, we need to type in some Python
code, which newcomers will have to take on trust until they have mastered Chapters 3
and 4. Again, depending on whether you are operating in notebook or console mode,
the procedures differ slightly.
Notebook users should type each boxed line or boxed code snippet into a cell. They
can then execute the code via ctl+ret or shift+ret. Readers using terminal mode
3 This is based on the Unix less facility. To use it effectively you need know only four commands:

• space gives the next screen,


• b gives the previous screen,
• h gives the help screen, which includes less frequently used commands,
• q quits.
2.2 Introspection 13

should type in, line by line, boxed lines or code snippets and enter each line separately
using the ret key.
For example, please type in

a=3
b=2.7
c=12 + 5j
s=’Hello World!’
L=[a, b, c, s, 77.77]

The first two lines mean that a refers to an integer and


√ b to a float (floating-point num-
ber). Python uses√ the engineers’ convention whereby −1 = j. (Mathematicians would
have preferred −1 = i.) Then c refers to a complex number.4 Typing each of a, b and
c on a line5 by itself and executing it, reveals the value of the object to which the identi-
fier refers. Note that there is a useful shortcut to display multiple values – try a, b, c
on a single line. Next try c? on a line by itself. This confirms that c does indeed refer
to a complex number, shows how it will be displayed and points out that another way
of creating it would have been c=complex(12,5). Next try c. immediately followed
by tab. The interpreter will immediately offer three possible completions. What do they
mean? Here it is almost obvious, but try c.real?. (Using tab completion, you don’t
need to type eal.) This reveals that c.real is a float with the value 12, i.e., the real
part of the complex number. The newcomer might like to check out c.imag. Next try
out c.conjugate?. (Again only five keystrokes plus return are needed!) This reveals
that c.conjugate is a function, to be used, e.g., as cc = c.conjugate().
The notation here might seem rather strange to users of say Fortran or C, where
real(c) or conjugate(c) might have been expected. The change in syntax comes
about because Python is an object-oriented language. The object here is 12+5j, referred
to as c. Then c.real is an enquiry as to the value of a component of the object. It
changes nothing. However, c.conjugate() either alters the object or, as here, creates
a new one, and is hence a function. This notation is uniform for all objects, and is
discussed in more detail in Section 3.10.
Returning to the snippet, typing s by itself on a line prints a string. We can confirm
this by the line s?, and s. followed by a tab reveals 38 possible completions associated
with string objects. The reader should use introspection to reveal what some of them do.
Similarly, L.? shows that L is a list object with nine available completions. Do try a few
of them out! As a general rule, the use of introspection and tab completion anywhere
in Python code should generate focused documentation. There is a further introspection
command ?? which, where appropriate, will reveal the original source code of a func-
tion, and an example will be given later in Section 2.5. (The object functions we have
so far encountered are built-in, and were not coded in Python!)

4 Note that in the code snippet there is no multiplication sign (*) between 5 and j.
5 For the sake of brevity, we shall not distinguish between a line in terminal mode and a one-line cell in
notebook mode.
14 Getting Started with IPython

2.3 History

If you look at the output from the code in the previous section, you will see that IPython
employs a history mechanism which is very similar to that in Mathematica notebooks.
Input lines are labelled In[1], In[2], . . . , and if input In[n] produces any output, it
is labelled Out[n]. As a convenience, the past three input lines/cells are available as
_i, _ii and _iii, and the corresponding output lines/cells are available as _, __ and
___. In practice though, you can insert the content of a previous input line/cell into the
current one by navigating using ↑ (or ctl-p) and ↓ (or ctl-n), and this is perhaps the
most common usage. Unusually, but conveniently, history persists in terminal mode. If
you close down IPython (using exit) and then restart it, the history of the previous
session is still available via the arrow keys. There are many more elaborate things you
can do with the history mechanism, try the command history?.

2.4 Magic Commands

The IPython interpreter expects to receive valid Python commands. However, it is very
convenient to be able to type commands which control either the behaviour of IPython
or that of the underlying operating system. Such commands, which coexist with Python
ones, are called magic commands. A very long very detailed description can be found
by typing %magic in the interpreter, and a compact list of available commands is given
by typing %lsmagic. (Do not forget tab completion!) Note that there are two types of
magic, line magic, prefixed by %, and cell magic, prefixed by %%. The latter is relevant
only to notebook mode. You can get very helpful documentation on each command by
using introspection.
Let us start by considering system commands. A harmless example is pwd, which
comes from the Unix operating system where it just prints the name of the current
directory (print working directory) and exits. There are usually three ways to achieve
this in the IPython window. You should try out the following commands.

!pwd

Nothing in Python starts with a “!”, and IPython interprets this as the Unix shell com-
mand pwd, and produces the ASCII answer.

%pwd

Nothing in Python starts with a “%”, and IPython treats this as a line magic command,
interpreting it as the shell command. The u indicates that the string is encoded in Uni-
code, which enables a rich variety of outputs. Unicode is mentioned briefly in Section
A.3.1.

pwd
2.5 IPython in Action: An Extended Example 15

Here is a subtle, but useful, feature. For line magic commands, the % prefix is not al-
ways necessary, see the discussion of %automagic below. The interpreter sees no other
meaning for pwd and so treats it as %pwd.
pwd=’hoho’

pwd

Now pwd is assigned to a string and the magic is switched off.


%pwd

Here the intent is unambiguous.


del pwd

pwd

Now that pwd has no other assignment, it works as a line magic command.
%automagic?

If %automagic is on, as it is by default, the single per cent sign (%) which starts all line
magic commands, can be omitted. This is a great convenience, provided one remembers
that one is dealing with a magic command.
Magic cell commands start with two mandatory per cent signs. They operate on entire
cells, and can be extremely useful, see, e.g., the next section.

2.5 IPython in Action: An Extended Example

For the rest of this chapter we present the first part of an extended example, in order to
show the effectiveness of magic commands. The second part is the example, in Section
3.9, where we consider how to implement arbitrary precision real arithmetic via frac-
tions. There is an issue with fractions, e.g., 3/7 and 24/56 are usually regarded as the
same number. Thus there is a problem here, to determine the “highest common factor”
of two integers a and b, or, as mathematicians are wont to say, their “greatest common
divisor” (GCD), which can be used to produce a canonical form for the fraction a/b.
(By inspection of factors, the GCD of 24 and 56 is 8, which implies 24/56 = 3/7 and no
further reduction of the latter is possible.) Inspection of factors is not easily automated,
and a little research, e.g., on the web, reveals Euclid’s algorithm. To express this con-
cisely, we need a piece of jargon. Suppose a and b are integers. Consider long division
of a by b. The remainder is called a mod b, e.g., 13 mod 5 = 3, and 5 mod 13 = 5. Now
denote the GCD of a and b by gcd(a, b). Euclid’s algorithm is most easily described
recursively via
gcd(a, 0) = a, gcd(a, b) = gcd(b, a mod b), (b  0). (2.1)
16 Getting Started with IPython

Try evaluating by hand gcd(56, 24) according to this recipe. It’s very fast! It can be
shown that the most laborious case arises when a and b are consecutive Fibonacci num-
bers, so they would be useful for a test case. The Fibonacci numbers Fn are defined
recursively via
F0 = 0, F1 = 1, Fn = Fn−1 + Fn−2 , n  2. (2.2)
The sequence begins 0, 1, 1, 2, 3, 5, 8, . . ..
How do we implement both Euclid’s algorithm and Fibonacci numbers efficiently
and speedily in Python? We start with the Fibonacci task because it looks to be more
straightforward.
In order to get started with mastering IPython, the novice reader is asked to take
on trust the next two code snippets. A partial explanation is offered here, but all of
the features will be explained more thoroughly in Chapter 3. However, an important
point needs to be made here. Every programming language needs subsidiary blocks of
code, e.g., the contents of functions or do-loops. Most delineate them by some form of
bracketing, but Python relies exclusively on indentation. All blocks of code must have
the same indentation. The need for a sub-block is usually indicated by a colon (:). A
sub-block is further indented (four spaces is the conventional choice), and the end of
the sub-block is indicated by the cancellation of this extra indentation. IPython and all
Python-aware editors will handle this automatically. Three examples are given below.
The novice reader should try to understand, perhaps roughly, what is going on, before
we move on to consider possible workflows to execute the snippets.
1 # File: fib.py Fibonacci numbers
2

3 """ Module fib: Fibonacci numbers.


4 Contains one function fib(n).
5 """
6

7 def fib(n):
8 """ Returns n’th Fibonacci number. """
9 a,b=0,1
10 for i in range(n):
11 a,b=b,a+b
12 return a
13

14 ####################################################
15 if __name__ == "__main__":
16 for i in range(1001):
17 print "fib(",i,") = ",fib(i)

The details of Python syntax are explained in Chapter 3. For the time being, note that
lines starting with a hash (#), e.g., lines 1 and 14, denote comments. Also lines 3–5
define a docstring, whose purpose will be explained shortly. Lines 7–12 define a Python
function. Note the point made above that every colon (:) demands an indentation. Line 7
2.5 IPython in Action: An Extended Example 17

is the function declaration. Line 8 is the function docstring, again soon to be explained.
Line 9 introduces identifiers a and b, which are local to this function, and refer initially
to the values 0 and 1 respectively. Next examine line 11, ignoring for the moment its in-
dentation. Here a is set to refer to the value that b originally referred to. Simultaneously,
b is set to refer to the sum of values originally referred to by a and b. Clearly, lines 9
and 11 replicate the calculations implicit in (2.2). Now line 10 introduces a for-loop or
do-loop, explained in Section 3.7.1, which extends over line 11. Here range(n) gener-
ates a dummy list with n elements, [0, 1, . . . , n − 1], and so line 11 is executed precisely
n times. Finally, line 12 exits the function with the return value set to that referred to
finally by a.
Naturally, we need to provide a test suite to demonstrate that this function behaves as
intended. Line 14 is simply a comment. Line 15 will be explained soon. (When typing it,
note that there are four pairs of underscores.) Because it is an if statement terminated by
a colon, all subsequent lines need to be indented. We have already seen the idea behind
line 16. We repeat line 17 precisely 1001 times with i = 0, 1, 2, . . . , 1000. It prints a
string with four characters, the value of i, another string with four characters, and the
value of fib(i).
We now present two possible workflows for creating and using this snippet.

2.5.1 An IPython terminal workflow


Using your chosen editor first create a file fib.py in the directory where IPython is
running, which holds the content of the code snippet, and then save it. Then, within the
IPython window, issue the (magic) command run fib. If your creation was syntacti-
cally correct, the response will be 1001 lines of Fibonacci values. If not, the response
will indicate the first error encountered. Returning to the editor window, correct and
save the source. Then try the run fib command again. (Beginners must expect to go
through this cycle several times, but it is quick!)

2.5.2 An IPython notebook workflow


Open a new cell and type the code snippet into it. It is then prudent to save the notebook
(esc followed by s). Next run the cell, using ctl+return. If your creation was syntac-
tically correct, the response will be 1001 lines of Fibonacci values. If not, the response
will indicate the first error encountered. Return to the cell, correct it and run the cell
again (Beginners must expect to go through this cycle several times, but it is quick!)
Once you are happy with the program, return to the cell, and insert right at the front the
cell magic command
%%writefile fib.py

Now run the cell again. This write the cell content to the file fib.py in the current
directory, or overwrites that file if it already existed.
Once the program has been verified, we can ask how fast is it? Run the program again
but with the enhanced magic command run -t fib and IPython will produce timing
18 Getting Started with IPython

data. On my machine, the “User time” is 0.05 s, but the “Wall time” is 0.6 s. Clearly,
the discrepancy reflects the very large number of characters printed to the screen. To
verify this, modify the snippet as follows. Comment out the print statement in line 17 by
inserting a hash (#) character at the start of the line. Add a new line 18: fib(i), being
careful to get the indentation correct. (This evaluates the function, but does nothing with
the value.) Now run the program again. On my machine it takes 0.03 s, showing that
fib(i) is fast, but printing is not. (Don’t forget to comment out line 18, and uncomment
in line 17!)
We still need to explain the docstrings in lines 3–5 and 8, and the weird line 15. Close
down IPython (in terminal mode, use exit) and then reopen a fresh version. Type the
single line import fib, which reflects the core of the filename. The tail .py is not
needed. We have imported an object fib. What is it? Introspection suggests the com-
mand fib?, and IPython’s response is to print the docstring from lines 3–5 of the snip-
pet. This suggests that we find out more about the function fib.fib, so try fib.fib?,
and we are returned the docstring from line 8. The purpose of docstrings, which are
messages enclosed in pairs of triple double-quotes, is to offer online documentation to
other users and, just as importantly, you in a few days time! However, introspection has
a further trick up its sleeve. Try fib.fib?? and you will receive a listing of the source
code for this function!
You should have noticed that import fib did not list the first 1001 Fibonacci num-
bers. Had we instead, in a separate session, issued the command run fib, they would
have been printed! Line 15 of the snippet detects whether the file fib.py is being im-
ported or run, and responds accordingly without or with the test suite. How it does this
is explained in Section 3.4.
Now we return to our original task, which was to implement the gcd function implicit
in equation (2.1). Once we recognize that (i) Python has no problem with recursion, and
(ii) a mod b is implemented as a%b, then a minimal thought solution suggests itself, as
in the following snippet. (Ignore for the time being lines 14–18.)

1 # File gcd.py Implementing the GCD Euclidean algorithm.


2

3 """ Module gcd: contains two implementations of the Euclid


4 GCD algorithm, gcdr and gcd.
5 """
6

7 def gcdr(a,b):
8 """ Euclidean algorithm, recursive vers., returns GCD. """
9 if b==0:
10 return a
11 else:
12 return gcdr(b,a%b)
13

14 def gcd(a,b):
15 """ Euclidean algorithm, non-recursive vers., returns GCD. """
2.5 IPython in Action: An Extended Example 19

16 while b:
17 a,b=b,a%b
18 return a
19

20 ##########################################################
21 if __name__ == "__main__":
22 import fib
23

24 for i in range(963):
25 print i, ’ ’, gcd(fib.fib(i),fib.fib(i+1))

The only real novelty in this snippet is the import fib statement in line 22, and we
have already discussed its effect above. The number of times the loop in lines 24 and 25
is executed is crucial. As printed, this snippet should run in a fraction of a second. Now
change the parameter 963 in line 24 to 964, save the file, and apply run gcd again. You
should find that the output appears to be in an infinite loop, but be patient. Eventually,
the process will terminate with an error statement that the maximum recursion depth
has been exceeded. While Python allows recursion, there is a limit on the number of
self-calls that can be made.
This limitation may or may not be a problem for you. But it is worth a few moments
thought to decide whether we could implement Euclid’s algorithm (2.1) without using
recursion. I offer one possible solution in the function gcd implemented in lines 14–18
of the snippet. Lines 16 and 17 define a while loop, note the colon terminating line 16.
Between while and the colon, Python expects an expression which evaluates to one of
the Boolean values True or False. As long as True is found, the loop executes line 17,
and then retests the expression. If the test produces False, then the loop terminates
and control passes to the next statement, line 18. In the expected context, b will always
be an integer, so how can an integer take a Boolean value? The answer is remarkably
simple. The integer value zero is always coerced to False, but all non-zero values
coerce to True. Thus the loop terminates when b becomes zero, and then the function
returns the value a. This is the first clause of (2.1). The transformation in line 17 is the
second clause, so this function implements the algorithm. It is shorter than the recursive
function, can be called an arbitrary number of times and, as we shall see, runs faster.
So was the expenditure of thought worthwhile? Using the run command, we can
obtain revealing statistics. First, edit the snippet to make 963 loops with the gcdr func-
tion, and save it. Now invoke run -t gcd to obtain the time spent. On my machine, the
“User time” is 0.31 s. Yours will vary, but it is relative timings that matter. The “Wall
time” reflects the display overhead and is not relevant here. Next invoke run -p gcd,
which invokes the Python profiler. Although you would need to read the documenta-
tion to understand every facet of the resulting display, a little scientific intuition can be
very useful. This shows that there were 963 direct calls (as expected) of the function
gcdr, within a total of 464,167 actual calls. The actual time spent within this func-
tion was 0.237 s. Next there were 1926 calls (as expected) of the function fib, and the
time expended was 0.084 s. Note that these timings cannot be compared with the older
20 Getting Started with IPython

run -t gcd ones, because the newer ones include the profiler overhead, which is sig-
nificant here. However, we can conclude that about 74% of the time was spent in the
function gcdr.
Next we need to repeat the exercise for the gcd function. Amend line 25 of the snippet
to replace gcdr by gcd and resave the file. Now run -t gcd to get a “User time” of
0.20 s. The other command run -p gcd reveals that the 1926 calls of function fib
took 0.090 s. However, the function gcd was called only 993 times (as expected), which
occupied 0.087 s. Thus gcd occupied about 49% of the time taken. Very approximately,
these relative timings factor out the profiler overhead. Now 74% of the 0.31 s timing
for the recursive version is 0.23 s, while 49% of the 0.20 s time for the non-recursive
version is 0.098 s. Thus the expenditure of thought has produced a shortened code which
runs in 43% of the time of the “thoughtless code”!
There are two points which need to be gleaned from this example.
1. The IPython magic command run or %run is the Python workhorse. You do need,
via introspection, to study its docstring. Look also at the variants %run -t and
%run -p. It is also worthwhile introspecting %timeit at this stage.
2. You will see much in the literature about methods for “speeding up” Python. These
are often very clever pieces of software engineering. But none are as effective as
human ingenuity!
3 A Short Python Tutorial

Although Python is a small language, it is a very rich one. It is very tempting, when
writing a textbook, to spell out all of the ramifications, concept by concept. The obvious
example is the introductory tutorial from the originator of Python, Guido van Rossum.
This is available in electronic form as the tutorial in your Python documentation or
on-line1 or as hard copy (van Rossum and Drake Jr. (2011)). It is relatively terse at
150 printed pages, and does not mention NumPy. My favourite textbook, Lutz (2013),
runs to over 1500 pages, a marathon learning curve, and mentions NumPy only in pass-
ing. It is excellent at explaining the features in detail, but is too expansive for a first
course in Python. A similar criticism can be applied to two books with a more scientific
orientation, Langtangen (2009) and Langtangen (2014), both around 800 pages, with a
significant overlap between them. I recommend these various books among many others
for reference, but not for learning the language.
Very few people would learn a foreign language by first mastering a grammar text-
book and then memorizing a dictionary. Most start with a few rudiments of grammar
and a tiny vocabulary. Then by practice they gradually extend their range of constructs
and working vocabulary. This allows them to comprehend and speak the language very
quickly, and it is the approach to learning Python that is being adopted here. The dis-
advantage is that the grammar and vocabulary are diffused throughout the learning pro-
cess, but this is ameliorated by the existence of textbooks, such as those cited in the first
paragraph.

3.1 Typing Python

Although the narrative can simply be read, it is extremely helpful to have the IPython
terminal to hand, so that you can try out the code samples. For longer code snippets,
e.g., those in Sections 3.9 and 3.11, it is advisable to use either notebook mode, or
terminal mode together with an editor, so that you can save the code. Your choices are
described in Sections A.2 and A.3 of Appendix A. After trying out the code snippets,
you are strongly encouraged to try out your own experiments in the interpreter.
Every programming language includes blocks of code, which consist of one or more
lines of code forming a syntactic whole. Python uses rather fewer parentheses () and
braces {} than other languages, and instead uses indentation as a tool for formatting
1 It is available at http://docs.python.org/2/tutorial.
22 A Short Python Tutorial

blocks. After any line ending in a colon, :, a block is required, and it is differentiated
from the surrounding code by being consistently indented. Although the amount is not
specified, the unofficial standard is four spaces. IPython and any Python-aware text
editor will do this automatically. To revert to the original indentation level, use the
ret key to enter a totally empty line. Removing braces improves readability, but the
disadvantage is that each line in a block must have the same indentation as the one
before, or a syntax error will occur.
Python allows two forms of comments. A hash symbol, #, indicates that the rest of
the current line is a comment, or more precisely a “tweet”. A “documentation string” or
docstring can run over many lines and include any printable character. It is delimited by
a pair of triple quotes, e.g.,

""" This is a very short docstring. """

For completeness, we note that we may place several statements on the same line, pro-
vided we separate them with semicolons, but we should think about readability. Long
statements can be broken up with the continuation symbol ‘\’. More usefully, if a state-
ment includes a pair of brackets, (), we can split the line at any point between them
without the need for the continuation symbol. Here are simple examples.

a=4; b=5.5; c=1.5+2j; d=’a’


e=6.0*a-b*b+\
c**(a+b+c)
f=6.0*a-b*b+c**(
a+b+c)
a, b, c, d, e, f

3.2 Objects and Identifiers

Python deals exclusively with objects and identifiers. An object may be thought of as a
region of computer memory containing both some data and information associated with
those data. For a simple object, this information consists of its type and its identity,2 i.e.,
the location in memory, which is of course machine dependent. The identity is therefore
of no interest for most users. They need a machine-independent method for accessing
objects. This is provided by an identifier, a label which can be attached to objects. It is
made up of one or more characters. The first must be a letter or underscore, and any sub-
sequent characters must be digits, letters or underscores. Identifiers are case-sensitive:
x and X are different identifiers. (Identifiers which have leading and/or trailing under-
scores have specialized uses, and should be avoided by the beginner.) We must avoid
using predefined words, e.g., list, and should always try to use meaningful identifiers.
However, the choice among, say xnew, x_new and xNew is a matter of taste. Consider
2 An unfortunate choice of name, not to be confused with the about-to-be-defined identifiers.
3.2 Objects and Identifiers 23

  

     

  





  

Figure 3.1 A schematic representation of assignments in Python. After the first command
p=3.14, the float object 3.14 is created and identifier p is assigned to it. Here the object is
depicted by its identity, a large number, its address in the memory of my computer (highly
machine-dependent) where the data are stored, and the type. The second command q=p assigns
identifier q to the same object. The third command p=’pi’ assigns p to a new “string” object,
leaving q pointing to the original float object.

the following code, which makes most sense if typed in, line-by-line, in the terminal
window.

1 p=3.14
2 p
3 q=p
4 p=’pi’
5 p
6 q

Note that we never declared the type of the object referred to by the identifier p. We
would have had to declare p to be of type “double” in C and “real*8” in Fortran. This is
no accident or oversight. A fundamental feature of Python is that the type belongs to
the object, not to the identifier.3
Next, in line 3, we set q=p. The right-hand side is replaced by whatever object p
pointed to, and q is a new identifier which points to this object, see Figure 3.1. No
equality of identifiers q and p is implied here! Notice that, in line 4, we reassign the
identifier p to a “string” object. However, the original float object is still pointed to by
the identifier q, see Figure 3.1, and this is confirmed by the output of lines 5 and 6. Sup-
pose we were to reassign the identifier q. Then, unless in the interim another identifier
had been assigned to q, the original “float” object would have no identifier assigned to
it and so becomes inaccessible to the programmer. Python will detect this automatically
and silently free up the computer memory, a process known as garbage collection.
3 The curious can find the type of an object with identifier p with the command type(p) and its identity
with id(p).
24 A Short Python Tutorial

Because of its importance in what follows we emphasize the point that the basic
building block in Python is the assignment operation, which despite appearances has
nothing to do with equality. In pseudocode,
<identifier>=<object>

which will appear over and over again. As we have already stated earlier, the type of
an object “belongs” to the object and not to any identifier assigned to it. Henceforth we
shall try to be less pedantic!
Since we have introduced a “float”, albeit informally, we turn next to a simple class
of objects.

3.3 Numbers

Python contains three simple types of number objects, and we introduce a fourth, not so
simple, one.

3.3.1 Integers
Python refers to integers as ints. Early versions supported integers only in the range
[−231 , 231 − 1], but in recent versions the range is considerably larger and is now limited
only by the availability of memory.
The usual operations of addition (+), subtraction (−) and multiplication (∗) are of
course available. There is a slight problem with division, for though p and q may be
integers, p/q need not. We may assume without loss of generality that q > 0, in which
case there exist unique integers m and n with
p = mq + n, where 0  n < q.
Then integer division in Python is defined by p//q, which returns m. The remainder n
is available as p%q. Exponentiation pq is also available as p**q, and can produce a real
number if q < 0.

3.3.2 Real numbers


Floating-point numbers are readily available as floats. In most installations, the default
will be approximately 16 digits of precision for floats in the range (10−308 , 10308 ). These
correspond to doubles in the C-family of languages and real*8 in the Fortran family.
The notation for float constants is standard, e.g.,
-3.14, -314e-2, -314.0e-2, -0.00314E3

all represent the same float.


The usual arithmetic rules for addition, subtraction, multiplication, division and ex-
ponentiation are available for floats. For the first three, mixed mode operations are im-
plemented seamlessly, e.g., if addition of an int and a float is requested, then the int
3.3 Numbers 25

is automatically widened to be a float. The same applies for division if one operand is
an int and the other is a float. However, if both are ints, e.g., ±1/5, what is the result?
Earlier versions (< 3.0) of Python adopt integer division, 1/5=0 and -1/5=-1, while
versions  3.0 use real division, 1/5=0.2 and -1/5=-0.2. This is a potential pitfall
which is easily avoided. Either use integer division // or widen one of the operands to
ensure an unambiguous result.
Python has a useful feature inherited from its C roots. Suppose we wish to increment
the float referred to by a by two. Clearly the code

temp=a+2
a=temp

will work. However, it is faster and more efficient to use the single instruction

a+=2

The same applies of course to the other arithmetic operators.


Widening of an int to a float is available explicitly, e.g., float(4) will return either
4. or 4.0 . Narrowing of a float to an int is defined by the following algorithm. If x is
real and positive, then there exist an integer m and a float y such that

x = m + y, where 0  y < 1.0.

In Python, this narrowing is implemented via int(x), which returns m. If x is nega-


tive, then int(x)=-int(-x), succinctly described as “truncation towards zero”, e.g.,
int(1.4)=1 and int(-1.4)=-1.
In programming languages, we expect a wide range of familiar mathematical func-
tions to be available. Fortran builds in the most commonly used ones, but in the C-family
of languages we need to import them with a statement such as #include math.h at the
top of the program. Python also requires a module to be imported, and as an example we
consider the math module, which includes many standard mathematical functions for
real numbers. (Modules are defined in Section 3.4.) Suppose first that we do not know
what the math module contains. The following snippet first loads the module and then
lists the identifiers of its contents.

import math
dir(math) # or math.<TAB> in IPython

To find out more about the individual objects, we can either consult the written docu-
mentation or use the built-in help, e.g., in IPython

math.atan2? # or help(math.atan2)

If one is already familiar with the contents, then a quick-and-dirty-fix is to replace the
import command above by

from math import *


26 A Short Python Tutorial

anywhere in the code before invoking the functions. Then the function mentioned above
is available as atan2(y,x) rather than math.atan2(y,x), which at first sight looks
appealing. However, there is a another module, cmath, which includes many standard
mathematical functions for complex numbers. Now suppose we repeat the quick-and-
dirty fix.
from cmath import *

Then what does atan2(y,x) refer to? It is unambiguous to widen a real to a complex
number, but not in the other direction! Note that, unlike C, the import command can
occur anywhere in the program before its contents are needed, so chaos is waiting,
imperturbably, to wreck your calculation! Of course Python knows about this, and the
recommended workflow is described in Section 3.4.

3.3.3 Boolean numbers


For completeness, we include here Boolean numbers or bool, which are a subset of
the ints with two possible values, True and False, roughly equivalent to 1 (one) and 0
(zero).
Suppose that box and boy refer to bools. Then expressions such as “not box”,
“box and boy” and “box or boy” take on their usual meanings.
The standard equality operators are defined for ints and floats x, y, e.g., x==y (equal-
ity), x!=y (inequality). As a simple exercise, to remind you of the limitations of Python
floats, guess the result of the following line, next type it and then explain the result.
math.tan(math.pi/4.0)==1.0

However, for the comparison operators “x>y”, “x>=y”, “x<y” and “x<=y”, widening
takes place if necessary. Unusually, but conveniently, chaining of comparison operators
is allowed, e.g., “0<=x<1<y>z” is equivalent to
(0<=x) and (x<1) and (1<y) and (y>z)

Note that there is no comparison between x and y or z in this example.

3.3.4 Complex numbers


We have introduced three classes of numbers which form the simplest types of Python
object. These are the basis for classes of numbers that are more complicated. For exam-
ple rational numbers can be implemented in terms of pairs of integers. For our purposes,
a probably more useful class is that of complex numbers, which is implemented
√ in terms
of a pair of real numbers. Whereas mathematicians usually denote −1 by i, many en-
gineers prefer j, and Python has adopted the latter approach. Thus a Python complex
number can be defined explicitly by, e.g., c=1.5-0.4j. Note carefully the syntax: the
j (or equivalently J) follows the float with no intervening “∗”. Alternatively, a pair of
floats a and b can be widened to a complex number via c=complex(a,b). We can nar-
row a complex; e.g., with c as in the last sentence, c.real returns a and c.imag returns
3.4 Namespaces and Modules 27

b. Another useful object is c.conjugate(), which returns the complex conjugate of


c. The syntax of complex attributes will be explained in Section 3.10.
The five basic arithmetic operations work for Python complex numbers, and in mixed
mode widening is done automatically. The library of mathematical functions for com-
plex arguments is available. We need to import from cmath rather than math. However,
for obvious reasons, the comparison operations involving ordering, described above,
are not defined for complex numbers, although the equality and inequality operators are
available.
You have now seen enough of Python to use the interpreter as a sophisticated five-
function calculator, and you are urged to try out a few examples of your own.

3.4 Namespaces and Modules

While Python is running, it needs to keep a list of those identifiers which have been
assigned to objects. This list is called a namespace, and as a Python object it too has
an identifier. For example, while working in the interpreter, the namespace has the un-
memorable name __main__.
One of the strengths of Python is its ability to include files of objects, functions etc.,
written either by you or by someone else. To enable this inclusion, suppose you have
created a file containing objects, e.g., obj1, obj2 that you want to reuse. The file should
be saved as, e.g., foo.py, where the .py ending is mandatory. (Note that with most text
editors you need this ending for the editor to realize that it is dealing with Python code.)
This file is then called a module. The module’s identifier is foo, i.e., the filename minus
the ending.
This module can be imported into subsequent sessions via

import foo

(When the module is first imported, it is compiled into bytecode and written back to
storage as a file foo.pyc. On subsequent imports, the interpreter loads this precompiled
bytecode unless the modification date of foo.py is more recent, in which case a new
version of the file foo.pyc is generated automatically.)
One effect of this import is to make the namespace of the module available as foo.
Then the objects from foo are available with, e.g., identifiers foo.obj1 and foo.obj2.
If you are absolutely sure that obj1 and obj2 will not clash with identifiers in the
current namespace, you can import them via

from foo import obj1, obj2

and then refer to them as obj1 etc.


The “quick-and-dirty-fix” of Section 3.3.2 is equivalent to the line

from foo import *


28 A Short Python Tutorial

which imports everything from the module foo’s namespace. If an identifier obj1 al-
ready existed, its identifier will be overwritten by this import process, which usually
means that the object becomes inaccessible. For example, suppose we had an identifier
gamma referring to a float. Then

from math import *

overwrites this and gamma now refers to the (real) gamma-function. A subsequent

from cmath import *

overwrites gamma with the (complex) gamma-function! Note too that import statements
can appear anywhere in Python code, and so chaos is lurking if we use this option.
Except for quick, exploratory work in the interpreter, it is far better to modify the
import statements as, e.g.,

import math as re
import cmath as co

so that in the example above gamma, re.gamma and co.gamma are all available.
We now have sufficient background to explain the mysterious code line

if __name__ == "__main__"

which occurred in both of the snippets in Section 2.5. The first instance occurred in a
file fib.py. Now if we import this module into the interpreter, its name is fib and not
__main__ and so the lines after this code line will be ignored. However, when devel-
oping the functions in the module, it is normal to make the module available directly,
usually via the %run command. Then, as explained at the start of this section, the con-
tents are read into the __main__ namespace. Then the if condition of the code line is
satisfied and the subsequent lines will be executed. In practice, this is incredibly con-
venient. While developing a suite of objects, e.g., functions, we can keep the ancillary
test functions nearby. In production mode via import, these ancillary functions are ef-
fectively “commented out”.

3.5 Container Objects

The usefulness of computers is based in large part on their ability to carry out repeti-
tive tasks very quickly. Most programming languages therefore provide container ob-
jects, often called arrays, which can store large numbers of objects of the same type,
and retrieve them via an indexing mechanism. Mathematical vectors would correspond
to one-dimensional arrays, matrices to two-dimensional arrays etc. It may come as a
surprise to find that the Python core language has no array concept. Instead, it has con-
tainer objects which are much more general, lists, tuples, strings and dictionaries. It
will soon become clear that we can simulate an array object via a list, and this is how
3.5 Container Objects 29

numerical work in Python used to be done. Because of the generality of lists, such sim-
ulations took a great deal longer than equivalent constructions in Fortran or C, and this
gave Python a deservedly poor reputation for its slowness in numerical work. Devel-
opers produced various schemes to alleviate this, and they have now standardized on
the NumPy add-on module to be described in Chapter 4. Arrays in NumPy have much
of the versatility of Python lists, but are implemented behind the scenes as arrays in
C, significantly reducing, but not quite eliminating, the speed penalty. However, in this
section we describe the core container objects in sufficient detail for much scientific
work. They excel in the “administrative, bookkeeping chores” where Fortran and C are
at their weakest. Number-crunching numerical arrays are deferred to the next chapter,
but the reader particularly interested in numerics will need to understand the content of
this section, because the ideas developed here carry forward into the next chapter.

3.5.1 Lists
Consider typing the code snippet into the IPython terminal.

1 [1,4.0,’a’]
2 u=[1,4.0,’a’]
3 v=[3.14,2.78,u,42]
4 v
5 len(v)
6 len? # or help(len)
7 v*2
8 v+u
9 v.append(’foo’)
10 v

Line 1 is our first instance of a Python list, an ordered sequence of Python objects sepa-
rated by commas and surrounded by square brackets. It is itself a Python object, and can
be assigned to a Python identifier, as in line 2. Unlike arrays, there is no requirement that
the elements of a list be all of the same type. In lines 3 and 4, we see that in creating the
list an identifier is replaced by the object it refers to, e.g., one list can be an element in
another. The beginner should consult Figure 3.1 again. It is the object, not the identifier,
which matters. In line 5, we invoke an extremely useful Python function len() which
returns the length of the list, here 4. (Python functions will be discussed in Section 3.8.
In the meantime, we can find what len does by typing the line len? in IPython.) We
can replicate lists by constructions like line 7, and concatenate lists as in line 8. We can
append items to the ends of lists as in line 9. Here v.append() is another useful func-
tion, available only for lists. You should try v.append? or help(v.append) to see a
description of it. Incidentally, list. followed by tab completion or help(list) will
give a catalogue of functions intrinsic to lists. They are the analogue of c.conjugate()
in Section 3.3.4.
30 A Short Python Tutorial

3.5.2 List indexing


We can access elements of u by indexing, u[i] where i ∈ [0, len(u)) is an integer.
Note that indexing starts with u[0] and ends with u[len(u)-1]. So far, this is very
similar to what is available for arrays in, e.g., C or Fortran. However, a Python list such
as u “knows” its length, hence we could also index the elements, in reverse order, by
u[len(u)-k], where k ∈ (0, len(u)], which Python abbreviates to u[-k]. This turns out
to be very convenient. For example, not only is the first element of any list w referred to
by w[0], but the last element is w[-1]. The middle line of Figure 3.2 shows both sets
of indices for a list of length 8. Using the code snippet above, you might like to guess
the objects corresponding to v[1] and v[-3], and perhaps use the interpreter to check
your answers.
At first sight, this may appear to be a trivial enhancement, but it becomes very pow-
erful when coupled to the concepts of slicing and mutability, which we address next.
Therefore, it is important to make sure you understand clearly what negative indices
represent.

3.5.3 List slicing


Given a list u, we can form more lists by the operation of slicing. The simplest form of a
slice is u[start:end], which is a list of length end-start, as shown in Figure 3.2. If
the slice occurs on the right-hand side of an assignation, then a new list is created. For
example, su=u[2:6] generates a new list with four elements, where su[0] is initialized
with u[2]. If the slice occurs on the left, no new list is generated. Instead, it allows us
to change a block of values in an existing list. There are important new constructs here
which may well be unfamiliar to C and Fortran users.
Consider the simple example below which illustrates the possibilities, and, once you
have understood it, carry out further experiments of your own. This is best worked
through in IPython terminal mode.
1 u=[0,1,2,3,4,5,6,7]
2 su=u[2:4]
3 su
4 su[0]=17
5 su
6 u
7 u[2:4]=[3.14,’a’]
8 u

If start is zero, it may be omitted, e.g., u[ :-1] is a copy of u with the last element
omitted. The same applies at the other end, u[1: ] is a copy with the first element
omitted and u[:] is a copy of u. Here, we assume that the slice occurs on the right-hand
side of an assignation. The more general form of slicing is su = u[start:end:step].
Then su contains the elements u[start], u[start+step], u[start+2*step], . . . ,
as long as the index is less than start+end. Thus with the list u chosen as in the
3.5 Container Objects 31

        

     

        
       

         

Figure 3.2 Indices and slicing for a list u of length 8. The middle line shows the contents of u
and the two sets of indices by which the elements can be addressed. The top line shows the
contents of a slice of length 4 with conventional ordering. The bottom line shows another slice
with reversed ordering.

example above we would have u[2:-1:2]=[2,4,6]. A particularly useful choice is


step=-1, which allows traversal of the list in the reverse direction. See Figure 3.2 for
an example.

3.5.4 List mutability


For any container object u, it may be possible to modify an element or a slice, without
any apparent change having been applied to the object’s identifier. Such objects are said
to be mutable. As an example, consider a politician’s promises. In particular, lists are
mutable. There is a trap here for the unwary. Consider the code

1 a=4
2 b=a
3 b=’foo’
4 a
5 b
6 u=[0,1,4,9,16]
7 v=u
8 v[2]=’foo’
9 v
10 u

The first five lines should be comprehensible: a is assigned to the object 4; so is b. Then
32 A Short Python Tutorial

b is assigned to the object ’foo’, and this does not change a. In line 6, u is assigned to
a list object and so is v in line 7. Because lists are mutable, we may change the second
element of the list object in line 8. Line 9 shows the effect. But u is pointing to the same
object (see Figure 3.1) and it too shows the change in line 10. While the logic is clear,
this may not be what was intended, for u was never changed explicitly.
It is important to remember the assertion made above: a slice of a list is always a
new object, even if the dimensions of the slice and the original list agree. Therefore,
compare lines 6–10 of the code snippet above with
1 u=[0,1,4,9,16]
2 v=u[ : ]
3 v[2]=’foo’
4 v
5 u

Now line 2 makes a slice object, which is a copy4 of the object defined in line 1.
Changes to the v-list do not alter the u-list and vice versa.
Lists are very versatile objects, and there exist many Python functions which can
generate them. We shall discuss list generation at many points in the rest of this book.

3.5.5 Tuples
The next container to be discussed is the tuple. Syntactically it differs from a list only by
using () instead of [] as delimiters, and indexing and slicing work as for lists. However,
there is a fundamental difference. We cannot change the values of its elements: a tuple is
immutable. At first sight, the tuple would appear to be entirely redundant. Why not use a
list instead? The rigidity of a tuple however has an advantage. We can use a tuple where
a scalar quantity is expected, and in many cases we can drop the brackets () when there
is no ambiguity, and indeed this is the commonest way of utilizing tuples. Consider the
snippet below, where we have written a tuple assignation in two different ways.
(a,b,c,d)=(4,5.0,1.5+2j,’a’)
a,b,c,d = 4,5.0,1.5+2j,’a’

The second line shows how we can make multiple scalar assignments with a single
assignation operator. This becomes extremely useful in the common case where we need
to swap two objects, or equivalently two identifiers, say a and L1. The conventional way
to do this is
temp=a
a=L1
L1=temp

4 For the sake of completeness, we should note that this is a shallow copy. If u contains an element which is
mutable, e.g., another list w, the corresponding element of v still accesses the original w. To guard against
this, we need a deep copy to obtain a distinct but exact copy of both u and its current contents. For further
details inspect the copy module. In other words, try import copy followed by the line copy?
3.5 Container Objects 33

This would work in any language, assuming temp, a and L1 all refer to the same type.
However,

a,L1 = L1,a

does the same job in Python, is clearer, more concise and works for arbitrary types.
Another use, perhaps the most important one for tuples, is the ability to pass a variable
number of arguments to a function, as discussed in Section 3.8.4. Finally, we note a
feature of the notation which often confuses the beginner. We sometimes need a tuple
with only one element, say foo. The construction (foo) strips the parentheses and
leaves just the element. The correct tuple construction is (foo,).

3.5.6 Strings
Although we have already seen strings in passing, we note that Python regards them as
immutable container objects for alphanumeric characters. There is no comma separator
between items. The delimiters can be either single quotes or double quotes, but not a
mixture. The unused delimiter can occur within the string, e.g.

s1="It’s time to go"


s2=’ "Bravo!" he shouted.’

Indexing and slicing work in the same way as for lists.


There are two very useful conversion functions which can be associated with strings.
The function str() when applied to a Python object will produce a string representation
of that object. Among its many uses, the function eval acts as the inverse of str().
Consider the snippet

L = [1,2,3,5,8,13]
ls = str(L)
ls
eval(ls) == L

Strings will turn out to be very useful for the input of data, and, most importantly,
producing formatted output from the print function (see Sections 3.8.6 and 3.8.7).

3.5.7 Dictionaries
As we have seen, a list object is an ordered collection of objects, A dictionary object
is an unordered collection. Instead of accessing the elements by virtue of their position,
we have to assign a keyword, an immutable object, usually a string, which identifies
the element. Thus a dictionary is a collection of pairs of objects, where the first item in
the pair is a key to the second. A key–object pair is written as key:object. We fetch
items via keys rather than position. The dictionary delimiters are the braces { }. Here is
a simple example that illustrates the basics.
Other documents randomly have
different content
La vieja y la hija conocieron inmediatamente a Delgadina, pero se
desentendieron de ello y la agasajaron mucho. Cuando estuvieron solas dijo
la madre:—«No te decía yo que la matásemos!»—«Mamita, contestó la
hija, aunque se parece mucho a Delgadina, no puede ser ella ¿no le arrancó
usted misma los ojos? y ella los tenía negros y los de ésta son azules. Y
fíjese que el viejo es el padre de ella y no se parece en nada a su
compadre». Con esto se tranquilizaron.
Muchas veces más convidó el Rey a comer a Delgadina, y siempre tenía
ella gran cuidado de no lavarse las manos en la mesa; pero en una ocasión
que se las manchó con fruta hubo de lavárselas, y sucedió que sin querer las
sacudió. Inmediatamente comenzaron a caer de entre sus dedos a puñados
las onzas de oro, tan nuevecitas, tan amarillas como si estuvieran
recién acuñadas. Todos se quedaron con la boca abierta y no podían salir de
su asombro.
Entonces el Rey conoció que había sido engañado por la vieja y que la
verdadera Delgadina era la que hasta entonces había pasado por hija del
antiguo pescador. El Rey le pidió que le contase su historia y Delgadina
accedió gustosa.
La vieja y su hija protestaron de que todo era mentira, y entonces el Rey
hizo venir al viejo y a su familia, que corroboraron lo que a ellos les
constaba, y como si esto no fuese bastante apareció de súbito el Culebrón,
que refirió todo lo sucedido sin omitir detalles.
Cuando hubo concluido el Culebrón su relato, se convirtió en un
hermoso niño, y volviéndose a Delgadina le dijo:—«Yo soy el Angel de tu
guarda y he hecho esto contigo porque siempre fuiste buena hija y
compasiva con los pobres; yo estaré continuamente a tu lado y velaré por
ti».
Mientras hablaba el niño, vieron todos que le brotaban de sus espaldas
dos brillantes alas, que desplegó suavemente cuando terminó, y emprendió
el vuelo desapareciendo ante la vista atónita de los circunstantes.
El Rey hizo quemar a la vieja y a su hija, mandó buscar al padre de
Delgadina y se casó con ella; y en el momento mismo en que le ponían la
bendición, el hijo del viejo pescador recobró la vista.
Y así todos los buenos fueron felices y los malos castigados.
Y aquí se acabó el cuento y entró por la puerta del convento, nosotros
nos quedamos afuera y los frailes se quedaron adentro.
4. LA TENQUITA.

(Recitado en 1905 por Polonia Gonzalez, de 50 años, de la provincia de Colchagua)

Para saber y contar y contar para aprender.


Esta era una Tenquita que tenía unos tenquitos muy lindos, que acababan
de salir del huevo.
Una mañanita salió a buscarles que comer, y como era invierno y había
caído mucha nieve, a la Tenquita se le heló una patita.
Al verse coja la avecita se afligió mucho y llorando le dijo a la Nieve:
—Nieve, ¿por qué eres tan mala que me quemaste la patita a mí?[C].
Y la Nieve le contestó:
—Más malo es el Sol que me derrite a mí.
Entonces la Tenquita se fué donde el Sol, y le dijo:
—Sol, ¿por qué eres tan malo que derrites a la Nieve y la Nieve me
quema la patita a mí?
Y el Sol le respondió:
—Más malo es el Nublado que me tapa a mí.
Se fué la Tenquita a ver al Nublado, y le dijo:
—Nublado, ¿por qué eres tan malo que tapas al Sol, el Sol derrite a la
Nieve y la Nieve me quema la patita a mí?
—Más malo es el Viento que me corre a mí.
Fué la Tenquita donde el Viento, y le dijo:
—Viento, ¿por qué eres tan malo que corres al Nublado, el Nublado tapa
al Sol, el Sol derrite a la Nieve y la Nieve me quema la patita a mí?
—Más mala es la Pared que me ataja a mí.
Fué la Tenquita a ver a la Pared, y le dijo:
—Pared, ¿por qué eres tan mala que atajas al Viento, el Viento corre al
Nublado, el Nublado tapa al Sol, el Sol derrite a la Nieve y la Nieve me
quema la patita a mí?
—Más malo es el Ratón que me agujerea a mí.
Fué la Tenquita donde el Ratón y le dijo:
—Ratón, ¿por qué eres tan malo que agujereas a la Pared, la Pared ataja
al Viento, el Viento corre al Nublado, el Nublado tapa al Sol, el Sol derrite a
la Nieve y la Nieve me quema la patita a mí?
—Más malo es el Gato que me come a mí.
Fué la Tenquita donde el Gato y le dijo:
—Gato, ¿por qué eres tan malo que te comes al Ratón, el Ratón agujerea
a la Pared, la Pared ataja al Viento, el Viento corre al Nublado, el Nublado
tapa al Sol, el Sol derrite a la Nieve y la Nieve me quema la patita a mí?
—Más malo es el Perro que me corre a mí.
Entonces la Tenquita fué donde el Perro y le dijo:
—Perro, ¿por qué eres tan malo que corres al Gato, el Gato come al
Ratón, el Ratón agujerea a la Pared, la Pared ataja al Viento, el Viento corre
al Nublado, el Nublado tapa al Sol, el Sol derrite a la Nieve y la Nieve me
quema la patita a mí?
—Más malo es el Palo que me pega a mí.
Fué entonces la Tenquita donde el Palo, y le dijo:
—Palo, ¿por qué eres tan malo que pegas al Perro, el Perro corre al Gato,
el Gato come al Ratón, el Ratón agujerea a la Pared, la Pared ataja al
Viento, el Viento corre al Nublado, el Nublado tapa al Sol, el Sol derrite a la
Nieve y la Nieve me quema la patita a mí?
—Más malo es el Fuego que me quema a mí.
Fué la Tenquita donde el Fuego y le dijo:
—Fuego, ¿por qué eres tan malo que quemas al Palo, el Palo pega al
Perro, el Perro corre al Gato, el Gato corre al Ratón, el Ratón agujerea la
Pared, la Pared ataja al Viento, el Viento corre al Nublado, el Nublado tapa
al Sol, el Sol derrite a la Nieve, y la Nieve me quema la patita a mí?
—Más mala es el Agua que me apaga a mí.
Fué la Tenquita donde el Agua y le dijo:
—Agua, ¿por qué eres tan mala que apagas al fuego, el Fuego quema al
Palo, el Palo pega al Perro, el Perro corre al Gato, el Gato come al Ratón, el
Ratón agujerea a la Pared, la Pared ataja al Viento, el Viento corre al
Nublado, el Nublado tapa al Sol, el Sol derrite a la Nieve y la Nieve me
quema la patita a mí?
—Más malo es el Buey que me bebe a mí.
Fué la Tenquita donde el Buey y le dijo:
—Buey, ¿por qué eres tan malo que bebes el Agua, el Agua apaga al
Fuego, el Fuego quema al Palo, el Palo pega al Perro, el Perro corre al Gato,
el Gato come al Ratón, el Ratón agujerea a la Pared, la Pared ataja al Viento
el Viento corre al Nublado, el Nublado tapa al Sol, el Sol derrite a la Nieve
y la Nieve me quema la patita a mí?
—Más malo es el Cuchillo que me mata a mí.
Fué la Tenquita donde el Cuchillo, y le dijo:
—Cuchillo, ¿por qué eres tan malo que matas al Buey, el Buey se bebe
al Agua, el Agua apaga al Fuego, el Fuego quema al Palo, el Palo pega al
Perro, el Perro corre al Gato, el Gato come al Ratón, el Ratón agujerea a la
Pared, la Pared ataja al Viento, el Viento corre al Nublado, el Nublado tapa
al Sol, el Sol derrite a la Nieve y la Nieve me quema la patita a mí?
—Más malo es el Hombre que me hace a mí.
Fué la Tenquita donde el Hombre, y le dijo:
—Hombre, ¿por qué eres tan malo que haces al Cuchillo, el Cuchillo
mata al Buey, el Buey se bebe al Agua, el Agua apaga al Fuego, el Fuego
quema al Palo, el Palo pega al Perro, el Perro corre al Gato, el Gato come al
Ratón, el Ratón agujerea a la Pared, la Pared ataja al Viento, el Viento corre
al Nublado, el Nublado tapa al Sol, el Sol derrite a la Nieve y la Nieve me
quema la patita a mí?
—Pregúntaselo al Señor que me hizo a mí.
Fué entonces la Tenquita donde su Divina Majestad, y arrodillándose
humildemente delante de ella inclinó la cabeza hasta besar el suelo, y le
dijo:
—Señor, ¿por qué hiciste al Hombre, que es tan malo, el Hombre hace al
Cuchillo, el Cuchillo mata al Buey, el Buey se bebe al Agua, el Agua apaga
al Fuego, el Fuego quema al Palo, el Palo pega al Perro, el Perro corre al
Gato, el Gato come al Ratón, el Ratón agujerea a la Pared, la Pared ataja al
Viento, el Viento corre al Nublado, el Nublado tapa al Sol, el Sol derrite a la
Nieve y la Nieve me quema la patita a mí?
Y la Tenquita se puso a llorar tan amargamente que daba lástima verla.
El Señor se compadeció de la desgracia de la pobre avecita y le dijo con
mucha dulzura:
—Vete tranquila, Tenquita, a cuidar a tus tenquitos, que están tiritando
de frío y muriéndose de hambre.
La Tenquita, como buena cristiana, obedeció al momento y cuando llegó
a su nidito se encontró con que tenía buena y sana la patita quemada.
En el cuento que sigue, español, pero que no he
visto impreso, el desarrollo es casi el mismo que el
de la Tenquita. Lo publico como nota comparativa.
5. EL GALLITO (Cuento de pega)

(Dictado en 1911 por don Victoriano de Castro, español, de 55 años. Lo oyó contar en Belver de
los Montes, provincia de Zaragoza, donde el cuento era muy popular, cuando él era niño)

Había una vez en una aldea un Gallo, que recibió una invitación de otro
Gallo, primo suyo, para asistir a sus bodas. El Gallo se levantó muy
temprano, se acicaló y vistió convenientemente y emprendió el viaje,
olvidando tomar el desayuno.
En el camino encontró una boñiga de vaca, toda llena de granos de trigo
sin digerir; y aquí vinieron los apuros de mi buen Gallo, que empezó a decir
entre sí:
—¿Qué haré? picaré o no picaré? si pico, me mancho el pico, y si no, me
muero de hambre.
Así estuvo meditando por algún rato y mirando los granos de trigo, hasta
que cayó en la tentación y se dió un buen hartazgo.
Siguió su camino y a poco andar encontró una mata de Malva y le dijo:
—Malva, límpiame el pico, que voy a la boda de mi primo Juan
Periquito.
La Malva dijo:
—No quiero.
Más adelante encontró a una Oveja y le dijo:
—Oveja, come a Malva, que Malva no quiso limpiarme el pico, que voy
a la boda de mi primo Juan Periquito.
La Oveja dijo:
—No quiero.
Siguió andando y más adelante encontró a un Lobo y le dijo:
—Lobo, come a Oveja, que Oveja no quiso comer a Malva, que Malva
no quiso limpiarme el pico, que voy a la boda de mi primo Juan Periquito.
El Lobo dijo:
—No quiero.
Siguió el Gallo su camino y más adelante encontró a un Perro y le dijo:
—Perro, mata a Lobo, que Lobo no quiso comer a Oveja, que Oveja no
quiso comer a Malva, que Malva no quiso limpiarme el pico, que voy a la
boda de mi primo Juan Periquito.
El Perro dijo:
—No quiero.
A poco andar encontró el Gallo a un Palo y le dijo:
—Palo, apalea a Perro, que Perro no quiso matar a Lobo, que Lobo no
quiso comer a Oveja, que Oveja no qui so comer a Malva, que Malva
no quiso limpiarme el pico, que voy a la boda de mi primo Juan Periquito.
El Palo dijo:
—No quiero.
Anduvo el Gallo un rato más y se encontró con un Fuego y le dijo:
—Fuego, quema a Palo, que Palo no quiso pegar a Perro, que Perro no
quiso matar a Lobo, que Lobo no quiso comer a Oveja, que Oveja no quiso
comer a Malva, que Malva no quiso limpiarme el pico, que voy a la boda de
mi primo Juan Periquito.
El Fuego dijo:
—No quiero.
Más adelante encontró el Gallo al Agua y le dijo:
—Agua, apaga a Fuego, que Fuego no quiso quemar a Palo, que Palo no
quiso pegar a Perro, que Perro no quiso matar a Lobo, que Lobo no quiso
comer a Oveja, que Oveja no quiso comer a Malva, que Malva no quiso
limpiarme el pico, que voy a la boda de mi primo Juan Periquito.
El Agua dijo:
—No quiero.
Siguió andando el Gallo y más adelante encontró a un Burro, y le dijo:
—Burro, bébete a Agua, que Agua no quiso apagar a Fuego, que Fuego
no quiso quemar a Palo, que Palo no quiso pegar a Perro, que Perro no
quiso matar a Lobo, que Lobo no quiso comer a Oveja, que Oveja no quiso
comer a Malva, que Malva no quiso limpiarme el pico, que voy a la boda de
mi primo Juan Periquito.
(Aquí se suspende el cuento y se habla de cualquiera otra cosa. De
pronto se dice:—«¿Dónde llegaba? ¿al Palo? ¿al Fuego?»; y cuando
contesta alguno:—«Al Burro», se le dice:—«Alzale la cola y bésale el c...»)
6. LA TORTILLA O EL CANARITO ENCANTADO

(Referido por don Osvaldo Martínez, Presbítero, de Santiago, en 1912)

Este era un Rey que tenía una hija única, de una hermosura
extraordinaria, virtuosa, caritativa y hacendosa. El Rey la amaba
entrañablemente y, como se dice, tenía puestos los ojos en ella.
La Princesa acostumbraba subir todos los días a la terraza del palacio y
allí pasaba las horas cosiendo o bordando y recreándose con la vista de las
plantas, árboles y flores que adornaban el parque real, que desde allí se
dominaba.
Un día que estaba en su acostumbrado trabajo, un lindo Canarito se paró
en la rama de un árbol que casi llegaba hasta donde ella estaba sentada, y
entonó un canto tan melodioso que la princesa, a fin de oirle mejor, se
levantó para acercarse a la avecita, pero apenas se movió de su asiento, el
Canarito se fué.
La Princesa, pensando que el pajarito podía volver, hizo colocar una
jaula con trampa en el mismo árbol, para cazarlo.
Efectivamente, el Canarito volvió al día siguiente, pero en vez de
acercarse a la jaula, se posó en el bastidor de la Princesa y después de
gorjear unos cuantos trinos, tomó con el pico una madeja de seda y
emprendió el vuelo.
Al otro día estaba la Princesa, como siempre, ocupada en sus labores,
cuando de repente llega el Canarito, se para en el bastidor, canta dulcemente
un instante, y tomando con el pico el dedal de oro que la Princesa acababa
de dejar en el costurero, y abriendo las alas desapareció en el espacio.
La repetición de la aventura preocupó bastante a la Princesa, que no pasó
buena noche. Sin embargo, se levantó temprano y volvió a la terraza a
continuar su bor dado, pensando en el Canarito, de quien a toda costa
quería apoderarse.
En esto estaba cuando llega la linda avecita, cantando aún mejor que en
los días anteriores, y sin siquiera detenerse un momento, se apodera de las
tijeras de oro de la Princesa, y elevándose por los aires, se pierde de vista.
La Princesa cayó gravemente enferma. Por llamado del Rey, vinieron los
médicos más prestigiosos y los adivinos de más fama, tanto del país como
del extranjero, y ninguno pudo conocer la enfermedad.
Mientras tanto, la Princesa languidecía, su mal se agravaba, y se iba
consumiendo poco a poco. El Rey, desesperado, hizo publicar un bando en
que ofrecía grandes riquezas al que lograra sanar a su hija.
Muchos lo tentaron, pero ninguno lo consiguió, y la Princesa seguía
empeorando a ojos vistas.

En un pueblo algo alejado de la ciudad en que la Corte residía, vivía una


viejecita que tenía un hijo vivo y despierto, llamado Juan.
Un día lo llamó y le dijo:
—Mira, Juanito, toma estas tres tortillas que acabo de hacer al rescoldo y
se las llevas a la Princesa, que ellas le darán salud. Que no te vayas a comer
ninguna, ni se te pierdan, porque las tres han de llegar a poder de la
Princesa.
El muchacho tenía la costumbre de obedecer sin replicar. Subió en un
burro; a un lado de las alforjas colocó las tortillas y al otro un pedazo de
pan, harina y un poco de charqui y se puso en marcha.
La mitad del camino llevaría andado, cuando el burro se puso a
corcovear y por más que Juanito le pegaba fuerte y feo con una varilla, el
animal no avanzaba un paso.
Viendo la porfía de la bestia, Juanito sacó las tortillas de las alforjas y
descendió del burro para seguir a pie; pero en cuanto bajó, se le cayó una de
las tortillas y se le fué rodando por el camino.
Era de ver cómo Juanito corría detrás de la tortilla, que rodaba y rodaba,
sin poderla alcanzar; y el pícaro burro, que antes no quería moverse, cómo
seguía a Juanito, que casi le pisaba los talones.
Por fin la tortilla se metió adentro de una cueva y Juanito se coló detrás
de ella.
Cuando Juanito estuvo adentro, se encontró, sin saber cómo, en un gran
comedor regiamente amueblado. La mesa estaba cubierta de ricas viandas y
manjares de toda especie que exhalaban un perfume delicioso, y como al
muchacho, con la carrera, se le había abierto el apetito, tomó el cucharón
para servirse un plato de cazuela y ya iba a meterlo en la sopera, cuando el
cucharón se le enderezó en la mano y pegándole fuertemente en la cara le
dijo:
—¿Cómo te atreves a comer antes que tus amos?
En esto se sintió un gran ruído, y entró rodando al comedor una gran
bola de cobre. Juanito, lleno de miedo, apenas tuvo tiempo de esconderse
detrás de la puerta, y desde allí pudo ver que la bola se abría en dos partes,
como una concha, y de ella salía un lindo canario.
Con el mismo ruído y el mismo aparato entraron otras dos bolas más,
una tras otra, y de cada una salió otro canario.
Las tres avecitas sacudieron sus plumas un momento, como si se
desperezaran, y después, volando, se introdujeron a un elegante dormitorio
situado al lado del comedor, en el que había tres lujosas camas.
Juanito continuaba observando desde su escondite, con la curiosidad que
es de suponer, tan extraños acontecimientos. De pronto vió que tres negros
atravesaban el patio y el comedor y entraban al dormitorio conduciendo
sendos baños de plata, que colocaban al lado de las camas.
Inmediatamente los Canaritos se zambulleron en el agua y un rato
después salían de los baños transformados en hermosos Príncipes. Los
esclavos los perfumaron, los enjuagaron y ayudaron a vestirse, y en seguida
se reti raron, dejándolos recostados en sus camas, contándose lo que les
había pasado en los últimos quince días, tiempo que no se veían.
Dos de los Príncipes nada importante tuvieron que referir; pero, en
cambio, el tercero contó que en una de sus excursiones había divisado a una
Princesa tan hermosa como no había visto otra en su vida, que estaba
perdidamente enamorado de ella y que, no hallando cómo llamar su
atención, le había robado un día una madeja de seda con que bordaba, otro
día su dedal y al siguiente unas tijeras de oro, objetos que tenía al lado en su
velador. Y tomándolos, los besaba tiernamente, diciéndoles las palabras más
dulces y cariñosas.
Después de escuchar esto, Juanito logró escabullirse sin ser notado, y
como el hambre le apretaba, se metió en la cocina, en la cual no encontró a
nadie. Con temor probó de uno de los guisos, y viendo que nada le pasaba,
se creyó autorizado para hartar su estómago.
Después de satisfacer su apetito, salió, sin tropiezos, de aquel palacio
encantado, y al lado afuera de la entrada de la cueva, tropezó con su burro,
que lo esperaba. Montó en él, y a las pocas horas se encontró frente al
palacio del Rey.
Pidió permiso al jefe de la guardia para pasar a ver a la Princesa y
entregarle las tortillas, con las cuales—aseguraba él—sanaría la enferma. Al
principio no querían dejarlo entrar, pero en vista de su insistencia, lo
condujeron a presencia del Rey, y como la petición de Juanito estaba de
acuerdo con el bando que el mismo Rey había mandado publicar, ordenó
que se le llevase a las habitaciones de la Princesa.
La Princesa, cansada con las preguntas de tanto charlatán como había
ido a visitarla, en cuanto entró Juanito se dió vuelta para la pared; pero éste,
sin inmutarse, le habló en los siguientes términos, de un resuello:
—Manda a decir mi mamita que su mercé es su señorita, que tenga muy
buenos días y que cómo está y que aquí le manda estas tres tortillas,
pero no le traigo más que dos, porque la otra se me fué rodando cuando salí
de mi tierra, y yo, por seguirla, llegué hasta un palacio encantado, en donde
vi y oí cosas tan maravillosas como tal vez no habrá visto ni oído alma
viviente en este mundo. Figúrese usted, señorita que, escondido detrás de la
puerta del comedor del palacio, vi que llegaban tres grandes bolas de cobre,
que al rodar metían mucho ruido y que se abrían por la mitad y que de cada
una de ellas salía un canarito.
Al llegar a este punto, la Princesa se volvió para el lado de Juanito, e
incorporándose en la cama, le preguntó con ansiedad:
—¿Y qué hicieron esos pajaritos?
—Sacudieron sus alitas y en seguida se fueron volando a un dormitorio
situado al lado del comedor y en el cual había tres camas; y entonces
llegaron tres negros, trayendo cada uno un baño que depositó al lado de las
camas; en cada uno de ellos se metió un Canario y a los pocos instantes
salieron convertidos en tres hermosos Príncipes, que se recostaron en sus
camas y empezaron a contarse lo que les había ocurrido en los últimos días.
Dos de ellos no tuvieron nada nuevo que contar, pero el otro, que era el más
lindo de los tres, les dijo que un día que pasaba volando por el palacio de un
Rey, divisó a la Princesa más hermosa que en su vida había visto, que se
había enamorado perdidamente de ella y que, para llamar su atención, le
había robado un día una madeja de seda, otra vez el dedal de oro y otro día
sus tijeras. No oí más, porque ya no aguantaba el hambre y me fuí a la
cocina a comer algo. Después que maté el hambre salí, y al lado afuera
encontré a mi burro, monté en él y me vine a cumplir el encargo de mi
mamita. Pero su mercé me perdonará que no le haya traído más que dos de
las tres tortillas que mi mamita me entregó para su mercé, porque como
habrá visto, no es mía la culpa de que se me haya perdido una.
La Princesa, que había escuchado anhelante a Juanito, contestó:
—Está muy bien, Juanito ¿y serías capaz de llevarme a la cueva en que
está el palacio encantado?
—Como nó pues, señorita, si el camino es bien refácil; no está más que a
la vueltecita de la esquina.
La Princesa hizo llamar al Rey.
—Padre, le dijo, todos los que hasta ahora han venido a verme no han
sido sino unos charlatanes, con excepción de este niño, que es médico
verdadero. El me ha traído la salud, pero aunque me siento bien, para
restablecerme por completo necesito hacer un viaje de unos cuantos días, y
espero que Vuestra Majestad no me negará el permiso. El solo me
acompañará.
El Rey se quedó admirado de ver el cambio tan radical que en un
momento se había operado en la salud de su hija, y como la amaba tanto y
nada se atrevía a negarle, le concedió el permiso que solicitaba. Quiso que
llevara dinero, mucho dinero, para los gastos que pudieran ofrecérsele; pero
ella lo rehusó, lo mismo que el séquito que se le ofrecía, y salió sin más
compañía que Juanito, montados ambos en el burro que había traído al niño
a palacio.
El burro los condujo en pocas horas hasta la entrada de la cueva, en
donde bajaron. La Princesa le dió a Juanito una carta para el Rey, en la que
le decía que no pasase cuidados por ella, que estaba bien, que en pocos días
más regresaría completamente restablecida, y que le entregara a Juanito el
dinero que había ofrecido al que la sanase de su enfermedad.
Deshizo Juanito el camino y puso en manos del Rey la carta de la
Princesa. El Rey ordenó que se le diese una gran suma de dinero y con ella
regresó Juanito a casa de su madre, y ambos, desde entonces, llevan una
vida tranquila y holgada.

Volvamos a la Princesa que, una vez que quedó sola, entró al interior de
la cueva y se encontró de repente en medio de un gran comedor regiamente
amueblado. No sabía qué hacerse, cuando entró el Canarito
revoloteando y cantando alegremente y después de hacerle mil gracias
a su adorada, se detuvo y le habló de esta suerte:
—Hermosa Princesa, ¿cómo te has atrevido a poner tus plantas en este
sitio en que te esperan tantos peligros?
—Linda avecita, por verte y tenerte a mi lado encontraré livianos todos
los trabajos que se me presenten; no aspiro sino a estar en tu compañía y oir
tu hermoso canto.
—Princesa, esta cueva encantada está al cuidado de una vieja hechicera;
búscala y la encontrarás en la última pieza del interior y dile que deseas
ocuparte y vienes a ofrecerle tus servicios; ella los aceptará y te encargará
trabajos que te parecerán imposibles de ejecutar, pero no tengas cuidado
que yo velaré siempre por tí y te ayudaré.
La Princesa, después de recorrer muchos patios y galerías, llegó a una
pieza a cuya puerta estaba sentada una vieja de aspecto repelente, con la
cabellera desgreñada, el rostro sucio, las uñas larguísimas, los ojos
encarnizados. En cuanto divisó a la Princesa, con voz áspera le preguntó:
—¿Qué buscas aquí, vil gusanillo de la tierra?
—Señora, le contestó, necesito emplearme y andaba buscando dónde
servir, cuando llegué a esta casa y como encontré la puerta franca y nadie
acudió a mi llamado, entré hasta este sitio sin encontrar en mi camino a
ninguna persona; ¿no querría Ud. tomarme a su servicio?
—Está bien, dijo la vieja; retírate a aquella pieza y mañana, de alba,
vienes a recibir mis órdenes.
La Princesa se retiró sumamente afligida; el rostro mal agestado de la
Bruja y su voz dura y antipática la atemorizaron y pasó la noche sin dormir.
Apenas amaneció se fué a la pieza de la vieja, que ya estaba en pie y que
la esperaba con un gran frasco de vidrio.
—Toma este frasco, le dijo, y antes de las doce del día me lo traerás
lleno de lágrimas de picaflores; si no consigues llenarlo, te costará la vida.

La Princesa salió llorando sin saber a dónde dirigirse, pero a poco andar
vió en un árbol al Canarito, que le dijo:
—Ve a aquel monte que se divisa allí cerca; antes de subir cortarás una
varillita de la primera planta que encuentres a mano derecha del camino que
conduce a la cima, subes y esperas arriba la salida del sol; colocas el frasco
en el suelo e inmediatamente vendrá una multitud de picaflores y uno tras
otro irá parándose en la boca del frasco.
Entonces tú les vas dando un golpecito en la cabeza con la varilla y
derramará cada uno tres lágrimas dentro del frasco. Serán tantos y se
turnarán tan rápidamente que en menos de una hora lo llenarán.
Siguió la princesa el camino que le indicó el Canario y al llegar al monte
cortó una varilla del primer arbusto que halló a la derecha de la senda; en
seguida continuó su marcha, y una vez que estuvo arriba, dejó el frasco en
el suelo, se sentó sobre una piedra y se quedó meditando sobre su triste
suerte y las raras aventuras de su corta vida, hasta que el sol se levantó
brillante y majestuoso en el horizonte.
Inmediatamente acudió de todas partes una multitud de picaflores, cuyas
plumas tornasoladas lanzaban vívidos reflejos al ser heridas por los rayos
solares. Las lindas avecitas revoloteaban en torno de la Princesa, y saliendo
del grupo, de a dos y de a tres se paraban en el borde de la boca del frasco y
esperaban que la joven les diese un suave golpecito en la cabeza con la
varilla, para retirarse y dejar el puesto a otras de sus compañeras. Esta
escena se repitió con tal rapidez que, aunque sólo eran tres las lágrimas que
cada picaflor depositaba en el frasco, en media hora éste se había llenado.
Sin embargo de haber cumplido su tarea, la Princesa no se movió de aquel
sitio: el solo recuerdo de la Bruja le imponía pavor y la hacía extremecerse,
¡y se sentía tan bien en medio de los árboles y de los pajaritos!
Cuando el sol llegó a lo más alto del cielo, la Princesa se despidió
cariñosamente de los picaflores, agradeciéndoles con frases llenas de
dulzura el servicio que le habían hecho; y rodeada de ellos, que no la
dejaron sino cuando llegó al plano, descendió del cerro con el frasco en sus
brazos.
Pocos momentos después llegaba a la cueva y se encontraba en presencia
de la aborrecible vieja, y entregándole el frasco le decía:
—Señora, estáis servida.
—Está bien, refunfuñó la Bruja; mañana temprano vendrás a recibir una
nueva orden.
Y arrojándole un mendrugo de pan, le indicó con el dedo que se retirara
a su cuarto.
La Princesa pasó la noche sin dormir, así es que muy temprano, antes
que amaneciese, ya estaba en presencia de la hechicera. La vieja, que la
esperaba, le pasó un cofre de una hermosura imponderable, cubierto de
incrustaciones de oro y de adornos de flores de diamantes, perlas y rubíes, y
entregándole una llavecita, le ordenó que la llevase a casa de otra vieja, su
amiga, porque era su cumpleaños. Esta amiga la abriría y sacaría su
contenido y después debía regresar la Princesa con la caja y estar de vuelta
antes del mediodía.
Salió la Princesa llorando y sin saber cómo, se halló de pronto al pie del
monte en que había estado la mañana anterior. Allí encontró al Canarito,
que le dijo:
—Enjuga tu llanto, hermosa Princesa, y quédate aquí hasta la hora
conveniente. Lo que la vieja desea es que abras el cofre; pero no lo abrirás,
ni tampoco lo llevarás a casa de la amiga de la Bruja, porque ella te lo haría
abrir. Poco antes de las doce te irás a la cueva y entregarás el cofre a la vieja
diciéndole que su amiga lo había abierto y habían salido de adentro unos
guerreros que la habían muerto. Y el Canarito se fué.
Mientras llegaba la hora, la Princesa se entretuvo con los picaflores que
revoloteaban a su alrededor de la manera más graciosa, haciendo mil
figuras y evoluciones como si bailaran; pero cuando el sol iba a llegar
al mediodía, bajó siempre rodeada de las avecitas, hasta que llegó a la
cueva. La vieja la esperaba en el interior, en la puerta de su habitación, y le
entregó el cofre diciéndole que apenas la amiga lo había abierto, habían
salido de él una cantidad innumerable de guerreros armados que en un
momento le dieron la muerte, desapareciendo en seguida.
—Pero ¿es cierto lo que me dices, muchacha? contestó la vieja, ¡si no
puede ser!
—Pero así ha sido, señora.
—A ver, pásame la llave.
Y tomándola, abre el cofre y sale de él un verdadero ejército de jóvenes
armados de espadas, lanzas y hachas con las cuales traspasan y destrozan a
la infame vieja, que se revuelca en el suelo en medio de un mar de sangre.
Los jóvenes guerreros desaparecen dejándola por muerta; pero la Bruja
tenía la vida de los gatos, y, arrastrándose como pudo, se echó a la cama.
La Princesa quedó anonadada con esta escena, y se habría quedado quién
sabe hasta cuándo como enclavada en el suelo, si la voz de la vieja no la
hubiese sacado de su abstracción.
—Hijita, le dijo la vieja con un tono que trataba de aparecer cariñoso,
vaya a la otra pieza, tome el primero de los frascos que hay en el armario y
me lo trae; quiero tomar del licor que hay en él para morir y dejar de sufrir.
Pasó la Princesa a la pieza contigua, y ahí encontró al Canarito, que le
dijo muy quedo al oído:
—No le lleves el primero sino el último de los frascos del armario, para
que muera de veras: cualquier otro que le lleves le dará la vida y no
terminarán nunca nuestros sufrimientos.
Obedeció la Princesa y le llevó el último frasco.
—¿Este es el primero, hijita?
—Sí, señora, éste es el primero.
—No vaya a haberse equivocado y haya tomado el segundo.
—No, señora, estoy completamente segura de que he traído el primero.
—Entonces deme una cucharada de él.
La Princesa le pasó una cucharada del líquido que el frasco contenía y la
vieja se lo bebió con ansia; pero apenas lo tragó, comenzó la Bruja a
torcerse, a despedazarse con las uñas, a morderse las manos y los brazos,
dando unos gritos tan desaforados que parecía que el palacio se iba a venir
al suelo.
Por suerte, todo esto duró poco, porque la vieja, en medio de los
mayores dolores, entregó pronto su alma al diablo, a quien con tanto
empeño había servido durante su larga vida.
En cuanto cesaron los alaridos de la Bruja, sucedió una cosa inesperada.
La cueva y el palacio se convirtieron en un bello y extenso país; los
Canarios, en tres hermosos príncipes; los negros que había visto Juanito, en
grandes de la corte, y los picaflores, en los habitantes del reino, todos los
cuales vinieron a rendir homenaje a la Princesa.
Acercóse a ella el más hermoso de los tres Príncipes e hincando una
rodilla en tierra, habló a la Princesa de esta manera:
—Princesa, yo soy aquel Canario que os arrebató la madeja de seda, el
dedal y las tijeras y que más tarde os aconsejó lo que debíais hacer para
libraros y librarnos de la malvada hechicera que por satisfacer una ruin
venganza mató a nuestros padres y nos tenía hechizados a mí, a mis
hermanos y a nuestro pueblo. Bien sabéis que yo os amo y que no podré
vivir sino en vuestra compañía. Sé que vos me amáis también, pues por
amor a mí habéis arrastrando tantos peligros. ¿Queréis que vayamos ahora
mismo donde vuestro padre, que es nuestro vecino, para pedir vuestra
mano?
—Príncipe, contestó la joven, mi anhelo es ser vuestra esposa; partamos
cuanto antes.
El pueblo, entusiasmado, aclamó a la Princesa, llamán dola su reina,
su buena y querida reina, y jurando amarla y protegerla de todo peligro.
Grande fué el alborozo del Rey, padre de la Princesa, al verla llegar
completamente sana de su enfermedad y en tan buena compañía. Las bodas
se celebraron al día siguiente y hubo grandes fiestas y regocijos públicos en
los dos reinos, cuyos pueblos confraternizaban como si fueran uno. Los
novios fueron muy felices; gobernaron a su pueblo con bondad paternal y
Dios los premió dándoles hijos bellos y virtuosos, que les hicieron
agradable su peregrinación en esta vida.
7. EL REY TIENE CACHITO

(Contado por el Presbítero don Osvaldo Martínez, de Santiago, en 1912)

Este era un Rey que cayó enfermo de un fuerte dolor a la cabeza. Su


dolencia lo obligó durante muchos días a guardar cama y durante ellos no
pudo ocuparse de los asuntos de gobierno. Cuando se levantó, se encontró
con que le había salido un cachito.
El Rey, por supuesto, quiso tener oculta de todos esta desgracia; pero no
lo consiguió: el pelo le creció tanto que tuvo necesidad de hacer llamar a un
peluquero, encargando que le trajeran el más discreto de la ciudad.
Sus Ministros pasaron revista a todos los fígaros de la capital y por fin
creyeron encontrar al que su Majestad necesitaba: era éste un pobre hombre
que, aunque manejaba magistralmente la tijera y la navaja, casi no tenía
clientela porque era muy reservado y poco comunicativo; no hablaba sino
cuando era de absoluta necesidad.
Con los informes de los Ministros, el Rey lo nombró su peluquero.
En la primera sesión, el Rey le dijo que a ninguna persona debía
comunicarle su desgracia y le exigió bajo juramento que así lo hiciese. El
Peluquero juró que a ninguna persona diría que el Rey tenía un cachito.
Después de esto le cortó el pelo y se retiró para volver dentro de un mes.
No hizo mas que salir el Peluquero y sentir un desasosiego como nunca
lo había tenido; y lo peor es que este malestar no lo dejaba y experimentaba
como una necesidad de echar afuera aquel secreto que le hormigueaba por
todo el cuerpo. Y aquí tenemos a nuestro hombre, que hasta entonces había
vivido tranquilo, convertido en el ser más desgraciado de la tierra: no
comía, no dormía, no trabajaba, no tenía ánimos para nada.
Y sin embargo de no comer, se iba hinchando, hinchando hasta ponerse
redondo como una tinaja.
El pobre hombre se sentía desfallecer, no hallaba qué hacerse; estaba
seguro de que se moriría en horas más si no contaba su secreto. Pero ¿y el
juramento? El era buen cristiano y por nada de la vida perdería su alma.
Desesperado, salió al campo; y aquí le ocurrió una idea salvadora. Con
una estaca que halló a mano abrió un hoyo y echándose de barriga en tierra
se puso a decirle:—¡El Rey tiene cachito! el Rey tiene cachito!—repitiendo
la frase no menos de cien veces; y a medida que la iba diciendo, la barriga
se le iba deshinchando. En seguida tapó el hoyo con la misma tierra que de
él había sacado.
¡Qué desahogado, qué aliviado y qué flaco se levantó el Barbero! ¡Qué
feliz se sintió! Pocos momentos después llegó a su casa pidiendo
desaforadamente que le dieran de comer; ¡qué apetito! todo lo que le
servían se le hacía poco! La mujer estaba desesperada: ¿de dónde sacaría
alimentos suficientes para llenar aquel tonel sin fondo? Se comió todo lo
que pilló a mano, cuanta materia engullible había en la casa, y por fin, más
cansado de hacer funcionar las mandíbulas que satisfecho, se acostó.
¡Era de ver la placidez con que dormía el santo varón! Durmió dos días con
sus noches, y se levantó feliz, cantando y con grandes disposiciones para
trabajar. Era otro hombre.
Pasaron los días uno tras otro hasta completar una semana, cuando
ocurrió una cosa inesperada. Los niños de la escuela habían ido a hacer la
chancha al campo vecino y encontraron una mata de capachitos, que había
brotado precisamente en el lugar en que el Peluquero había hecho el hoyo;
arrancaban las florecitas y tomándolas con el dedo pulgar, índice y cordial,
las reventaban en sus frentes, como tienen costumbre de hacerlo; pero en
esta vez la florecitas, al estallar, decían:
—¡El Rey tiene cachito!
Admirados los niños de este prodigio, llevaron a sus casas todos los
capachitos que quedaban y repitieron la prueba y los capachitos siempre
decían:—¡El Rey tiene cachito!
No se podía dudar de la noticia, y ella corrió como el aceite: en pocos
instantes la conocía toda la ciudad. Y tanto y tanto cundió que llegó a oídos
del Rey.
El Rey hizo llamar al Peluquero y después de apostrofarlo duramente le
dijo que le haría pagar con la vida su indiscreción. El Peluquero
respetuosamente repuso:—Señor, yo juré a Vuestra Majestad no decirle a
ninguna persona su secreto y lo he cumplido, porque hasta ahora no se lo he
dicho a alma nacida. ¿Qué culpa tengo yo si los capachitos lo andan
proclamando a los cuatro vientos?
Por cierto que se cuidó de contarle lo que había hecho, y como de esto
no había testigos, el Rey hubo de perdonarlo.
8. EL CUERPO SIN ALMA.

(Referido en 1912 por Beatriz Montecinos, de 50 años, de Talca)

Para saber y contar y contar para saber.


Este era un caballero que tenía un fundo cerca de la ciudad, muy grande
y muy hermoso, pero que tenía la maldición de que nadie podía vivir en él,
porque, sin saber cómo ni por qué, al otro día amanecían muertos los que
pretendían trabajarlo. El caballero estaba desesperado, y ofreció darlo a
medias al que se atreviese a sembrarlo.
Había en la misma ciudad una viuda muy pobre, que tenía tres hijos,
decididos y valientes, los cuales se pusieron de acuerdo para trasladarse al
fundo. Partieron, llevando cada uno un pedazo de pan y otro de queso, que
para más no les alcanzó el poco dinero que tenían.
Habían andado ya un buen trecho, cuando el menor se hizo a un lado de
sus hermanos, que siguieron andando, porque se le ofreció una necesidad.
Iba ya a reunirse con ellos, cuando se le presentó una pobre vieja pidiéndole
una limosna. El, compadecido, le dió el pan y el queso que llevaba, y
entonces la anciana le entregó una varillita, diciéndole que era de virtud y
que le haría todo lo que le pidiese, y desapareció.
Llegaron los tres hermanos al fundo muy de madrugada y convinieron en
que mientras iban a trabajar los dos menores, el mayor se quedaría haciendo
la comida para los tres.
Fueron los menores al trabajo y cuando el mayor tenía hecha la comida y
en punto para servirla, salió de un pozo que había cerca de la cocina un
enorme Culebrón, y el joven, del susto, se fué de espaldas y casi se mató del
golpe.
—La vida o la comida, le dijo el Culebrón.
—La comida, le contestó el pobre, más muerto que vivo.
El Culebrón devoró la comida y en seguida desapareció por el pozo.
Poco después llegaron los otros dos hermanos, quienes, de tanto que
habían trabajado, venían que no podían más de hambre. Cuando supieron lo
que había pasado, casi se murieron de rabia.
Al día siguiente se quedó el segundo haciendo la comida, partieron a
trabajar los otros dos, y sucedió lo mismo que el día anterior: salió el
Culebrón, se comió la comida, y dejó tocando tabletas a los tres hermanos.
El tercer día se quedó el menor, y en el momento en que éste retiraba la
olla del fuego, salió el Culebrón y le dijo:
—La vida o la comida.
—Ni la vida ni la comida, le respondió el joven, y poniéndose en facha
con su varillita en la mano, obligó al Culebrón a retirarse a su pozo bastante
mal herido.
Llegaron los otros dos, y comieron todos con mucho apetito.
Después dijo el más joven:
—Para vernos libres en adelante de este estorbo, amárrenme con un
cordel y descuélguenme en el pozo y yo mataré al Culebrón donde se
encuentre. Cuando mueva la cuerda es para que la tiren y me suban.
Bajó el joven, y en el fondo del pozo se encontró con un hermosísimo
palacio, que tenía todas las puertas y ventanas cerradas. Golpeó inútilmente,
porque no le abrieron. Entonces, sacando su varillita, dijo:
—Dios y una hormiguita, e inmediatamente se convirtió en hormiga. Así
pudo entrar por una rendija y llegó a una sala en donde había una niña más
bella que el sol. Se le subió por un costado y de repente la picó.
—¿Quién me pica? dijo la niña.
—Yo, señorita, contestó el joven desencantándose.
Se pusieron a conversar. La niña le dijo que eran tres hermanas, hijas del
Culebrón, el cual las tenía encerradas bajo siete llaves y no les permitía ver
a nadie.
—Yo mataré al Culebrón y las libraré a ustedes.
—No podrás matarlo—le dijo la joven—porque mi padre es el Cuerpo
sin Alma.
—Pero tú podrás averiguar en dónde tiene el alma y entonces yo daré
buena cuenta de él.
Fué la niña al lugar en que estaba su padre, y con ella el joven,
convertido en hormiga, pegado a su costado.
—Papá, ¿por qué lo llaman a usted el Cuerpo sin Alma?
—No te lo diré, porque las paredes tienen oídos y los matorrales ojos.
—Pero si aquí estamos solos, y encerradas como vivimos ¿a quién
podría confiarle lo que usted me diga?
Entonces él repuso:
—Hija, has de saber que en el monte vecino hay una laguna; dentro de la
laguna hay un toro; matando a ese toro, sale de su cuerpo un león; matando
a ese león, sale una zorra muy corredora, que nadie la podrá alcanzar;
adentro de la zorra hay una paloma; y adentro de la paloma, un huevo. Ese
huevo es mi alma, y si llegan a quebrarlo, soy muerto.
Siguieron hablando un rato sobre otras cosas y poco después la niña se
retiró a su pieza. Inmediatamente el joven se fué corriendo para la laguna, y
apenas había llegado a la orilla, salió el toro bramando y escarbando la
tierra que daba miedo.
—Dios y un toro de los más bravos—dijo el joven sacando la varillita y
al punto se convirtió en toro y se puso a pelear con el que había salido de la
laguna, hasta que lo mató. Por el hocico del toro muerto salió un león, que
echaba el cielo abajo con sus rugidos.
—Dios y un león de los más bravos—dijo el joven a la varillita, y
convirtiéndose en león, atacó rudamente a su contrario y lo mató. Entonces
salió la zorra corredora del hocico del león muerto, y tanto y tan bien corría
que no se le veían las patas.
—Dios y un perro zorrero, de los más corredores y más bravos, dijo el
joven, y en el mismo instante se volvió perro, y tan ligero corría, que
las patas no tocaban el suelo. En un momento alcanzó a la zorra y también
la despachó.
Mientras tanto el Cuerpo sin Alma se sentía muy enfermo y daba unos
quejidos terribles. La niña se acercó a preguntarle qué tenía.
—Retírate, traidora—le dijo el Culebrón—si no quieres que te mate.
Del cuerpo de la zorra salió una paloma, que se perdió en el espacio. El
joven dijo:
—Dios y un halcón de los más voladores;—y convertido en halcón dió
alcance a la paloma, la mató y le sacó del buche el huevo que tenía
guardado y que era el alma del Culebrón.
Poco después se presentó en el palacio y mostrándole el huevo, dijo al
Culebrón, que apenas respiraba ya, tan desfallecido estaba:
—¿Conoces esto?
—¿Cómo no lo he de conocer, si es mi alma?
—Te la entregaré si me das el manojo de llaves del palacio.
El Cuerpo sin Alma le entregó las llaves y el joven, disparándole el
huevo, le dijo:
—Ahí la tienes.
Pero el huevo le dió en la frente al Culebrón y se reventó, y el Culebrón
cayó muerto.
El joven se fué a librar a las tres niñas, pero la menor, que era la que él
había visto, no quería que sacase a las otras, porque estaba enamorada de él
y temía que sus hermanas, que también eran muy bellas, le robasen su amor.
Pero él le dijo:
—Si nosotros también somos tres; mis hermanos se casarán con tus
hermanas.
Las sacó a las otras dos de su encierro y amarrando primeramente a la
menor, movió el cordel y los que estaban arriba la subieron. Los dos
hermanos, cuando la vieron tan buena moza, se pusieron a pelear, para ver
cuál se la llevaba; pero ella les dijo que eran tres y que luego subirían
las otras dos.
Cuando hubieron subido las tres niñas, los hermanos mayores no
volvieron a echar el cordel, y tomando cada uno a su compañera, dejaron
abandonada a la menor, que esperó en vano que subiera el joven que había
quedado en el pozo.
Un momento después conoció éste su desgracia, y, turbado con la pena
que le causaba la traición de sus hermanos, por decirle a la varillita “siete
estados para arriba”, le dijo “siete estados para abajo” y llegó a la tierra de
los pigmeos, donde, del golpe tan violento que recibió, quedó sin sentidos.
Cuando volvió en sí, los pigmeos le habían robado su varillita de virtud.
El pobre entró a sufrir mucho y llegó su miseria a tal estado que se vió
obligado a ocuparse como cuidador de los rebaños del Rey de los pigmeos
para ganarse la vida.
Un día que lloraba su desgracia, se le apareció una Aguilita y le
preguntó:
—¿Por qué está tan triste y llorando?
—¿Cómo no he de llorar, distante de la que amo y viéndome en el estado
en que me hallo y sin esperanzas de volver a la tierra?
—Yo lo sacaré de aquí si le parece; pero tiene que llevar mucha carne,
porque el viaje es largo y hay que atravesar el mar.
—Está bien, llevaremos un cordero.
Y el joven mató un cordero y dividiéndolo en cuartos lo puso sobre el
Aguila y él se montó en seguida encima.
Al poco rato el Aguilita pidió de comer y él le puso en el pico un cuarto
de cordero. Volaron un rato, y el Aguilita pidió más, y él le entregó el
segundo cuarto; después, el tercero; y por fin el único que quedaba.
Iban volando por sobre el mar cuando el Aguilita dijo:
—Compañero, ¿queda carnecita? mire que me faltan las fuerzas y nos
caeremos al mar y nos ahogaremos si no como.
El joven se cortó una pierna y se la atravesó en el pico al Aguila. Esta
escena se repitió dos veces más, y el joven tuvo que cortar su otra pierna y
el brazo izquierdo, que el Aguila devoró en un instante. De pronto dijo el
Aguila:
—Ya llegamos; bájese, compañerito, que en aquel palacio está su niña; y
apúrese porque la van a casar con un príncipe y ella no quiere, porque lo
está esperando a usted.
—¿Y cómo me bajo—respondió el joven—si no tengo piernas?
—Echese al suelo no más, y no se demore, que lo dejan sin novia.
Al dejarse caer, el joven se encontró con sus dos piernas y sus dos
brazos, y si buen mozo había sido antes, quedó desde entonces mucho
mejor. Llorando de alegría, le dió las gracias a la Aguilita, y ella,
convirtiéndose en ángel, le dijo que era el de su guarda, que viéndolo tan
triste, había venido a sacarlo de apuros.
Cuando llegó al palacio en que estaba su amada, la alegría de ésta fué
grande, y en lugar de celebrarse el matrimonio con el príncipe con quien la
obligaban a casarse, se casó con el joven que tanto había sufrido por ella y
había sido su primer amor. La fiesta estuvo muy buena y hasta ahora estará
que se arde; yo me encontré en ella y comí y tomé hasta que casi reventé. Y
aquí se acabó el cuento y se lo llevó el viento para serranías de más adentro.
9. LA HUACHITA CORDERA.

(Referido en Abril de 1914 por Mercedes Albornoz, de 14 años, Villa Alegre)

Este era un hombre que vivía en el campo y había quedado viudo con
dos hijos pequeños: un niñito y una niñita. El hombre era pobre y para
alimentar a sus hijos tenía que salir a trabajar todos los días antes que
apareciera el sol, y como los niños no eran capaces de hacer nada, se los
dejaba encomendados a una vecina que los trataba con mucho cariño, les
lavaba su ropita y les daba muy bien de comer.
Mejoró un poco la situación del hombre y se casó con la vecina; pero
ésta, apenas salía su marido de la casa, obligaba a los niños a hacer el
fuego, a que le trajesen agua del río en baldes que eran muy pesados para
ellos, a barrer y ejecutar otros trabajos superiores a sus escasas y débiles
fuerzas; y si la leña no estaba bien encendida, o los baldes no llegaban
completamente llenos, o quedaba un poco de basura en el suelo, les pegaba
cruelmente con lo primero que hallaba a mano.
Una vez, el niño le dijo a la niña:—Vámonos de aquí, hermanita; ¿para
qué estamos sufriendo tanto?,—y al otro día muy temprano dejaron su
lecho, abandonaron la casa en que habían nacido y marcharon a la ventura,
alimentándose de frutas y de yerbas y durmiendo en las cuevas de las
montañas o en los ranchos abandonados que encontraban en su camino.
Después de muchos días de marcha, llegaron a una tierra desierta, sin
casas ni árboles, en la que el calor del sol se hacía sentir con toda su fuerza.
Los niños morían de sed y en ninguna parte hallaban agua para aplacarla.
Por fin llegaron a la orilla de una laguna y cuando se disponían a beber,
oyeron una voz que decía:
—El que de esta agua bebiere tiburón se ha de volver y devorará a su
hermano.
—Hermanita, no tomemos de esta agua—dijo el niño—aguantemos la
sed y vámonos, puede ser que más allá encontremos agua buena.
Muy tristes se apartaron de la laguna y a cada instante más sedientos;
pero luego tropezaron con un pozo y el corazón se les alegró. Sirviéndose
de una cuerda que estaba en el suelo al lado del brocal, echaron
adentro un tiesto que cerca estaba, y cuando ya lo alzaban repleto de
agua, salió del pozo una voz que decía:
—El que de esta agua bebiere, sierpe se ha de volver y devorará a su
hermano.
—Hermanita, no tomemos de esta agua—dijo el niño—aguantemos la
sed y vámonos, pueda ser que más allá encontremos otra mejor.
La niña no soportaba la sed, y si no hubiera sido por la amenaza de que
si bebía de esa agua devoraría a su hermano, habría bebido hasta saciarse.
Continuaron su camino muy tristes, desfallecidos, casi sin fuerzas para
andar, pero a los pocos pasos tropezaron con un arroyo de agua fresca y
cristalina. Echáronse de bruces para beber y cuando sus secas fauces
estaban a punto de humedecerse, oyeron estas palabras que salían de la
corriente:
—El que de esta agua beba, corderito se ha de volver.
—Hermanita no tomemos...—alcanzó apenas a decir el niño, cuando vió
a su hermana convertida en corderita. La pobrecilla, no oyendo la amenaza
de que si bebía devoraría a su hermano, se apresuró a apagar su sed y
alcanzó a tragar unos cuantos sorbos de aquella agua maldita.
Es fácil suponer en qué estado dejaría esta desgracia a los pobres
hermanos, que ya no tuvieron otro consuelo que conversar y comunicarse
sus penas, porque, por suerte para ellos, al experimentar la niña su
transformación, no había perdido el uso de la palabra. Sin embargo, el niño
lloraba mucho; no podía acostumbrarse a ver a su hermana convertida en
animal.
Un día le salió al paso una viejecita.
—¿Por qué llora tanto, hijito?—le preguntó.
—¿Cómo no he de llorar, mamita, con la desgracia que nos ha sucedido?
¡Qué no daría yo por ver a mi hermana convertida en mujer otra vez!
—Hijito, eso no es posible por ahora; pero con esta varillita de virtud
que voy a ocultar en las lanas de la Corde rita, tendrá ella lo que quiera;
podrá hasta volverse mujer por tres horas cada vez que lo desee, y para
siempre cuando un príncipe quiera casarse con ella.
Y desapareció después de colocar una varita entre las lanas de la
Cordera.
Desde ese momento la Corderita dejó de lamentarse y se la veía brincar
y correr al rededor de su hermano y balar alegremente; porque ha de saberse
que no hablaba con él sino cuando estaban solos.
Pasó algún tiempo, y el niño que ya se había convertido en hombre,
entró a servir como pastor de los rebaños del Rey, el cual, como era muy
bondadoso, le permitió conservar la Corderita a su lado.
Sucedió que en la noche del primer día en que el pastor había entrado en
funciones, el hijo del Rey tuvo que pasar por el patio en que estaban las
habitaciones de los sirvientes, y se extrañó de oir de la más alejada, que era
la que ocupaba el pastor y la Corderita, una voz femenina. Se detuvo a
escuchar para referirle a la Reina, su madre, lo que oyera, pues era
prohibido que las sirvientas penetraran a las piezas de ese patio; pero no
sintió sino murmullos y no alcanzó a entender ni una palabra. Al día
siguiente, el Príncipe refirió a su madre lo sucedido, y en la tarde, cuando el
pastor regresó, después de guardar el ganado, fué conducido a presencia de
la Reina.
A la pregunta que le hizo la Reina de quién era la mujer que en la noche
anterior había estado en su aposento, contestó:
—No estaba, señora, con ninguna mujer, sino con una huachita Cordera
que el Rey mi Señor me ha permitido guardar a mi lado y a la que he
conseguido enseñar varias palabras. (No se atrevió a contarle la verdad).
—¿Y qué palabras sabe? preguntó la Reina admirada.
—Dice ya, papá, mamá, hermano y otras.
—Tráeme la Corderita; quiero verla.
Fué el jóven a su pieza, contó a su hermana lo que había hablado con la
Reina y le aconsejó que mientras tanto no dijese más palabras que las
que él había dicho a la Reina que le había enseñado, y la condujo a la
presencia de la soberana.
La Corderita se bañaba todos los días en el río, de modo que siempre
estaba muy limpia. La Reina quedó encantada y le dijo al pastor que se la
dejase, que ella la cuidaría muy bien.
La Reina le tomó mucho cariño y a todas partes iba con ella. La
Corderita la llamaba mamá; al Rey le decía papá, y al Príncipe hermano.
La Reina se dijo un día:—Si un rústico pastor ha podido enseñar a este
animalito a pronunciar unas cuantas palabras, ¿por qué no he de conseguir
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookgate.com

You might also like