Doing Math with Python

Doing Math with Python

Use Programming to Explore Algebra, Statistics, Calculus, and More!
by Amit Saha
August 2015, 264 pp.
ISBN-13: 
978-1-59327-640-9

“Saha does an excellent job providing a clear link between Python and upper-level math concepts, and demonstrates how Python can be transformed into a mathematical stage. This book deserves a spot on every geometry teacher’s bookshelf.”
School Library Journal

Doing Math with Python shows you how to use Python to delve into high school–level math topics like statistics, geometry, probability, and calculus. You’ll start with simple projects, like a factoring program and a quadratic-equation solver, and then create more complex projects once you’ve gotten the hang of things.

Along the way, you’ll discover new ways to explore math and gain valuable programming skills that you’ll use throughout your study of math and computer science. Learn how to:

  • Describe your data with statistics, and visualize it with line graphs, bar charts, and scatter plots
  • Explore set theory and probability with programs for coin flips, dicing, and other games of chance
  • Solve algebra problems using Python’s symbolic math functions
  • Draw geometric shapes and explore fractals like the Barnsley fern, the Sierpinski triangle, and the Mandelbrot set
  • Write programs to find derivatives and integrate functions

Creative coding challenges and applied examples help you see how you can put your new math and coding skills into practice. You’ll write an inequality solver, plot gravity’s effect on how far a bullet will travel, shuffle a deck of cards, estimate the area of a circle by throwing 100,000 “darts” at a board, explore the relationship between the Fibonacci sequence and the golden ratio, and more.

Whether you’re interested in math but have yet to dip into programming or you’re a teacher looking to bring programming into the classroom, you’ll find that Python makes programming easy and practical. Let Python handle the grunt work while you focus on the math.

Uses Python 3

Author Bio 

Amit Saha is a software engineer who has worked for Red Hat and Sun Microsystems. He created and maintains Fedora Scientific, a Linux distribution for scientific and educational users. He is also the author of Write Your First Program (Prentice Hall Learning).

Table of contents 

Introduction

Chapter 1: Working with Numbers
Chapter 2: Visualizing Data with Graphs
Chapter 3: Describing Data with Statistics
Chapter 4: Algebra and Symbolic Math with SymPy
Chapter 5: Playing with Sets and Probability
Chapter 6: Drawing Geometric Shapes and Fractals
Chapter 7: Solving Calculus Problems

Afterword

Appendix A: Software Installation
Appendix B: Overview of Python Topics

View the detailed Table of Contents (PDF)
View the Index (PDF)

Reviews 

Amit Saha was interviewed by Mouse vs. Python as the PyDev of the Week!

"This is a great book — the level of the material is carefully judged throughout, the mathematics and the computing complement and motivate each other, and the exercises are superb."
Dr. Ian Hawke, University of Southampton

"Will show you how to reliably express mathematical ideas and operations in Python code...the grounding this gives you will make your Python code more effective, reliable and meaningful."
Network Security Newsletter

"A good new resource for budding data scientists...contains all the important first ingredients that a newbie data scientist needs to get started in the field."
insideBIGDATA

"Doing Math with Python helps students learn how to do math with the help of a little programming. It’s like learning two subjects at once."
GeekMom

"Doing Math with Python is an amazing book. I wish I could have encountered this book earlier in my own education as a way to better solidify the math I was learning. I'm working on incorporating aspects into my regular classroom because I and my MESA students have been enjoying it so much. If you are even considering getting this book, you should. And if you aren't considering it I think you should change your mind."
Neal Whitlock, high school teacher

"Great for gaining a very basic understanding of Python and quickly turning that into something with real-world application. Along the way, you’ll deepen your skills in the language."
Game Vortex

Updates 

Page 8: In the final paragraph:
...they take a string as input ('1') and return a number (2 or 2.0)
Should instead read:
...they take a string as input ('1') and return a number (1 or 1.0)

Page 47: At the end of the first paragraph:
The x-axis of the graph displays the force, and the y-axis displays the distance
Should instead read:
The x-axis of the graph displays the distance, and the y-axis displays the force

Page 48: In the caption for Figure 2-12:
Visualization of the relationship between the gravitational force and the squared distance
Should instead read:
Visualization of the relationship between the gravitational force and the distance

Page 52: In the first paragraph:
At 2, we calculate the time of flight and then call the frange() function with the values for start, final, and increment set to 0, t_flight, and 0.001, respectively
Should instead read:
At 2, we calculate the time of flight and then call the frange() function with the values for start, final, and interval set to 0, t_flight, and 0.001, respectively

Page 55: In Challenge #2, second paragraph:
...and uses the label y to refer to the list of results
Should instead read:
...and uses the label y to refer to the result

Page 76: In the text box:
...and crime are correlated because they both go up
Should instead read:
...and crime rate are correlated because they both go up

Page 77: In the final block of code, the line for yi in y should be left-aligned with the line y_square=[ ] above

Page 87: Between the fourth and fifth paragraphs, add the sentence:
If you are unable to access the service, download a copy of the file from https://github.com/doingmathwithpython/code/blob/master/chapter3/solutio....

Page 97:
In the section "Factorizing and Expanding Expressions," the expand() function must be imported before it can be used:

from sympy import expand

Page 99: In the code snippet, line (2) should be left-aligned under line (1)

Page 112: In the second code listing:
from sympy import Symbol, sympify, solve
Should instead read:
from sympy import Symbol, sympify, solve, SympifyError

Page 115: In the first paragraph:
(using the first letter of the color in each case)
Should instead read:
(using the first letter of the color in each case, except black for which you use 'k')

Page 132: Add the following line at the top of the second listing:
from sympy import FiniteSet

Page 132: Remove one level of indentation from the entire second code block (except for the first line, from sympy import FiniteSet

Page 132:
In the section "Probability," the code line above the one marked with a (3), which reads:

for num in s:

Should instead read:

for num in space:

Page 135: In the third code block, delete the line:
import matplotlib.pyplot as plt

Page 139:
In the "Simulate a fictional ATM" program listing, the statement that reads:

probability = [1/6, 1/6, 1/3, 2/3]

Should instead read:

probability = [1/6, 1/6, 1/3, 1/3]

Pages 164-165: Update the equations as follows:
Transformation 1
x1 = 0.85x + 0.04y
y1 = -0.04x + 0.85y + 1.6

Transformation 2
x1 = 0.2x - 0.26y
y1 = 0.23x + 0.22y + 1.6
Transformation 3
x1 = -0.15x + 0.28y
y1 = 0.26x + 0.24y + 0.44

Transformation 4
x1 = 0
y1 = 0.16y

Page 194: At the bottom of the second paragraph, the formula:
abs(x_old – x_new) > epsilon
Should instead read:
abs(x_old – x_new) <= epsilon

Page 195: In the code listing:
from sympy import Derivative, Symbol, sympify
Should instead read:
from sympy import Derivative, Symbol, sympify, SympifyError

Page 213:
The link in Appendix A to download Anaconda is no longer active. The correct link is https://www.anaconda.com/distribution/