Python Introduction to Math Library

Sarper Makas
ILLUMINATION
Published in
3 min readApr 4, 2023
Python Math Library Blog Post Cover, Image created by the author.

The math library in Python is a built-in module that offers a collection of functions for performing various mathematical operations. The library includes functions for fundamental arithmetic as well as advanced mathematical operations such as trigonometry, logarithms, and complex number operations.

Python’s math library makes it simple to conduct complex mathematical operations without requiring complex mathematical calculations. It is an effective tool for scientific and engineering applications, as well as for problem solving in finance, data science, and other disciplines.

In this blog article, we will look at the functions and constants that you can use in your projects.

pi

Variable for the π number. math.pi stands for 3.141592653589793

e

Variable for the e number. math.e stands for 2.718281828459045

inf

The math.inf constant gives a positive infinity in floating-point terms.

Use -math.inf for negative infinity.

The inf number is the same as float("inf").

import math

# positive infinity
print(math.inf)

# negative infinity

print(-math.inf)
print(math.inf == float("inf"))

nan

The math.inf constant gives a positive infinity in floating-point terms.

Use -math.inf for negative infinity.

The inf number is the same as float(‘inf’).

floor(x)

Return the floor of x as an Integral. this is the largest integer ≤ x

import math

# Round down 3.14 to the nearest integer
result = math.floor(3.14)

print(result) # Output: 3

ceil(x)

Return the ceiling of x as an Integral. this is the samllest integer ≥ x

import math

# Round up 3.14 to the nearest integer
result = math.ceil(3.14)

print(result) # Output: 4

pow(x, y)

Return x**y (x to the power of y)

import math

# Raise 2 to the power of 3
result = math.pow(2, 3)

print(result) # Output: 8.0
import math

# Calculate 2 to the power of 3, modulo 5
result = math.pow(2, 3, 5)

print(result) # Output: 3.0 (i.e., 2**3 % 5 == 3)

sqrt(x)

Returns the square root of x

import math

# Calculate the square root of 9
result = math.sqrt(9)

print(result) # Output: 3.0

exp(x)

Returns the exponential value of x.

import math

# Calculate the exponential value of 2
result = math.exp(2)

print(result) # Output: 7.3890560989306495

log(x, base)

Returns the natural logarithm of x, or the logarithm of x to the specified base if provided.

import math

# Calculate the natural logarithm of 10
result = math.log(10)

print(result) # Output: 2.302585092994046

# Calculate the logarithm of 100 to base 10
result = math.log(100, 10)

print(result) # Output: 2.0

sin(x)

Returns the sine of x (in radians)

import math

# Calculate the sine of 30 degrees (converted to radians)
result = math.sin(math.radians(30))

print(result) # Output: 0.49999999999999994

cos(x)

Returns the cosine of x (in radians).

import math

# Calculate the cosine of 60 degrees (converted to radians)
result = math.cos(math.radians(60))

print(result) # Output: 0.5000000000000001

tan(x)

Returns the tangent of x (in radians).

import math

# Calculate the tangent of 45 degrees (converted to radians)
result = math.tan(math.radians(45))

print(result) # Output: 0.9999999999999999

asin(x)

Returns the arc sine of x (in radians).

import math

# Calculate the arc sine of 0.5
result = math.asin(0.5)

print(result) # Output: 0.5235987755982989

acos(x)

Returns the arc cosine of x (in radians).

import math

# Calculate the arc cosine of 0.5
result = math.acos(0.5)

print(result) # Output: 1.0471975511965979

atan(x)

Returns the arc tangent of x (in radians).

import math

# Calculate the arc tangent of 1
result = math.atan(1)

print(result) # Output: 0.7853981633974483

degrees(x)

Converts angle x from radians to degrees.

import math

# Convert 1 radian to degrees
result = math.degrees(1)

print(result) # Output: 57.29577951308232

radians(x)

Converts angle x form degrees to radians.

import math

# Convert 1 radian to degrees
result = math.radians(180)

print(result) # Output: 3.141592653589793

--

--