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 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
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 145 and systems of equations 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 Contents in Detail
Part 3 Blazing Your own trail 9 175 Building objects with Classes 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