Basic Python: How to do Math in Python

If you’re doing anything data science or machine learning related in Python, you should know some math. This post is for anyone looking to get into a Python role working with data or machine learning. First, we’ll cover how to use base Python (no imported libraries) to do basic math on floats and integers. Then, we’ll cover how to use some of the functions in the `math` library in Python. This is by no means a comprehensive guide and there will be more advanced guides in the future, so be on the lookout!

Basic Python Math Operators

There are four basic math operations – addition, subtraction, division, and multiplication. We actually went over all of these in our post on How to Build Your Own Simple Calculator. Here we’ll go over what the operators are. The operators are pretty much the same as you’d expect as if you were writing them in WolframAlpha. The add symbol is “+”, the subtract symbol is “-”, the multiply symbol is “*”, and the divide symbol is “/”.

def add(a, b):
    return a + b
 
def subtract(a, b):
    return a - b
 
def multiply(a, b):
    return a * b
 
def divide(a, b):
    return a/b

Operators for Integer Math in Python

Great, those were all the basics, let’s take a look at some less intuitive operators. In traditional math, these are used only on integers. In Python, you can actually use these on floats too (except integer divide) but you may not get an exact answer. The operators we’ll look at in this section are integer divide (“//”), modulo (“%”), and exponent (“**”). The modulo operator gives you the remainder of `a%b`, and the exponent operator, `a**b`, will give you `a` raised to the `b` power.

def integer_divide(a, b):
    return a//b
 
def modulo(a, b):
    return a%b
 
def exponent(a, b):
    return a**b

Python Math Module Functions You Need to Know

These aren’t the only math module functions, but these are pretty much the only ones you need to know for data science and machine learning. To use these functions you’ll need to import the `math` library. We’re going to go over eight functions that you’ll need to know.

  1. Square Root – takes a square root of `a`
  2. Power – raise `a` to the `b` power, it’s the same as `a**b`
  3. Custom Logarithm – allows you to take a logarithm of `a` in base `b`
  4. Exponential – returns `e` to the `a` power. The number `e` is Euler’s Number
  5. Natural Logarithm – the opposite of the exponential, gives the logarithm in base `e`.
  6. Base 10 Logarithm – takes a logarithm of `a` in base 10
  7. Base 2 Logarithm – takes a logarithm of `a` in base 2
  8. Hyperbolic Tangent – this one is specific to ML, as this is the activation function used in Recurrent Neural Networks, an important part of Natural Language Processing.

To learn more about the math module, check out the documentation.

import math
 
def _sqrt(a):
    return math.sqrt(a)
 
def _pow(a, b):
    return math.pow(a, b)
 
def _log(a, b):
    return math.log(a, b)
 
def _exp(a):
    return math.exp(a)
 
def natural_log(a):
    return math.log(a)
 
def _log10(a):
    return math.log10(a)
 
def _log2(a):
    return math.log2(a)
 
def _tanh(a):
    return math.tanh(a)

Further Reading

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

One thought on “Basic Python: How to do Math in Python

Leave a Reply

%d bloggers like this: