Math Adventures with Python An Illustrated Guide to Exploring Math with Code 1st Edition Peter Farrell pdf download
Math Adventures with Python An Illustrated Guide to Exploring Math with Code 1st Edition Peter Farrell pdf download
https://textbookfull.com/product/math-adventures-with-python-an-
illustrated-guide-to-exploring-math-with-code-1st-edition-peter-
farrell/
https://textbookfull.com/product/practical-business-math-
procedures-with-business-math-handbook-jeffrey-slater/
https://textbookfull.com/product/doing-math-with-python-use-
programming-to-explore-algebra-statistics-calculus-and-more-1st-
edition-amit-saha/
https://textbookfull.com/product/math-for-
programmers-3d-graphics-machine-learning-and-simulations-with-
python-1st-edition-paul-orland/
https://textbookfull.com/product/math-for-
programmers-3d-graphics-machine-learning-and-simulations-with-
python-1st-edition-paul-orland-2/
Begin to Code with Python Rob Miles
https://textbookfull.com/product/begin-to-code-with-python-rob-
miles/
https://textbookfull.com/product/learn-to-program-with-small-
basic-an-introduction-to-programming-with-games-art-science-and-
math-early-access-1st-edition-marji/
https://textbookfull.com/product/help-your-kids-with-math-
revised-edition-dk/
https://textbookfull.com/product/math-for-
programmers-3d-graphics-machine-learning-and-simulations-with-
python-meap-v10-1st-edition-paul-orland/
https://textbookfull.com/product/math-smart-the-savvy-student-s-
guide-to-mastering-basic-math-penguin-random-house/
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
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