Math Functions and Module
6 min ยท The Basics
Built-in Math Functions
Python includes several built-in math functions that are always available without importing any modules. These are convenient for common mathematical operations:
builtin_math.py
# Absolute value (distance from zero)
print(abs(-5)) # 5
print(abs(5)) # 5
print(abs(-3.7)) # 3.7
# Rounding (to nearest integer or specified decimal places)
print(round(3.7)) # 4 (round to nearest integer)
print(round(3.4)) # 3
print(round(3.14159, 2)) # 3.14 (2 decimal places)
print(round(2.675, 2)) # 2.67 (banker's rounding)
# Min and Max (find smallest/largest value)
print(min(1, 5, 3)) # 1
print(max(1, 5, 3)) # 5
print(min([10, 5, 8, 3])) # 3 (works with lists too)
# Power function (exponentiation)
print(pow(2, 3)) # 8 (same as 2 ** 3)
print(pow(5, 2)) # 25
print(pow(16, 0.5)) # 4.0 (square root)
# Sum function
print(sum([1, 2, 3, 4])) # 10
The math Module
For more advanced mathematical operations, Python provides the math module. You need to import it to use its functions:
math_module.py
import math
# Mathematical constants
print(math.pi) # 3.141592653589793 (ฯ)
print(math.e) # 2.718281828459045 (Euler's number)
print(math.tau) # 6.283185... (2ฯ)
# Square root and powers
print(math.sqrt(16)) # 4.0
print(math.sqrt(2)) # 1.4142135623730951
print(math.pow(2, 3)) # 8.0 (same as 2 ** 3)
# Rounding functions
print(math.floor(3.7)) # 3 (round down)
print(math.ceil(3.2)) # 4 (round up)
print(math.trunc(3.7)) # 3 (truncate decimal part)
# Trigonometric functions (angles in radians)
print(math.sin(math.pi/2)) # 1.0 (sin of 90 degrees)
print(math.cos(0)) # 1.0 (cos of 0 degrees)
print(math.tan(math.pi/4)) # ~1.0 (tan of 45 degrees)
# Logarithmic functions
print(math.log(100, 10)) # 2.0 (log base 10 of 100)
print(math.log(8, 2)) # 3.0 (log base 2 of 8)
print(math.log(100)) # Natural log (base e)
# Other useful functions
print(math.fabs(-5)) # 5.0 (absolute value, always float)
print(math.factorial(5)) # 120 (5! = 5*4*3*2*1)
print(math.gcd(48, 18)) # 6 (greatest common divisor)
Common Math Module Functions
Here's a comprehensive list of commonly used math functions:
math.sqrt(x)โ Square root of xmath.pow(x, y)โ x raised to the power of ymath.floor(x)โ Largest integer โค x (round down)math.ceil(x)โ Smallest integer โฅ x (round up)math.fabs(x)โ Absolute value (always returns float)math.factorial(x)โ Factorial of x (x!)math.gcd(a, b)โ Greatest common divisormath.sin(x),math.cos(x),math.tan(x)โ Trigonometric functionsmath.log(x, base)โ Logarithm with specified basemath.piโ Mathematical constant ฯmath.eโ Mathematical constant e
Practical Examples
Here are real-world examples of using math functions:
practical_math.py
import math
# Calculate area of a circle
radius = 5
area = math.pi * radius ** 2
print(f"Circle area: {area:.2f}")
# Calculate distance between two points
x1, y1 = 0, 0
x2, y2 = 3, 4
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
print(f"Distance: {distance}") # 5.0 (3-4-5 triangle)
# Rounding prices
price = 19.998
rounded_price = round(price, 2)
print(f"Price: ${rounded_price}")
# Finding maximum in a dataset
scores = [85, 92, 78, 96, 88]
highest = max(scores)
lowest = min(scores)
print(f"Highest: {highest}, Lowest: {lowest}")
# Converting degrees to radians for trig functions
degrees = 45
radians = math.radians(degrees) # Convert to radians
print(f"sin({degrees}ยฐ) = {math.sin(radians):.2f}")
Built-in vs math Module
๐ When to Use What
Built-in functions (abs, round, min, max, sum): Use for basic operations that don't require importing
math module: Use for advanced mathematical operations like square roots, trigonometry, logarithms, and special constants
main.py
๐ค Output
Click "Run" to execute...