Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More!
C o n t E n t S i n D E ta i l acknowleDgMents xiii introDuction xv Who Should Read This Book xvi What’s in This Book xvi Scripts Solutions and Hints xvii 1 working with nuMbers 1 Basic Mathematical Operations 1 Labels Attaching Names to Numbers 4 Different Kinds of Numbers 4 Working with Fractions 5 Complex Numbers 6 Getting User Input 8 Handling Exceptions and Invalid Input 9 Fractions and Complex Numbers as Input 11 Writing Programs That Do the Math for You 12 Calculating the Factors of an Integer 12 Generating Multiplication Tables 15 Converting Units of Measurement 17 Finding the Roots of a Quadratic Equation 20 What You Learned 22 Programming Challenges 22 1 Even-Odd Vending Machine 22 2 Enhanced Multiplication Table Generator 23 3 Enhanced Unit Converter 23 4 Fraction Calculator 23 5 Give Exit Power to the User 24 2 Visualizing Data with graPhs 27 Understanding the Cartesian Coordinate Plane 28 Working with Lists and Tuples 29 Iterating over a List or Tuple 31 Creating Graphs with Matplotlib 32 Marking Points on Your Graph 33 Graphing the Average Annual Temperature in New York City 35 Comparing the Monthly Temperature Trends of New York City 38 Customizing Graphs 41 Saving the Plots 45 Plotting with Formulas 46 Newton’s Law of Universal Gravitation 46 Projectile Motion 48 What You Learned 54 Programming Challenges 55 1 How Does the Temperature Vary During the Day 55 2 Exploring a Quadratic Function Visually 55
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