DOING MATH WITH PYTHON
Use Programming to Explore Algebra, Statistics, Calculus, and More!
by Amit Saha
San Francisco
DOING MATH WITH PYTHON. Copyright 2015 by Amit Saha.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
Printed in USA
First printing
19 18 17 16 15 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-640-0
ISBN-13: 978-1-59327-640-9
Publisher: William Pollock
Production Editor: Riley Hoffman
Cover Illustration: Josh Ellingson
Interior Design: Octopod Studios
Developmental Editors: Seph Kramer and Tyler Ortman
Technical Reviewer: Jeremy Kun
Copyeditor: Julianne Jigour
Compositor: Riley Hoffman
Proofreader: Paula L. Fleming
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 415.863.9900;
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Saha, Amit, author.
Doing math with Python : use programming to explore algebra, statistics, calculus, and more! / by
Amit Saha.
pages cm
Summary: Uses the Python programming language as a tool to explore high school-level mathematics
like statistics, geometry, probability, and calculus by writing programs to find derivatives, solve
equations graphically, manipulate algebraic expressions, and examine projectile motion. Covers
programming concepts including using functions, handling user input, and reading and manipulating
data-- Provided by publisher.
Includes index.
ISBN 978-1-59327-640-9 -- ISBN 1-59327-640-0
1. Mathematics--Study and teaching--Data processing. 2. Python (Computer program language) 3.
Computer programming. I. Title.
QA20.C65S24 2015
510.2855133--dc23
2015009186
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To Protyusha, for never giving up on me
Contents in Detail
Acknowledgments
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 wouldnt 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 copyeditor, 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 books goal is to bring together three topics near to my heartprogramming, math, and science. What does that mean exactly? Within these pages, well programmatically explore high schoollevel 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, well use our computer to explore them.
Well 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, among other tasks. In other programs, well simulate real-life events, such as projectile motion, a coin toss, or a die roll. Using programs to simulate such events gives us an easy way to analyze and learn more about them.
Youll also find topics that would be extremely difficult to explore without programs. For example, drawing fractals by hand is tedious at best and close to impossible at worst. With a program, all we need to do is run a for loop with the relevant operation in the body of the loop.
I think youll find that this new context for doing math makes learning both programming and math more exciting, fun, and rewarding.
Who Should Read This Book
If you yourself are learning programming, youll appreciate how this book demonstrates ways to solve problems with computers. Likewise, if you teach such learners, I hope you find this book useful to demonstrate the application of programming skills beyond the sometimes abstract world of computer science.
This book assumes the reader knows the absolute basics of Python programming using Python 3specifically, what a function is, function arguments, the concept of a Python class and class objects, and loops. covers some of the other Python topics that are used by the programs, but this book doesnt assume knowledge of these additional topics. If you find yourself needing more background, I recommend reading Python for Kids by Jason Briggs (No Starch Press, 2013).
Whats in This Book?
This book consists of seven chapters and two appendices. Each chapter ends with challenges for the reader. I recommend giving these a try, as theres much to learn from trying to write your own original programs. Some of these challenges will ask you to explore new topics, which is a great way to enhance your learning.
, Working with Numbers, starts off with basic mathematical operations and gradually moves on to topics requiring a higher level of math know-how.
, Visualizing Data with Graphs, discusses creating graphs from data sets using the matplotlib library.