Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More!
To Protyusha for never giving up on me
B r i E f C o n tE n t S Acknowledgments xiii Introduction xv Chapter 1 Working with Numbers 1 Chapter 2 Visualizing Data with Graphs 27 Chapter 3 Describing Data with Statistics 61 Chapter 4 Algebra and Symbolic Math with SymPy 93 Chapter 5 Playing with Sets and Probability 121 Chapter 6 Drawing Geometric Shapes and Fractals 149 Chapter 7 Solving Calculus Problems 177 Afterword 209 Appendix A Software Installation 213 Appendix B Overview of Python Topics 221 Index 237
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