Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More!
3 Enhanced Projectile Trajectory Comparison Program 56 4 Visualizing Your Expenses 56 5 Exploring the Relationship Between the Fibonacci Sequence and the Golden Ratio 59 3 Describing Data with statistics 61 Finding the Mean 62 Finding the Median 63 Finding the Mode and Creating a Frequency Table 65 Finding the Most Common Elements 66 Finding the Mode 67 Creating a Frequency Table 69 Measuring the Dispersion 71 Finding the Range of a Set of Numbers 71 Finding the Variance and Standard Deviation 72 Calculating the Correlation Between Two Data Sets 75 Calculating the Correlation Coefficient 76 High School Grades and Performance on College Admission Tests 78 Scatter Plots 81 Reading Data from Files 83 Reading Data from a Text File 84 Reading Data from a CSV File 86 What You Learned 89 Programming Challenges 89 1 Better Correlation Coefficient Finding Program 89 2 Statistics Calculator 89 3 Experiment with Other CSV Data 89 4 Finding the Percentile 89 5 Creating a Grouped Frequency Table 90 4 93 algebra anD syMbolic Math with syMPy Defining Symbols and Symbolic Operations 94 Working with Expressions 96 Factorizing and Expanding Expressions 96 Pretty Printing 97 Substituting in Values 100 Converting Strings to Mathematical Expressions 103 Solving Equations 105 Solving Quadratic Equations 106 Solving for One Variable in Terms of Others 106 Solving a System of Linear Equations 108 Plotting Using SymPy 108 Plotting Expressions Input by the User 111 Plotting Multiple Functions 113 What You Learned 115 Programming Challenges 115 1 Factor Finder 115 2 Graphical Equation Solver 115 3 Summing a Series 116 4 Solving Single-Variable Inequalities 117 x Contents in Detail
5 121 Playing with sets anD Probability What’s a Set 121 Set Construction 122 Subsets Supersets and Power Sets 124 Set Operations 126 Probability 131 Probability of Event A or Event B 133 Probability of Event A and Event B 134 Generating Random Numbers 134 Nonuniform Random Numbers 137 What You Learned 140 Programming Challenges 140 1 Using Venn Diagrams to Visualize Relationships Between Sets 140 2 Law of Large Numbers 143 3 How Many Tosses Before You Run Out of Money 144 4 Shuffling a Deck of Cards 144 5 Estimating the Area of a Circle 145 6 Drawing geoMetric shaPes anD Fractals 149 Drawing Geometric Shapes with Matplotlib’s Patches 150 Drawing a Circle 151 Creating Animated Figures 153 Animating a Projectile’s Trajectory 156 Drawing Fractals 158 Transformations of Points in a Plane 158 Drawing the Barnsley Fern 163 What You Learned 168 Programming Challenges 168 1 Packing Circles into a Square 168 2 Drawing the Sierpin ski Triangle 170 3 Exploring Hénon’s Function 171 4 Drawing the Mandelbrot Set 172 7 177 solVing calculus ProbleMs What Is a Function 178 Domain and Range of a Function 178 An Overview of Common Mathematical Functions 178 Assumptions in SymPy 180 Finding the Limit of Functions 181 Continuous Compound Interest 183 Instantaneous Rate of Change 184 Finding the Derivative of Functions 185 A Derivative Calculator 186 Calculating Partial Derivatives 187 Higher-Order Derivatives and Finding the Maxima and Minima 188 Finding the Global Maximum Using Gradient Ascent 191 A Generic Program for Gradient Ascent 195 A Word of Warning About the Initial Value 196 The Role of the Step Size and Epsilon 197 Contents in Detail xi
Finding the Integrals of Functions 200 Probability Density Functions 201 What You Learned 205 Programming Challenges 205 1 Verify the Continuity of a Function at a Point 205 2 Implement the Gradient Descent 205 3 Area Between Two Curves 206 4 Finding the Length of a Curve 207 aFterworD 209 Things to Explore Next 209 Project Euler 210 Python Documentation 210 Books 210 Getting Help 211 Conclusion 211 a 213 soFtware installation Microsoft Windows 214 Updating SymPy 215 Installing matplotlib-venn 215 Starting the Python Shell 215 Linux 216 Updating SymPy 217 Installing matplotlib-venn 217 Starting the Python Shell 217 Mac OS X 217 Updating SymPy 220 Installing matplotlib-venn 220 Starting the Python Shell 220 b oVerView oF Python toPics 221 if __name__ __main__ 221 List Comprehensions 223 Dictionary Data Structure 224 Multiple Return Values 226 Exception Handling 228 Specifying Multiple Exception Types 228 The else Block 230 Reading Files in Python 230 Reading All the Lines at Once 232 Specifying the Filename as Input 232 Handling Errors When Reading Files 232 Reusing Code 235 inDex xii Contents in Detail 237
a C k n o w l E Dg M E n t S I would like to thank everyone at No Starch Press for making this book possible From the first emails discussing the book idea with Bill Pollock and Tyler Ortman through the rest of the process everyone there has been an absolute pleasure to work with Seph Kramer was amazing with his technical insights and suggestions and Riley Hoffman was meticulous in checking and re-checking that everything was correct It is only fair to say that without these two fine people this book wouldn’t have been close to what it is Thanks to Jeremy Kun and Otis Chodosh for their insights and making sure all the math made sense I would also like to thank the copy editor Julianne Jigour for her thoroughness SymPy forms a core part of many chapters in this book and I would like to thank everyone on the SymPy mailing list for answering my queries patiently and reviewing my patches with promptness I would also like to thank the matplotlib community for answering and clearing up my doubts I would like to thank David Ash for lending me his Macbook which helped me when writing the software installation instructions I also must thank every writer and thinker who inspired me to write from humble web pages to my favorite books