CONTENTS IN DETAIL

ACKNOWLEDGMENTS

INTRODUCTION

The Problem with School Math

About This Book

Who Should Use This Book

What’s in This Book?

Downloading and Installing Python

Starting IDLE

Installing Processing

PART I: HITCHIN’ UP YOUR PYTHON WAGON

1
DRAWING POLYGONS WITH THE TURTLE MODULE

Python’s turtle Module

Importing the turtle Module

Moving Your Turtle

Changing Directions

Repeating Code with Loops

Using the for Loop

Using a for Loop to Draw a Square

Creating Shortcuts with Functions

Using Variables to Draw Shapes

Using Variables in Functions

Variable Errors

Equilateral Triangles

Writing the triangle() Function

Making Variables Vary

Summary

2
MAKING TEDIOUS ARITHMETIC FUN WITH LISTS AND LOOPS

Basic Operators

Operating on Variables

Using Operators to Write the average() Function

Mind the Order of Operations!

Using Parentheses with Operators

Data Types in Python

Integers and Floats

Strings

Booleans

Checking Data Types

Using Lists to Store Values

Adding Items to a List

Operating on Lists

Removing Items from a List

Using Lists in Loops

Accessing Individual Items with List Indices

Accessing Index and Value with enumerate()

Indices Start at Zero

Accessing a Range of List Items

Finding Out the Index of an Item

Strings Use Indices, Too

Summation

Creating the running_sum Variable

Writing the mySum() Function

Finding the Average of a List of Numbers

Summary

3
GUESSING AND CHECKING WITH CONDITIONALS

Comparison Operators

Making Decisions with if and else Statements

Using Conditionals to Find Factors

Writing the factors.py Program

The Wandering Turtle

Creating a Number-Guessing Game

Making a Random Number Generator

Taking User Input

Converting User Input to Integers

Using Conditionals to Check for a Correct Guess

Using a Loop to Guess Again!

Tips for Guessing

Finding Square Roots

Applying the Number-Guessing Game Logic

Writing the squareRoot() Function

Summary

PART 2: RIDING INTO MATH TERRITORY

4
TRANSFORMING AND STORING NUMBERS WITH ALGEBRA

Solving First-Degree Equations

Finding the Formula for First-Degree Equations

Writing the equation() Function

Using print() Instead of return

Solving Higher-Degree Equations

Using quad() to Solve Quadratic Equations

Using plug() to Solve a Cubic Equation

Solving Equations Graphically

Getting Started with Processing

Creating Your Own Graphing Tool

Graphing an Equation

Using Guess and Check to Find the Roots

Writing the guess() Function

Summary

5
TRANSFORMING SHAPES WITH GEOMETRY

Drawing a Circle

Specifying Location Using Coordinates

Transformation Functions

Translating Objects with translate()

Rotating Objects with rotate()

Drawing a Circle of Circles

Drawing a Circle of Squares

Animating Objects

Creating the t Variable

Rotating the Individual Squares

Saving Orientation with pushMatrix() and popMatrix()

Rotating Around the Center

Creating an Interactive Rainbow Grid

Drawing a Grid of Objects

Adding the Rainbow Color to Objects

Drawing Complex Patterns Using Triangles

A 30-60-90 Triangle

Drawing an Equilateral Triangle

Drawing Multiple Rotating Triangles

Phase-Shifting the Rotation

Finalizing the Design

Summary

6
CREATING OSCILLATIONS WITH TRIGONOMETRY

Using Trigonometry for Rotations and Oscillations

Writing Functions to Draw Polygons

Drawing a Hexagon with Loops

Drawing an Equilateral Triangle

Making Sine Waves

Leaving a Trail

Using Python’s Built-in enumerate() Function

Creating a Spirograph Program

Drawing the Smaller Circle

Rotating the Smaller Circle

Making Harmonographs

Writing the harmonograph Program

Filling the List Instantly

Two Pendulums Are Better Than One

Summary

7
COMPLEX NUMBERS

The Complex Coordinate System

Adding Complex Numbers

Multiplying a Complex Number by i

Multiplying Two Complex Numbers

Writing the magnitude() Function

Creating the Mandelbrot Set

Writing the mandelbrot() Function

Adding Color to the Mandelbrot Set

Creating the Julia Set

Writing the julia() Function

Summary

8
USING MATRICES FOR COMPUTER GRAPHICS AND SYSTEMS OF EQUATIONS

What Is a Matrix?

Adding Matrices

Multiplying Matrices

Order Matters in Matrix Multiplication

Drawing 2D Shapes

Transforming Matrices

Transposing Matrices

Rotating Matrices in Real Time

Creating 3D Shapes

Creating the Rotation Matrix

Solving Systems of Equations with Matrices

Gaussian Elimination

Writing the gauss() Function

Summary

PART 3: BLAZING YOUR OWN TRAIL

9
BUILDING OBJECTS WITH CLASSES

Bouncing Ball Program

Making the Ball Move

Making the Ball Bounce Off the Wall

Making Multiple Balls Without Classes

Creating Objects Using Classes

Grazing Sheep Program

Writing the Class for the Sheep

Programming Sheep to Move Around

Creating the energy Property

Creating Grass Using Classes

Making the Grass Brown when Eaten

Giving Each Sheep a Random Color

Programming Sheep to Reproduce

Letting the Grass Regrow

Providing an Evolutionary Advantage

Summary

10
CREATING FRACTALS USING RECURSION

The Length of a Coastline

What Is Recursion?

Writing the factorial() Function

Building a Fractal Tree

Koch Snowflake

Writing the segment() Function

Sierpinski Triangle

Square Fractal

Dragon Curve

Summary

11
CELLULAR AUTOMATA

Creating a Cellular Automaton

Writing a Cell Class

Resizing Each Cell

Making a CA Grow

Putting the Cells into a Matrix

Creating the Cell List

Python Lists Are Strange

List Index Notation

Letting Your CA Grow Automatically

Playing the Game of Life

The Elementary Cellular Automaton

Summary

12
SOLVING PROBLEMS USING GENETIC ALGORITHMS

Using a Genetic Algorithm to Guess Phrases

Writing the makeList() Function

Testing the makeList() Function

Writing the score() Function

Writing the mutate() Function

Generating a Random Number

Solving the Traveling Salesperson Problem (TSP)

Using Genetic Algorithms

Writing the calcLength() Method

Testing the calcLength() Method

Random Routes

Applying the Phrase-Guessing Mutation Idea

Mutating Two Numbers in a List

Crossing Over to Improve Routes

Summary

INDEX