How to Conquer Your Math Class Using Python

John John (304)
15 minutes

This guide will teach you how to use your Python programming skills to conquer your math class!

Posted in these interests:
h/python67 guides

A calculator can be faster for simple calculations, but there are a lot of benefits to using iPython. If you don't know what iPython is, check it out here. iPython is an interactive python shell with a lot of rich functionality.

Once installed, you can start the iPython shell by opening your Terminal application and typing:

ipython

One major benefit of using iPython for your calculations is that you can easily see your history. Many math problems have multiple steps and it's helpful to have your previous calculations visible.

Even more valuable is the ability to save your output as a variable. If you're new to programming this is extremely valuable for problems with multiple steps.

rate = 700.0/15000
22000 * rate
-> 1026.6666666666667

Using the Python shell will help you learn the language and programming in general.

Python, like many other languages, comes with great built-in functionality. For example, Python's math module comes with all sorts of helpful functionality like powers and logarithms, trigonometric functions, hyperbolic functions, and more. The best way to learn this and any other module is to read the docs, but here's a small taste of what is available.

import math

math.log(3)
-> 1.0986122886681098

# constants
math.e
-> 2.718281828459045

math.log(math.e)
-> 1.0

Helpful built in functions:

# rounding
round(1.234499, 3)
-> 1.234

# absolute value
abs(-3)
-> 3

# power
pow(3, 2)
-> 9.0

# sum
sum([0, 8, 11, 20, 5])
-> 44

Fractions in Python:

from fractions import Fraction

Fraction(1, 2) + Fraction(3, 5)
-> Fraction(11, 10)

Oftentimes in your math class you'll have to solve the same kind of problem over and over. You can write your own functions in Python to save time.

To solve the quadratic equation (ax**2 + bx + c = 0), you can write a function that represents the quadratic formula.

import math

def quadratic(a, b, c):
    # the discriminant
    d = (b**2) - (4 * a * c)

    # find both solutions
    s1 = (-b + math.sqrt(d)) / (2 * a)
    s2 = (-b - math.sqrt(d)) / (2 * a)

    print("Solution is {0}, {1}".format(s1, s2))

And to use your function:

quadratic(1, 5, 6)
# Solution is -2.0, -3.0

Good programmers are often considered lazy because they like to automate repetitive tasks. If you find yourself wasting time by writing the same formula over and over, it might be time to write a function. If you get really good at this and use it frequently, you might want to learn how to write your own Python module to organize your functions.

Change your Mac's accent color in under a minute!
1 minute

Mac appearances are, lucky for Mac users, highly customizable. Customizing your Mac's appearance can be a fun way to make your Mac fit your style or aesthetic.