Math Adventures with Python: An Illustrated Guide to Exploring Math with Code
304Math Adventures with Python: An Illustrated Guide to Exploring Math with Code
304eBook
Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
Related collections and offers
Overview
Math Adventures with Python will show you how to harness the power of programming to keep math relevant and fun. With the aid of the Python programming language, you'll learn how to visualize solutions to a range of math problems as you use code to explore key mathematical concepts like algebra, trigonometry, matrices, and cellular automata.
Once you've learned the programming basics like loops and variables, you'll write your own programs to solve equations quickly, make cool things like an interactive rainbow grid, and automate tedious tasks like factoring numbers and finding square roots. You'll learn how to write functions to draw and manipulate shapes, create oscillating sine waves, and solve equations graphically.
You'll also learn how to:
- Draw and transform 2D and 3D graphics with matrices
- Make colorful designs like the Mandelbrot and Julia sets with complex numbers
- Use recursion to create fractals like the Koch snowflake and the Sierpinski triangle
- Generate virtual sheep that graze on grass and multiply autonomously
- Crack secret codes using genetic algorithms
As you work through the book's numerous examples and increasingly challenging exercises, you'll code your own solutions, create beautiful visualizations, and see just how much more fun math can be!
Product Details
ISBN-13: | 9781593278687 |
---|---|
Publisher: | No Starch Press |
Publication date: | 01/08/2019 |
Sold by: | Penguin Random House Publisher Services |
Format: | eBook |
Pages: | 304 |
File size: | 24 MB |
Note: | This product may take a few minutes to download. |
Age Range: | 10 Years |
About the Author
Table of Contents
About the Author vi
About the Technical Reviewer vi
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 xxiv
Part 1 Hitchin' Up Your Python Wagon
1 Dmmma 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
Booleons 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
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 on 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 on 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
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 on Equilateral Triangle 109
Making Sine Waves 110
Leaving a Trail 113
Using Python's Built-in enumerated Function 114
Creating 1 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 Julio 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
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
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