# How to conquer your math class with Python

Tyler (271)
0

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

Posted in these interests:

python
PRIMARY
61 guides
1

## Use the iPython shell instead of a calculator

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.

2

## Use Python's standard libraries and built in functions

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``````

``````# 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``````
``````from fractions import Fraction

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

## Use functions to represent common formulas

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

# 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))``````

``````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.

Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
Tyler
In these interests: codepython
Slack has become one of the most important tools for communication in many companies, mine included.
Tyler
In these interests: slackpython
If you're familiar with Python's keyword-only arguments, then you've probably wondered why the same constraint doesn't exist for positional arguments. This changes with Python 3.
Tyler
In these interests: codepython
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
While this requirement may seem rare, learning how to nest defaultdicts properly can be extremely powerful and save you from bloated and confusing initialization code.
In Python, we often run into scenarios where we need to merge two or more lists. This is also called list concatenation, and Python provides multiple methods for accomplishing this.
Find and fix slow running Python unit tests
In Python, range is an immutable sequence type, meaning it's a class that generates a sequence of numbers that cannot be modified.
Posted in these interests:
python
61 guides 106 subscribers
PRIMARY
Python is howchoo's favorite programming language. We believe python promotes the most organized and performant codebase possible. We also love Django so, naturally, we love Python.
Discuss this guide:
Follow @howchoo and learn cool things!