Python provides a rich set of tools for performing mathematical operations. The built-in math module offers a wide range of mathematical functions for tasks like finding square roots, logarithms, trigonometric calculations, and more.
1. What is the Math Module?
The math module in Python provides access to standard mathematical functions defined in C. To use these functions, you must import the math module.
Importing the Math Module:
import math
2. Commonly Used Math Functions
2.1 Basic Math Functions
math.ceil(x): Rounds a number up to the nearest integer.math.floor(x): Rounds a number down to the nearest integer.math.fabs(x): Returns the absolute value ofx.math.factorial(x): Returns the factorial ofx(only for non-negative integers).math.fmod(x, y): Returns the remainder ofxdivided byy.
Examples:
import math
print(math.ceil(4.3)) # Outputs: 5
print(math.floor(4.7)) # Outputs: 4
print(math.fabs(-10)) # Outputs: 10.0
print(math.factorial(5)) # Outputs: 120
print(math.fmod(10, 3)) # Outputs: 1.0
2.2 Power and Logarithmic Functions
math.sqrt(x): Returns the square root ofx.math.pow(x, y): Returnsxraised to the power ofy(same asx ** y).math.log(x): Returns the natural logarithm ofx.math.log10(x): Returns the base-10 logarithm ofx.math.exp(x): Returnseraised to the power ofx.
Examples:
print(math.sqrt(16)) # Outputs: 4.0
print(math.pow(2, 3)) # Outputs: 8.0
print(math.log(2.7183)) # Outputs: ~1.0
print(math.log10(100)) # Outputs: 2.0
print(math.exp(1)) # Outputs: ~2.718
2.3 Trigonometric Functions
Python provides trigonometric functions for angles in radians.
math.sin(x): Sine ofx.math.cos(x): Cosine ofx.math.tan(x): Tangent ofx.math.asin(x),math.acos(x),math.atan(x): Inverse sine, cosine, and tangent.math.degrees(x): Converts radians to degrees.math.radians(x): Converts degrees to radians.
Examples:
print(math.sin(math.radians(30))) # Outputs: 0.5
print(math.cos(math.radians(60))) # Outputs: 0.5
print(math.tan(math.radians(45))) # Outputs: 1.0
print(math.degrees(math.pi)) # Outputs: 180.0
print(math.radians(180)) # Outputs: ~3.14159
2.4 Constants in Math Module
Python’s math module also includes some important constants:
math.pi: Represents the value of π (pi).math.e: Represents the base of the natural logarithm (e).math.tau: Represents 2π (tau).math.inf: Represents infinity.math.nan: Represents Not a Number (NaN).
Examples:
print(math.pi) # Outputs: 3.141592653589793
print(math.e) # Outputs: 2.718281828459045
print(math.tau) # Outputs: 6.283185307179586
3. Advanced Math Functions
3.1 Hyperbolic Functions
math.sinh(x): Hyperbolic sine.math.cosh(x): Hyperbolic cosine.math.tanh(x): Hyperbolic tangent.
Example:
print(math.sinh(1)) # Outputs: 1.1752011936438014
print(math.cosh(1)) # Outputs: 1.5430806348152437
print(math.tanh(1)) # Outputs: 0.7615941559557649
3.2 GCD and LCM
math.gcd(x, y): Returns the greatest common divisor ofxandy.math.lcm(x, y)(Python 3.9+): Returns the least common multiple ofxandy.
Example:
print(math.gcd(12, 18)) # Outputs: 6
print(math.lcm(12, 18)) # Outputs: 36
3.3 Rounding and Precision
math.trunc(x): Truncates the decimal part, returning the integer part ofx.math.isclose(a, b, rel_tol): Checks if two values are close to each other within a relative tolerance.
Example:
print(math.trunc(4.8)) # Outputs: 4
print(math.isclose(0.1 + 0.2, 0.3)) # Outputs: True
3.4 Handling NaN and Infinity
math.isnan(x): Checks ifxis NaN.math.isinf(x): Checks ifxis infinite.
Example:
print(math.isnan(float('nan'))) # Outputs: True
print(math.isinf(float('inf'))) # Outputs: True
4. Real-World Example: Area of a Circle
Using the math module to calculate the area of a circle:
import math
def calculate_circle_area(radius):
return math.pi * math.pow(radius, 2)
radius = 5
area = calculate_circle_area(radius)
print(f"The area of the circle with radius {radius} is {area:.2f}")
# Outputs: The area of the circle with radius 5 is 78.54
5. Summary
- Python’s
mathmodule provides built-in support for mathematical operations and constants. - It includes basic arithmetic, power functions, logarithms, trigonometry, and more.
- Use the
mathmodule for precision and performance in mathematical computations.
The math module is an essential tool for any Python developer working with numerical or scientific problems!
