Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More!
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
introDUCtion This book’s goal is to bring together three topics near to my heart programming math and science What does that mean exactly Within these pages we’ll programmati cally explore high school level topics like manipulating units of measurement examining projectile motion calculating mean median and mode determining linear correlation solving algebraic equations describing the motion of a simple pendulum simulating dice games creating geometric shapes and finding the limits derivatives and integrals of functions These are familiar topics for many but instead of using pen and paper we’ll use our computer to explore them We’ll write programs that will take numbers and formulas as input do the tedious calculations needed and then spit out the solution or draw a graph Some of these programs are powerful calculators for solving math problems They find the solutions to equations calculate the correlation between sets of data and determine the maximum value of a function