Doing Math with Python

Book description

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.

Publisher resources

View/Submit Errata

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. Brief Contents
  6. Contents in Detail
  7. Acknowledgments
  8. Introduction
    1. Who Should Read This Book
    2. What’s in This Book?
    3. Scripts, Solutions, and Hints
  9. Chapter 1: Working with Numbers
    1. Basic Mathematical Operations
    2. Labels: Attaching Names to Numbers
    3. Different Kinds of Numbers
      1. Working with Fractions
      2. Complex Numbers
    4. Getting User Input
      1. Handling Exceptions and Invalid Input
      2. Fractions and Complex Numbers as Input
    5. Writing Programs That Do the Math for You
      1. Calculating the Factors of an Integer
      2. Generating Multiplication Tables
      3. Converting Units of Measurement
      4. Finding the Roots of a Quadratic Equation
    6. What You Learned
    7. Programming Challenges
      1. #1: Even-Odd Vending Machine
      2. #2: Enhanced Multiplication Table Generator
      3. #3: Enhanced Unit Converter
      4. #4: Fraction Calculator
      5. #5: Give Exit Power to the User
  10. Chapter 2: Visualizing Data with Graphs
    1. Understanding the Cartesian Coordinate Plane
    2. Working with Lists and Tuples
      1. Iterating over a List or Tuple
    3. Creating Graphs with Matplotlib
      1. Marking Points on Your Graph
      2. Graphing the Average Annual Temperature in New York City
      3. Comparing the Monthly Temperature Trends of New York City
      4. Customizing Graphs
        1. Adding a Title and Labels
        2. Customizing the Axes
        3. Plotting Using pyplot
      5. Saving the Plots
    4. Plotting with Formulas
      1. Newton’s Law of Universal Gravitation
      2. Projectile Motion
        1. Generating Equally Spaced Floating Point Numbers
        2. Drawing the Trajectory
        3. Comparing the Trajectory at Different Initial Velocities
    5. What You Learned
    6. Programming Challenges
      1. #1: How Does the Temperature Vary During the Day?
      2. #2: Exploring a Quadratic Function Visually
      3. #3: Enhanced Projectile Trajectory Comparison Program
      4. #4: Visualizing Your Expenses
      5. #5: Exploring the Relationship Between the Fibonacci Sequence and the Golden Ratio
  11. Chapter 3: Describing Data with Statistics
    1. Finding the Mean
    2. Finding the Median
    3. Finding the Mode and Creating a Frequency Table
      1. Finding the Most Common Elements
      2. Finding the Mode
      3. Creating a Frequency Table
    4. Measuring the Dispersion
      1. Finding the Range of a Set of Numbers
      2. Finding the Variance and Standard Deviation
    5. Calculating the Correlation Between Two Data Sets
      1. Calculating the Correlation Coefficient
      2. High School Grades and Performance on College Admission Tests
    6. Scatter Plots
    7. Reading Data from Files
      1. Reading Data from a Text File
      2. Reading Data from a CSV File
    8. What You Learned
    9. Programming Challenges
      1. #1: Better Correlation Coefficient–Finding Program
      2. #2: Statistics Calculator
      3. #3: Experiment with Other CSV Data
      4. #4: Finding the Percentile
      5. #5: Creating a Grouped Frequency Table
  12. Chapter 4: Algebra and Symbolic Math with SymPy
    1. Defining Symbols and Symbolic Operations
    2. Working with Expressions
      1. Factorizing and Expanding Expressions
      2. Pretty Printing
        1. Printing a Series
      3. Substituting in Values
        1. Calculating the Value of a Series
      4. Converting Strings to Mathematical Expressions
        1. Expression Multiplier
    3. Solving Equations
      1. Solving Quadratic Equations
      2. Solving for One Variable in Terms of Others
      3. Solving a System of Linear Equations
    4. Plotting Using SymPy
      1. Plotting Expressions Input by the User
      2. Plotting Multiple Functions
    5. What You Learned
    6. Programming Challenges
      1. #1: Factor Finder
      2. #2: Graphical Equation Solver
      3. #3: Summing a Series
      4. #4: Solving Single-Variable Inequalities
        1. Hints: Handy Functions
  13. Chapter 5: Playing with Sets and Probability
    1. What’s a Set?
      1. Set Construction
        1. Checking Whether a Number Is in a Set
        2. Creating an Empty Set
        3. Creating Sets from Lists or Tuples
        4. Set Repetition and Order
      2. Subsets, Supersets, and Power Sets
      3. Set Operations
        1. Union and Intersection
        2. Cartesian Product
        3. Applying a Formula to Multiple Sets of Variables
        4. Different Gravity, Different Results
    2. Probability
      1. Probability of Event A or Event B
      2. Probability of Event A and Event B
      3. Generating Random Numbers
        1. Simulating a Die Roll
        2. Can You Roll That Score?
        3. Is the Target Score Possible?
      4. Nonuniform Random Numbers
    3. What You Learned
    4. Programming Challenges
      1. #1: Using Venn Diagrams to Visualize Relationships Between Sets
      2. #2: Law of Large Numbers
      3. #3: How Many Tosses Before You Run Out of Money?
      4. #4: Shuffling a Deck of Cards
      5. #5: Estimating the Area of a Circle
        1. Estimating the Value of Pi
  14. Chapter 6: Drawing Geometric Shapes and Fractals
    1. Drawing Geometric Shapes with Matplotlib’s Patches
      1. Drawing a Circle
      2. Creating Animated Figures
      3. Animating a Projectile’s Trajectory
    2. Drawing Fractals
      1. Transformations of Points in a Plane
      2. Drawing the Barnsley Fern
    3. What You Learned
    4. Programming Challenges
      1. #1: Packing Circles into a Square
      2. #2: Drawing the Sierpiński Triangle
      3. #3: Exploring Hénon’s Function
      4. #4: Drawing the Mandelbrot Set
        1. The imshow() Function
        2. Creating a List of Lists
        3. Drawing the Mandelbrot Set
  15. Chapter 7: Solving Calculus Problems
    1. What Is a Function?
      1. Domain and Range of a Function
      2. An Overview of Common Mathematical Functions
    2. Assumptions in SymPy
    3. Finding the Limit of Functions
      1. Continuous Compound Interest
      2. Instantaneous Rate of Change
    4. Finding the Derivative of Functions
      1. A Derivative Calculator
      2. Calculating Partial Derivatives
    5. Higher-Order Derivatives and Finding the Maxima and Minima
    6. Finding the Global Maximum Using Gradient Ascent
      1. A Generic Program for Gradient Ascent
      2. A Word of Warning About the Initial Value
      3. The Role of the Step Size and Epsilon
    7. Finding the Integrals of Functions
    8. Probability Density Functions
    9. What You Learned
    10. Programming Challenges
      1. #1: Verify the Continuity of a Function at a Point
      2. #2: Implement the Gradient Descent
      3. #3: Area Between Two Curves
      4. #4: Finding the Length of a Curve
  16. Afterword
    1. Things to Explore Next
      1. Project Euler
      2. Python Documentation
      3. Books
    2. Getting Help
    3. Conclusion
  17. Appendix A: Software Installation
    1. Microsoft Windows
      1. Updating SymPy
      2. Installing matplotlib-venn
      3. Starting the Python Shell
    2. Linux
      1. Updating SymPy
      2. Installing matplotlib-venn
      3. Starting the Python Shell
    3. Mac OS X
      1. Updating SymPy
      2. Installing matplotlib-venn
      3. Starting the Python Shell
  18. Appendix B: Overview of Python Topics
    1. if __name__ == '__main__'
    2. List Comprehensions
    3. Dictionary Data Structure
    4. Multiple Return Values
    5. Exception Handling
      1. Specifying Multiple Exception Types
      2. The else Block
    6. Reading Files in Python
      1. Reading All the Lines at Once
      2. Specifying the Filename as Input
      3. Handling Errors When Reading Files
    7. Reusing Code
  19. Index
  20. Footnotes
    1. Chapter 3: Describing Data with Statistics
    2. Chapter 6: Drawing Geometric Shapes and Fractals
    3. Chapter 7: Solving Calculus Problems
    4. Afterword
  21. About the Author

Product information

  • Title: Doing Math with Python
  • Author(s): Amit Saha
  • Release date: August 2015
  • Publisher(s): No Starch Press
  • ISBN: 9781593276409