Python math Module Drills
A Guide to Important Operations in the Python Math Module
Introduction
Each section contains a "drill" that examines key operations of several common Python math module utilities, preparing you for technical interviews, scientific programming, or reacquainting yourself with Python after a long absence from the language.
Variable and method names are kept short to minimize typing effort, enabling you to finish this exercise in about 15 minutes daily.
What This Article Is Not
This article focuses on learning through practical experience, leaving you to explore the operations in more detail at your own pace. Anybody with a background in applied maths should be able to pick this up quickly.
Setup
Install the latest version of Python here.
Open the Terminal, type “python” and press Enter. Next, import the math library and compute the square root of four to prove your setup is OK:
You’re now ready to start the drill.
Basics
This section doesn’t require the math module but is useful as a warm-up exercise on performing basic mathematics with Python.
5/2 # 2.5
5//2 # 2
5%2 # 1
5 ** 2 # 25
5e3 # 5000.0
int(1.1) # 1
float(1) # 1.0
str(1.1) # '1.1'
round(1.4) # 1.0
round(1.5) # 2.0
round(1.15,1) # 1.1
round(1.16,1) # 1.2
abs(-1) # 1
min(1,2) # 1
max(1,2) # 2
Constants
math.pi # 3.141592653589793
math.e # 2.718281828459045
math.inf # inf
math.nan # nan
From this point on, you must import the Python math module:
import math
Floating point arithmetic
# Ceiling
math.ceil(1.0) # 1
math.ceil(1.1) # 2
math.ceil(-1.0) # -1
math.ceil(-1.1) # -1
math.ceil(-0.9) # 0
# Floor
math.floor(1) # 1
math.floor(1.1) # 1
math.floor(-1) # -1
math.floor(-1.1) # -2
math.floor(-0.9) # -1
# Absolute value
math.fabs(1) # 1.0
math.fabs(-1) # 1.0
# Modulus
math.fmod(1,2) # 1.0 (1 mod 2)
math.fmod(2,2) # 0.0 (2 mod 2)
math.fmod(3,2) # 1.0 (3 mod 2)
x,y = math.modf(2.5)
x # 0.5
y # 2.0
# Remainder
math.remainder(5,2) # 1.0
# Truncation
math.trunc(1.9) # 1
Power, exponential and logarithmic functions
# Roots
math.sqrt(4) # 2.0
math.cbrt(27) # 3.0
# Power
math.pow(2, 3) # 8.0
math.pow(2, -3) # 0.125
# 2 to power
math.exp2(2) # 4.0
math.exp2(-1) # 0.5
# Logarithm to base
math.log(4,2) # 2.0
math.log2(4) # 2.0
math.log10(100) # 2.0
# Exponential
math.exp(1) # 2.718281828459045
Number-theoretic functions
# Factorial
math.factorial(3) # 6
# Integer square root
math.isqrt(4) # 2
math.isqrt(5) # 2
# Greatest common divisor
math.gcd(3,5) # 1
math.gcd(2,4) # 2
# Lowest common multiple
math.lcm(3,5) # 15
math.lcm(4,6) # 12
# Combinations
math.comb(3,1) # 3
math.comb(3,2) # 3
math.comb(3,3) # 1
# Permutations
math.perm(3,3) # 6
math.perm(3,2) # 6
math.perm(3,1) # 3
Summation and product functions
# Euclidian distance between two points
math.dist((3,4),(7,1)) # 5.0
# Sum over iterable
math.fsum([1,2]) # 3.0
# Euclidean norm
math.hypot(*[1,2,3,4]) # 5.477225575051661
# Product of iterable
numbers = [2,3,4]
math.prod(numbers) # 24
math.prod(numbers, start=10) # 240
# Sum of product of values between iterables
math.sumprod([2,3],[4,5]) # 23
Angle conversion
# Angle conversions
math.degrees(math.pi) # 180.0
math.radians(180) # 3.141592653589793 (math.pi)
Trigonometric functions
# Trigonmetric functions
math.sin(math.pi / 2) # 1.0
math.cos(math.pi) # -1.0
math.tan(math.pi / 4) # 0.9999999999999999
# Arc trigonometric functions
math.asin(1.0) # 1.5707963267948966 (math.pi / 2)
math.acos(-1.0) # 3.141592653589793 (math.pi)
math.atan(1.0) # 0.7853981633974483 (math.pi / 4)
math.atan(-1) # -0.7853981633974483 (-math.pi / 4)
math.atan2(-1, -1) # -2.356194490192345 (-3 * math.pi / 4)
Hyperbolic functions
# Hyperpolic functions
math.sinh(1) # 1.1752011936438014
(math.exp(1) - math.exp(-1)) / 2 # 1.1752011936438014
math.cosh(1) # 1.5430806348152437
(math.exp(1) + math.exp(-1)) / 2 # 1.5430806348152437
math.tanh(1) # 0.7615941559557649
math.sinh(1)/math.cosh(1) # 0.7615941559557649
# Arc hyperbolic functions
math.asinh((math.exp(1) - math.exp(-1)) / 2) # 1.0
math.acosh((math.exp(1) + math.exp(-1)) / 2) # 1.0
math.atanh(math.sinh(1)/math.cosh(1)) # 1.0
Thanks for reading! Let me know what you think in the comments section below, and don’t forget to subscribe 👍