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

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

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

Leave a Reply

%d bloggers like this: