Get Math Adventures with Python An Illustrated Guide to Exploring Math with Code 1st Edition Peter Farrell free all chapters
Get Math Adventures with Python An Illustrated Guide to Exploring Math with Code 1st Edition Peter Farrell free all chapters
com
https://textbookfull.com/product/math-adventures-with-
python-an-illustrated-guide-to-exploring-math-with-code-1st-
edition-peter-farrell/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/practical-business-math-procedures-
with-business-math-handbook-jeffrey-slater/
textboxfull.com
https://textbookfull.com/product/doing-math-with-python-use-
programming-to-explore-algebra-statistics-calculus-and-more-1st-
edition-amit-saha/
textboxfull.com
https://textbookfull.com/product/math-for-programmers-3d-graphics-
machine-learning-and-simulations-with-python-1st-edition-paul-orland/
textboxfull.com
https://textbookfull.com/product/math-for-programmers-3d-graphics-
machine-learning-and-simulations-with-python-1st-edition-paul-
orland-2/
textboxfull.com
Begin to Code with Python Rob Miles
https://textbookfull.com/product/begin-to-code-with-python-rob-miles/
textboxfull.com
https://textbookfull.com/product/help-your-kids-with-math-revised-
edition-dk/
textboxfull.com
https://textbookfull.com/product/math-for-programmers-3d-graphics-
machine-learning-and-simulations-with-python-meap-v10-1st-edition-
paul-orland/
textboxfull.com
https://textbookfull.com/product/math-smart-the-savvy-student-s-guide-
to-mastering-basic-math-penguin-random-house/
textboxfull.com
MAKE MATH
M AT H A D V E N T U R E S
COVERS
COME ALIVE
PYTHON 3
WITH PYTHON
WITH PY THON
M AT H A DV E N T UR E S W I T H PY T HON
A N I L L U S T R A T E D G U I D E T O
Math Adventures with Python will show you how to • Use recursion to create fractals like the Koch
harness the power of programming to keep math snowflake and the Sierpinski triangle E X P L O R I N G M A T H W I T H C O D E
relevant and fun. With the aid of the Python program-
• Generate virtual sheep that graze on grass and
ming language, you’ll learn how to visualize solutions
multiply autonomously
to a range of math problems as you use code to explore
key mathematical concepts like algebra, trigonometry, • Crack secret codes using genetic algorithms PETER FARRELL
matrices, and cellular automata.
As you work through the book’s numerous examples
Once you’ve learned programming basics like loops and increasingly challenging exercises, you’ll code
and variables, you’ll write your own programs to solve your own solutions, create beautiful visualizations,
equations quickly, make cool things like an interactive and see just how much more fun math can be!
rainbow grid, and automate tedious tasks like factoring
ABOUT THE AUTHOR
numbers and finding square roots. You’ll learn how to
write functions to draw and manipulate shapes, create Peter Farrell is a math and computer science teacher
oscillating sine waves, and solve equations graphically. with a passion for customizing (”hacking”) math
education and learning with technology. He lives in
You’ll also learn how to:
the San Francisco Bay Area with his wife and children.
• Draw and transform 2D and 3D graphics with
matrices
T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™
FARRELL
w w w.nostarch.com
$29.95 ($39.95 CDN)
LANGUAGES/PYTHON
SHELVE IN: PROGRAMMING
“ I L I E F L AT .”
This book uses a durable binding that won’t snap shut.
Math Adventures
with Python
Math
Adventu res
with Py thon
A n I llu s t r at e d G u i d e to
E x p l o r i n g M at h w it h C o d e
b y P e t e r Fa r r e ll
San Francisco
Math Adventures with Python. Copyright © 2019 by Peter Farrell.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-10: 1-59327-867-5
ISBN-13: 978-1-59327-867-0
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900; [email protected]
www.nostarch.com
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have
any liability to any person or entity with respect to any loss or damage caused or alleged to be caused
directly or indirectly by the information contained in it.
This book is dedicated to all my students,
from whom I’ve learned so much.
About the Author
Peter Farrell was a math teacher for eight years, starting first as
a Peace Corps volunteer in Kenya. He then worked as a computer
science teacher for three years. After reading Seymour Papert’s
Mindstorms and being introduced to Python by a student, he was
inspired to bring programming into math class. He is passionate
about using computers to make learning math more relevant, fun,
and challenging.
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Co nte nts i n Detai l
Acknowledgments xv
Introduction xvii
The Problem with School Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Who Should Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
What's in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Downloading and Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Starting IDLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Installing Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
1
Drawing Polygons with the Turtle module 3
Python’s turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Importing the turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Moving Your Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Changing Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Repeating Code with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Using the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Using a for Loop to Draw a Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Creating Shortcuts with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Using Variables to Draw Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Using Variables in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Variable Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Equilateral Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Writing the triangle() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Making Variables Vary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2
Making Tedious Arithmetic Fun
with Lists and Loops 19
Basic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Operating on Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Using Operators to Write the average() Function . . . . . . . . . . . . . . . . . . . . . . 21
Mind the Order of Operations! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Using Parentheses with Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Data Types in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Integers and Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Checking Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Using Lists to Store Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Adding Items to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Operating on Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Removing Items from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using Lists in Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Accessing Individual Items with List Indices . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Accessing Index and Value with enumerate() . . . . . . . . . . . . . . . . . . . . . . . . . 29
Indices Start at Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Accessing a Range of List Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Finding Out the Index of an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Strings Use Indices, Too . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Summation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Creating the running_sum Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Writing the mySum() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Finding the Average of a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3
Guessing and Checking with Conditionals 37
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Making Decisions with if and else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Using Conditionals to Find Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Writing the factors.py Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
The Wandering Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Creating a Number-Guessing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Making a Random Number Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Taking User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Converting User Input to Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using Conditionals to Check for a Correct Guess . . . . . . . . . . . . . . . . . . . . . . 45
Using a Loop to Guess Again! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Tips for Guessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Finding Square Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Applying the Number-Guessing Game Logic . . . . . . . . . . . . . . . . . . . . . . . . . 48
Writing the squareRoot() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
x Contents in Detail
Part 2: Riding into Math Territory
4
Transforming and Storing Numbers
with Algebra 53
Solving First-Degree Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Finding the Formula for First-Degree Equations . . . . . . . . . . . . . . . . . . . . . . . . 55
Writing the equation() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Using print() Instead of return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Solving Higher-Degree Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Using quad() to Solve Quadratic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Using plug() to Solve a Cubic Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Solving Equations Graphically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Getting Started with Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Creating Your Own Graphing Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Graphing an Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Using Guess and Check to Find the Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Writing the guess() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5
Transforming Shapes with Geometry 77
Drawing a Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Specifying Location Using Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Transformation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Translating Objects with translate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Rotating Objects with rotate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Drawing a Circle of Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Drawing a Circle of Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Animating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Creating the t Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Rotating the Individual Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Saving Orientation with pushMatrix() and popMatrix() . . . . . . . . . . . . . . . . . . 88
Rotating Around the Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Creating an Interactive Rainbow Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Drawing a Grid of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Adding the Rainbow Color to Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Drawing Complex Patterns Using Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
A 30-60-90 Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Drawing an Equilateral Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Drawing Multiple Rotating Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Phase-Shifting the Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Finalizing the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Contents in Detail xi
6
Creating Oscillations with Trigonometry 103
Using Trigonometry for Rotations and Oscillations . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Writing Functions to Draw Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Drawing a Hexagon with Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Drawing an Equilateral Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Making Sine Waves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Leaving a Trail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Using Python’s Built-in enumerate() Function . . . . . . . . . . . . . . . . . . . . . . . . . 114
Creating a Spirograph Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Drawing the Smaller Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Rotating the Smaller Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Making Harmonographs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Writing the harmonograph Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Filling the List Instantly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Two Pendulums Are Better Than One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
7
Complex Numbers 127
The Complex Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Adding Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Multiplying a Complex Number by i . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Multiplying Two Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Writing the magnitude() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Creating the Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Writing the mandelbrot() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Adding Color to the Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Creating the Julia Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Writing the julia() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
8
Using Matrices for Computer Graphics
and Systems of Equations 145
What Is a Matrix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Adding Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Multiplying Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Order Matters in Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Drawing 2D Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Transforming Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Transposing Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Rotating Matrices in Real Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Creating 3D Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Creating the Rotation Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Solving Systems of Equations with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Gaussian Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Writing the gauss() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
xii Contents in Detail
Part 3: Blazing Your Own Trail
9
Building Objects with Classes 175
Bouncing Ball Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Making the Ball Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Making the Ball Bounce Off the Wall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Making Multiple Balls Without Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Creating Objects Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Grazing Sheep Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Writing the Class for the Sheep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Programming Sheep to Move Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Creating the energy Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Creating Grass Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Making the Grass Brown when Eaten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Giving Each Sheep a Random Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Programming Sheep to Reproduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Letting the Grass Regrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Providing an Evolutionary Advantage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10
Creating Fractals Using Recursion 201
The Length of a Coastline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
What Is Recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Writing the factorial() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Building a Fractal Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Koch Snowflake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Writing the segment() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Sierpinski Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Square Fractal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Dragon Curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
11
Cellular Automata 225
Creating a Cellular Automaton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Writing a Cell Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Resizing Each Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Making a CA Grow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Putting the Cells into a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Creating the Cell List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Python Lists Are Strange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
List Index Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Letting Your CA Grow Automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Playing the Game of Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
The Elementary Cellular Automaton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Contents in Detail xiii
12
Solving Problems Using Genetic Algorithms 247
Using a Genetic Algorithm to Guess Phrases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Writing the makeList() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Testing the makeList() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Writing the score() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Writing the mutate() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Generating a Random Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Solving the Traveling Salesperson Problem (TSP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Using Genetic Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Writing the calcLength() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Testing the calcLength() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Random Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Applying the Phrase-Guessing Mutation Idea . . . . . . . . . . . . . . . . . . . . . . . . 265
Mutating Two Numbers in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Crossing Over to Improve Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Index 273
xiv Contents in Detail
Ackn owle dg m e nts
I’d like to thank Don “The Mathman” Cohen for showing me how fun and
challenging learning real math can be; Seymour Papert for proving that
coding belongs in math class; Mark Miller for giving me a chance to put
my ideas into action; Hansel Lynn and Wayne Teng of theCoderSchool,
who let me continue to have fun coding with students; and Ken Hawthorn
for sharing my projects at his school. Thank you to my No Starch editors,
Annie Choi, Liz Chadwick, and Meg Sneeringer, for all your help making
this a much better book, and to Paddy Gaunt, whose input is visible all
over this book. This book wouldn’t exist without you all. Thank you to
everybody who said no—you gave me the energy to keep going. Finally,
thank you to Lucy for always believing in me.
I ntro du ctio n
Next to this text, you see a picture of a dynamic sketch with a hundred
or so rotating triangles. It’s a challenging programming project, and if
you want it to rotate the right way (and look cool), you have to find the
centroid of the triangle. In many situations, making cool graphics is nearly
impossible without knowing the math behind geometry, for example. As
you’ll see in this book, knowing a little of the math behind triangles, like
the centroid, will make it easy to create our artworks. A student who knows
math and can create cool designs is more likely to delve into a little geom-
etry and put up with a few square roots or a trig function or two. A student
who doesn’t see any outcome, and is only doing homework from a textbook,
probably doesn’t have much motivation to learn geometry.
In my eight years of experience as a math teacher and three years
of experience as a computer science teacher, I’ve met many more math
learners who prefer the visual approach to the academic one. In the
process of creating something interesting, you come to understand that
math is not just following steps to solve an equation. You see that explor-
ing math with programming allows for many ways to solve interesting
problems, with many unforeseen mistakes and opportunities for improve-
ments along the way.
This is the difference between school math and real math.
xviii Introduction
used to be that all you could actively “do” in math class was solve equations,
factor polynomials, and graph functions. But now that computers can do
most of those calculations for us, these practices are no longer sufficient.
Simply learning how to automate solving, factoring, and graphing is not
the final goal. Once a student has learned to automate a process, they can
go further and deeper into a topic than was ever possible before.
Figure 2 shows a typical math problem you’d find in a textbook, asking
students to define a function, “f(x),” and evaluate it for a ton of values.
This same format goes on for 18 more questions! This kind of exercise
is a trivial problem for a programming language like Python. We could sim-
ply define the function f(x) and then plug in the values by iterating over a
list, like this:
import math
def f(x):
return math.sqrt(x + 3) - x + 1
The last line just makes the output pretty while rounding all the solu-
tions to three decimal places, as shown here:
f(0.000) = 2.732
f(1.000) = 2.000
f(1.414) = 1.687
f(0.414) = 2.434
Introduction xix
you can name a function that calculates the area of a rectangle by calling it
calculateArea(), like this:
def calculateArea(width,height):
xx Introduction
You’ll do this using Python and Processing in order to supercharge
what you can do in math class. This book is not about skipping the math;
it’s about using the newest, coolest tools out there to get creative and learn
real computer skills while discovering the connections between math,
art, science, and technology. Processing will provide the graphics, shapes,
motion, and colors, while Python does the calculating and follows your
instructions behind the scenes.
For each of the projects in this book, you’ll build the code up from
scratch, starting from a blank file, and checking your progress at every
step. Through making mistakes and debugging your own programs, you’ll
get a much deeper understanding of what each block of code does.
Introduction xxi
Chapter 6: Creating Oscillations with Trigonometry goes beyond right
triangles and lets you create oscillating shapes and waves.
Chapter 7: Complex Numbers teaches you how to use complex num-
bers to move points around the screen, creating designs like the
Mandelbrot set.
Chapter 8: Using Matrices for Computer Graphics and Systems of
Equations takes you into the third dimension, where you’ll translate
and rotate 3D shapes and solve huge systems of equations with one
program.
Chapter 9: Building Objects with Classes covers how to create one
object, or as many as your computer can handle, with roaming sheep
and delicious grass locked in a battle for survival.
Chapter 10: Creating Fractals Using Recursion shows how recursion
can be used as a whole new way to measure distances and create wildly
unexpected designs.
Chapter 11: Cellular Automata teaches you how to generate and pro-
gram cellular automata to behave according to rules you make.
Chapter 12: Solving Problems Using Genetic Algorithms shows you
how to harness the theory of natural selection to solve problems we
couldn’t solve in a million years otherwise!
xxii Introduction
You can choose the version for your operating
system. The site detected that I was using Windows.
Click the file when the download is complete, as
Figure 5: Click the down
shown in Figure 5.
loaded file to start the
Follow the directions, and always choose the install
default options. It might take a few minutes to
install. After that, search your system for “IDLE.”
That’s the Python IDE, or integrated development environment, which is what
you’ll need to write Python code. Why “IDLE”? The Python programming
language was named after the Monty Python comedy troupe, and one of
the members is Eric Idle.
Starting IDLE
Find IDLE on your system and open it.
A screen called a “shell” will appear. You can use this for the interactive
coding environment, but you’ll want to save your code. Click FileNew
File or press alt-N, and a file will appear (see Figure 7).
Figure 7: Python’s interactive shell (left) and a new module (file) window, ready for code!
This is where you’ll write your Python code. We will also use Processing,
so let’s go over how to download and install Processing next.
Installing Processing
There’s a lot you can do with Python, and we’ll use IDLE a lot. But when
we want to do some heavy-duty graphics, we’re going to use Processing.
Processing is a professional-level graphics library used by coders and artists
to make dynamic, interactive artwork and graphics.
Introduction xxiii
Go to https://processing.org/download/ and choose your operating system,
as shown in Figure 8.
xxiv Introduction
Part I
Hitchin' Up
Your Python
Wagon
Dr awi n g Po lyg o n s with
1
th e Tu rtle m o du le
We made our way back along the Avenue Gabriel, through the
strolling crowd. I left my grandmother to rest on a seat and went in
search of a cab. She, in whose heart I always placed myself when I
had to form an opinion of the most unimportant person, she was now
closed to me, had become part of the world outside, and, more than
from any casual passer-by, I was obliged to keep from her what I
thought of her condition, to say no word of my uneasiness. I could
not have spoken of it to her in greater confidence than to a stranger.
She had suddenly handed back to me the thoughts, the griefs which,
from the days of my infancy, I had entrusted for all time to her
keeping. She was not yet dead. I was already alone. And even those
allusions which she had made to the Guermantes, to Mme. de
Sévigné, to our conversations about the little clan, assumed an air of
being without point or occasion, fantastic, because they sprang from
the nullity of this very being who to-morrow possibly would have
ceased to exist, for whom they would no longer have any meaning,
from that nullity, incapable of conceiving them, which my
grandmother would shortly be.
“Well, sir, I don’t like to say no, but you have not made an
appointment, you have no time fixed. Besides, this is not my day for
seeing patients. You surely have a doctor of your own. I cannot
interfere with his practice, unless he were to call me in for a
consultation. It’s a question of professional etiquette....”
Just as I was signalling to a cabman, I had caught sight of the
famous Professor E——, almost a friend of my father and
grandfather, acquainted at any rate with them both, who lived in the
Avenue Gabriel, and, with a sudden inspiration, had stopped him just
as he was entering his house, thinking that he would perhaps be the
very person to advise my grandmother. But he was evidently in a
hurry and, after calling for his letters, seemed anxious to get rid of
me, so that my only chance of speaking to him lay in going up with
him in the lift, of which he begged me to allow him to work the
switches himself, this being a mania with him.
“But, sir, I am not asking you to see my grandmother here; you will
realise from what I am trying to tell you that she is not in a fit state to
come; what I am asking is that you should call at our house in half an
hour’s time, when I have taken her home.”
“Call at your house! Really, sir, you must not expect me to do that.
I am dining with the Minister of Commerce. I have a call to pay first. I
must change at once, and to make matters worse I have torn my
coat and my other one has no buttonholes for my decorations. I beg
you, please, to oblige me by not touching the switches. You don’t
know how the lift works; one can’t be too careful. Getting that
buttonhole made means more delay. Well, as I am a friend of your
people, if your grandmother comes here at once I will see her. But I
warn you that I shall be able to give her exactly a quarter of an hour,
not a moment more.”
I had started off at once, without even getting out of the lift which
Professor E—— had himself set in motion to take me down again,
casting a suspicious glance at me as he did so.
We may, indeed, say that the hour of death is uncertain, but when
we say so we represent that hour to ourselves as situated in a vague
and remote expanse of time, it never occurs to us that it can have
any connexion with the day that has already dawned, or may signify
that death—or its first assault and partial possession of us, after
which it will never leave hold of us again—may occur this very
afternoon, so far from uncertain, this afternoon every hour of which
has already been allotted to some occupation. You make a point of
taking your drive every day so that in a month’s time you will have
had the full benefit of the fresh air; you have hesitated over which
cloak you will take, which cabman to call, you are in the cab, the
whole day lies before you, short because you have to be at home
early, as a friend is coming to see you; you hope that it will be as fine
again to-morrow; and you have no suspicion that death, which has
been making its way towards you along another plane, shrouded in
an impenetrable darkness, has chosen precisely this day of all days
to make its appearance, in a few minutes’ time, more or less, at the
moment when the carriage has reached the Champs-Elysées.
Perhaps those who are haunted as a rule by the fear of the utter
strangeness of death will find something reassuring in this kind of
death—in this kind of first contact with death—because death thus
assumes a known, familiar guise of everyday life. A good luncheon
has preceded it, and the same outing that people take who are in
perfect health. A drive home in an open carriage comes on top of its
first onslaught; ill as my grandmother was, there were, after all,
several people who could testify that at six o’clock, as we came
home from the Champs-Elysées, they had bowed to her as she
drove past in an open carriage, in perfect weather. Legrandin,
making his way towards the Place de la Concorde, raised his hat to
us, stopping to look after us with an air of surprise. I, who was not
yet detached from life, asked my grandmother if she had
acknowledged his greeting, reminding her of his readiness to take
offence. My grandmother, thinking me no doubt very frivolous, raised
her hand in the air as though to say: “What does it matter? It is not of
the least importance.”
Yes, one might have said that, a few minutes earlier, when I was
looking for a cab, my grandmother was resting on a seat in the
Avenue Gabriel, and that a little later she had driven past in an open
carriage. But would that have been really true? The seat, for
instance, to maintain its position at the side of an avenue—for all that
it may be subjected also to certain conditions of equilibrium—has no
need of energy. But in order that a living person may be stable, even
when supported by a seat or in a carriage, there is required a tension
of forces which we do not ordinarily perceive, any more than we
perceive (because its action is universal) atmospheric pressure.
Possibly if we were to be hollowed out and then left to support the
pressure of the air we might feel, in the moment that preceded our
extinction, that terrible weight which there was nothing left in us to
neutralise. Similarly when the abyss of sickness and death opens
within us, and we have no longer any resistance to offer to the tumult
with which the world and our own body rush upon us, then to endure
even the tension of our own muscles, the shudder that freezes us to
the marrow, then even to keep ourself motionless in what we
ordinarily regard as nothing but the simple negative position of a
lifeless thing requires, if we wish our head to remain erect and our
eyes calm, an expense of vital energy and becomes the object of an
exhausting struggle.
And if Legrandin had looked back at us with that astonished air, it
was because to him, as to the other people who passed us then, in
the cab in which my grandmother was apparently seated she had
seemed to De foundering, sliding into the abyss, clinging desperately
to the cushions which could barely arrest the downward plunge of
her body, her hair in disorder, her eye wild, unable any longer to face
the assault of the images which its pupil was not strong enough now
to bear. She had appeared to them, although I was still by her side,
submerged in that unknown world somewhere in which she had
already received the blows, traces of which she still bore when I
looked up at her a few minutes earlier in the Champs-Elysées, her
hat, her face, her cloak left in disorder by the hand of the invisible
angel with whom she had wrestled. I have thought, since, that this
moment of her stroke cannot have altogether surprised my
grandmother, that indeed she had perhaps foreseen it a long time
back, had lived in expectation of it. She had not known, naturally,
when this fatal moment would come, had never been certain, any
more than those lovers whom a similar doubt leads alternately to
found unreasonable hopes and unjustified suspicions on the fidelity
of their mistresses. But it is rarely that these grave maladies, like that
which now at last had struck her full in the face, do not take up their
abode in the sick man for a long time before killing him, during which
time they make haste, like a “sociable” neighbour or tenant, to
introduce themselves to him. A terrible acquaintance, not so much
from the sufferings that it causes as from the strange novelty of the
definite restriction which it imposes upon life. A woman sees herself
dying, in these cases not at the actual moment of death but months,
sometimes years before, when death has hideously come to dwell in
her. The sufferer makes the acquaintance of the stranger whom she
hears coming and going in her brain. She does not know him by
sight, it is true, but from the sounds which she hears him regularly
make she can form an idea of his habits. Is he a criminal? One
morning, she can no longer hear him. He has gone. Ah! If it were
only for ever! In the evening he has returned. What are his plans?
Her specialist, put to the question, like an adored mistress, replies
with avowals that one day are believed, another day fail to convince
her. Or rather it is not the mistress’s part but that of the servants one
interrogates that the doctor plays. They are only third parties. The
person whom we press for an answer, whom we suspect of being
about to play us false, is life itself, and although we feel her to be no
longer the same we believe in her still or at least remain undecided
until the day on which she finally abandons us.
I helped my grandmother into Professor E——’s lift and a moment
later he came to us and took us into his consulting room. But there,
busy as he was, his bombastic manner changed, such is the force of
habit; for his habit was to be friendly, that is to say lively with his
patients. Since he knew that my grandmother was a great reader,
and was himself one also, he devoted the first few minutes to
quoting various favourite passages of poetry appropriate to the
glorious summer weather. He had placed her in an armchair and
himself with his back to the light so as to have a good view of her.
His examination was minute and thorough, even obliging me at one
moment to leave the room. He continued it after my return, then,
having finished, went on, although the quarter of an hour was almost
at an end, repeating various quotations to my grandmother. He even
made a few jokes, which were witty enough, though I should have
preferred to hear them on some other occasion, but which
completely reassured me by the tone of amusement in which he
uttered them. I then remembered that M. Fallières, the President of
the Senate, had, many years earlier, had a false seizure, and that to
the consternation of his political rivals he had returned a few days
later to his duties and had begun, it was said, his preparations for a
more or less remote succession to the Presidency of the Republic.
My confidence in my grandmother’s prompt recovery was all the
more complete in that, just as I was recalling the example of M.
Fallières, I was distracted from following up the similarity by a shout
of laughter, which served as conclusion to one of the Professor’s
jokes. After which he took out his watch, wrinkled his brows
petulantly on seeing that he was five minutes late, and while he bade
us good-bye rang for his other coat to be brought to him at once. I
waited until my grandmother had left the room, closed the door and
asked him to tell me the truth.
“There is not the slightest hope,” he informed me. “It is a stroke
brought on by uraemia. In itself, uraemia is not necessarily fatal, but
this case seems to me desperate. I need not tell you that I hope I am
mistaken. Anyhow, you have Cottard, you’re in excellent hands.
Excuse me,” he broke off as a maid came into the room with his coat
over her arm. “I told you, I’m dining with the Minister of Commerce,
and I have a call to pay first. Ah! Life is not all a bed of roses, as one
is apt to think at your age.”
And he graciously offered me his hand. I had shut the door behind
me, and a footman was shewing us into the hall when we heard a
loud shout of rage. The maid had forgotten to cut and hem the
buttonhole for the decorations. This would take another ten minutes.
The Professor continued to storm while I stood on the landing gazing
at a grandmother for whom there was not the slightest hope. Each of
us is indeed alone. We started for home.
The sun was sinking, it burnished an interminable wall along which
our cab had to pass before reaching the street in which we lived, a
wall against which the shadow cast by the setting sun of horse and
carriage stood out in black on a ruddy background, like a funeral car
on some Pompeian terra-cotta. At length we arrived at the house. I
made the invalid sit at the foot of the staircase in the hall, and went
up to warn my mother. I told her that my grandmother had come
home feeling slightly unwell, after an attack of giddiness. As soon as
I began to speak, my mother’s face was convulsed by the paroxysm
of a despair which was yet already so resigned that I realised that for
many years she had been holding herself quietly in readiness for an
uncalendared but final day. She asked me no question; it seemed
that, just as malevolence likes to exaggerate the sufferings of other
people, so in her devotion she would not admit that her mother was
seriously ill, especially with a disease which might affect the brain.
Mamma shuddered, her eyes wept without tears, she ran to give
orders for the doctor to be fetched at once; but when Françoise
asked who was ill she could not reply, her voice stuck in her throat.
She came running downstairs with me, struggling to banish from her
face the sob that contracted it. My grandmother was waiting below
on the sofa in the hall, but, as soon as she heard us coming, drew
herself together, stood up, and waved her hand cheerfully at
Mamma. I had partially wrapped her head in a white lace shawl,
telling her that it was so that she should not catch cold on the stairs. I
had hoped that my mother would not notice the change in her face,
the distortion of her mouth; my precaution proved unnecessary; my
mother went up to my grandmother, kissed her hand as though it
were that of her God, raised her up, carried her to the lift with infinite
precautions in which there was, with the fear of hurting her by any
clumsy movement, the humility of one who felt herself unworthy to
touch the most precious thing, to her, in the world, but never once
did she raise her eyes, nor look at the sufferer’s face. Perhaps this
was in order that my grandmother might not be saddened by the
thought that the sight of her could alarm her daughter. Perhaps from
fear of a grief so piercing that she dared not face it. Perhaps from
reverence, because she did not feel it permissible to herself, without
impiety, to remark the trace of any mental weakening on those
venerated features. Perhaps to be better able to preserve intact in
her memory the image of the true face of my grandmother, radiant
with wisdom and goodness. So they went up side by side, my
grandmother half hidden by her shawl, my mother turning away her
eyes.
Meanwhile there was one person who never took hers from what
could be made out of my grandmother’s altered features, at which
her daughter dared not look, a person who fastened on them a gaze
wondering, indiscreet and of evil omen: this was Françoise. Not that
she was not sincerely attached to my grandmother (indeed she had
been disappointed and almost scandalised by the coldness shewn
by Mamma, whom she would have liked to see fling herself weeping
into her mother’s arms), but she had a certain tendency always to
look at the worse side of things, she had retained from her childhood
two peculiarities which would seem to be mutually exclusive, but
which when combined strengthened one another: the want of
restraint common among people of humble origin who make no
attempt to conceal the impression, in other words the painful alarm
aroused in them by the sight of a physical change which it would be
in better taste to appear not to notice, and the unfeeling coarseness
of the peasant who begins by tearing the wings off dragonflies until
she is allowed to wring the necks of chickens, and lacks that
modesty which would make her conceal the interest that she feels in
the sight of suffering flesh.
When, thanks to the faultless ministrations of Françoise, my
grandmother had been put to bed, she discovered that she could
speak much more easily, the little rupture or obstruction of a blood-
vessel which had produced the uraemia having apparently been
quite slight. And at once she was anxious not to fail Mamma in her
hour of need, to assist her in the most cruel moments through which
she had yet had to pass.
“Well, my child,” she began, taking my mother’s hand in one of her
own, and keeping the other in front of her lips, so as to account for
the slight difficulty which she still found in uttering certain words. “So
this is all the pity you shew your mother! You look as if you thought
that indigestion was quite a pleasant thing!”
Then for the first time my mother’s eyes gazed passionately into
those of my grandmother, not wishing to see the rest of her face, and
she replied, beginning the list of those false promises which we
swear but are unable to fulfil:
“Mamma, you will soon be quite well again, your daughter will see
to that.”
And embodying all her dearest love, all her determination that her
mother should recover, in a kiss to which she entrusted them, and
which she followed with her mind, with her whole being until it
flowered upon her lips, she bent down to lay it humbly, reverently
upon the precious brow. My grandmother complained of a sort of
alluvial deposit of bedclothes which kept gathering all the time in the
same place, over her left leg, and from which she could never
manage to free herself. But she did not realise that she was herself
the cause of this (so that day after day she accused Françoise
unjustly of not “doing” her bed properly). By a convulsive movement
she kept flinging to that side the whole flood of those billowing
blankets of fine wool, which gathered there like the sand in a bay
which is very soon transformed into a beach (unless the inhabitants
construct a breakwater) by the successive deposits of the tide.
My mother and I (whose falsehood was exposed before we spoke
by the obnoxious perspicacity of Françoise) would not even admit
that my grandmother was seriously ill, as though such an admission
might give pleasure to her enemies (not that she had any) and it was
more loving to feel that she was not so bad as all that, in short from
the same instinctive sentiment which had led me to suppose that
Andrée was too sorry for Albertine to be really fond of her. The same
individual phenomena are reproduced in the mass, in great crises. In
a war, the man who does not love his country says nothing against it,
but regards it as lost, commiserates it, sees everything in the darkest
colours.
Françoise was of infinite value to us owing to her faculty of doing
without sleep, of performing the most arduous tasks. And if, when
she had gone to bed after several nights spent in the sickroom, we
were obliged to call her a quarter of an hour after she had fallen
asleep, she was so happy to be able to do the most tiring duties as if
they had been the simplest things in the world that, so far from
looking cross, her face would light up with a satisfaction tinged with
modesty. Only when the time came for mass, or for breakfast, then,
had my grandmother been in her death agony, still Françoise would
have quietly slipped away so as not to make herself late. She neither
could nor would let her place be taken by her young footman. It was
true that she had brought from Combray an extremely exalted idea
of everyone’s duty towards ourselves; she would not have tolerated
that any of our servants should “fail” us. This doctrine had made her
so noble, so imperious, so efficient an instructor that there had never
come to our house any servants, however corrupted, who had not
speedily modified, purified their conception of life so far as to refuse
to touch the usual commissions from tradesmen and to come
rushing—however little they might previously have sought to oblige
—to take from my hands and not let me tire myself by carrying the
smallest package. But at Combray Françoise had contracted also—
and had brought with her to Paris—the habit of not being able to put
up with any assistance in her work. The sight of anyone coming to
help her seemed to her like receiving a deadly insult, and servants
had remained for weeks in the house without receiving from her any
response to their morning greeting, had even gone off on their
holidays without her bidding them good-bye or their guessing her
reason, which was simply and solely that they had offered to do a
share of her work on some day when she had not been well. And at
this moment when my grandmother was so ill Françoise’s duties
seemed to her peculiarly her own. She would not allow herself, she,
the official incumbent, to be done out of her part in the ritual of these
festal days. And so her young footman, sent packing by her, did not
know what to do with himself, and not content with having copied the
butler’s example and supplied himself with note-paper from my desk
had begun as well to borrow volumes of poetry from my
bookshelves. He sat reading them for a good half of the day, out of
admiration for the poets who had written them, but also so as, during
the rest of his time, to begem with quotations the letters which he
wrote to his friends in his native village. Naturally he expected these
to dazzle them. But as there was little sequence in his ideas he had
formed the notion that these poems, picked out at random from my
shelves, were matters of common knowledge, to which it was
customary to refer. So much so that in writing to these peasants,
whose stupefaction he discounted, he interspersed his own
reflexions with lines from Lamartine, just as he might have said “Who
laughs last, laughs longest!” or merely “How are you keeping?”
To ease her pain my grandmother was given morphine.
Unfortunately, if this relieved her in other ways, it increased the
quantity of albumen. The blows which we aimed at the wicked ogre
who had taken up his abode in my grandmother were always wide of
the mark, and it was she, her poor interposed body that had to bear
them, without her ever uttering more than a faint groan by way of
complaint. And the pain that we caused her found no compensation
in a benefit which we were unable to give her. The savage ogre
whom we were anxious to exterminate we barely succeeded in
touching, and all we did was to enrage him still further, and possibly
hasten the moment at which he would devour his luckless captive.
On certain days when the discharge of albumen had been excessive
Cottard, after some hesitation, stopped the morphine. In this man, so
insignificant, so common, there was, in these brief moments in which
he deliberated, in which the relative dangers of one and another
course of treatment presented themselves alternately to his mind
until he arrived at a decision, the same sort of greatness as in a
general who, vulgar in all the rest of his life, is a great strategist, and
in an hour of peril, after a moment’s reflexion, decides upon what is
from the military point of view the wisest course, and gives the order:
“Advance eastwards.” Medically, however little hope there might be
of setting any limit to this attack of uraemia, it did not do to tire the
kidneys. But, on the other hand, when my grandmother did not have
morphine, her pain became unbearable; she perpetually attempted a
certain movement which it was difficult for her to perform without
groaning. To a great extent, suffering is a sort of need felt by the
organism to make itself familiar with a new state, which makes it
uneasy, to adapt its sensibility to that state. We can discern this
origin of pain in the case of certain inconveniences which are not
such for everyone. Into a room filled with a pungent smoke two men
of a coarse fibre will come and attend to their business; a third, more
highly strung, will betray an incessant discomfort. His nostrils will
continue to sniff anxiously the odour he ought, one would say, to try
not to notice but will keep on attempting to attach, by a more exact
apprehension of it, to his troubled sense of smell. One consequence
of which may well be that his intense preoccupation will prevent him
from complaining of a toothache. When my grandmother was in pain
the sweat trickled over the pink expanse of her brow, glueing to it her
white locks, and if she thought that none of us was in the room she
would cry out: “Oh, it’s dreadful!” but if she caught sight of my
mother, at once she employed all her energy in banishing from her
face every sign of pain, or—an alternative stratagem—repeated the
same plaints, accompanying them with explanations which gave a
different sense, retrospectively, to those which my mother might
have overheard:
“Oh! My dear, it’s dreadful to have to stay in bed on a beautiful
sunny day like this when one wants to be out in the air; I am crying
with rage at your orders.”
But she could not get rid of the look of anguish in her eyes, the
sweat on her brow, the convulsive start, checked at once, of her
limbs.
“There is nothing wrong. I’m complaining because I’m not lying
very comfortably. I feel my hair is untidy, my heart is bad, I knocked
myself against the wall.”
And my mother, at the foot of the bed, rivetted to that suffering
form, as though, by dint of piercing with her gaze that pain-bedewed
brow, that body which hid the evil thing within it, she could have
succeeded in reaching that evil thing and carrying it away, my
mother said:
“No, no, Mamma dear, we won’t let you suffer like that, we will find
something to take it away, have patience just for a moment; let me
give you a kiss, darling—no, you’re not to move.”
And stooping over the bed, with bended knees, almost kneeling on
the ground, as though by an exercise of humility she would have a
better chance of making acceptable the impassioned gift of herself,
she lowered towards my grandmother her whole life contained in her
face as in a ciborium which she extended over her, adorned in relief
with dimples and folds so passionate, so sorrowful, so sweet that
one knew not whether they had been carved by the chisel of a kiss,
a sob or a smile. My grandmother, also, tried to lift up her face to
Mamma’s. It was so altered that probably, had she been strong
enough to go out, she would have been recognised only by the
feather in her hat. Her features, like the clay in a sculptor’s hands,
seemed to be straining, with an effort which distracted her from
everything else, to conform to some particular model which we failed
to identify. This business of modelling was now almost finished, and
if my grandmother’s face had shrunk in the process it had at the
same time hardened. The veins that ran beneath its surface seemed
those not of a piece of marble but of some more rugged stone.
Constantly thrust forwards by the difficulty that she found in
breathing and as constantly forced back on to her pillow by
exhaustion, her face, worn, diminished, terribly expressive, seemed
like, in a primitive, almost prehistoric carving, the rude, flushed,
purplish, desperate face of some savage guardian of a tomb. But the
whole task was not yet accomplished. Next, her resistance must be
overcome, and that tomb, the entrance to which she had so painfully
guarded, with that tense contraction, entered.
In one of those moments in which, as the saying goes, one does
not know what saint to invoke, as my grandmother was coughing
and sneezing a good deal, we took the advice of a relative who
assured us that if we sent for the specialist X—— he would get rid of
all that in a couple of days. People say that sort of thing about their
own doctors, and their friends believe them just as Françoise always
believed the advertisements in the newspapers. The specialist came
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com