September 11, 2024

Python Math Module

The math module in Python provides a wide range of mathematical functions and constants, which can be used to perform mathematical operations beyond the basic arithmetic operations. This module is part of the standard Python library, so no additional installation is required.

1. Importing the math Module

To use the functions and constants provided by the math module, you first need to import it:

import math

2. Mathematical Constants

The math module provides several mathematical constants:

  • math.pi: The value of π (pi), approximately 3.14159.
  • math.e: The value of e (Euler’s number), approximately 2.71828.
  • math.tau: The value of τ (tau), approximately 6.28318 (equal to 2π).
  • math.inf: Represents positive infinity.
  • math.nan: Represents “Not a Number” (NaN).
import math

print(math.pi)   # Output: 3.141592653589793
print(math.e)    # Output: 2.718281828459045
print(math.tau)  # Output: 6.283185307179586
print(math.inf)  # Output: inf
print(math.nan)  # Output: nan

3. Basic Mathematical Functions

The math module includes a variety of basic mathematical functions:

3.1. Absolute Value

math.fabs(x) returns the absolute value of x.

import math

print(math.fabs(-10))  # Output: 10.0

3.2. Power and Exponentiation

  • math.pow(x, y): Returns x raised to the power of y (equivalent to x**y).
  • math.sqrt(x): Returns the square root of x.
  • math.exp(x): Returns e raised to the power of x.
import math

print(math.pow(2, 3))   # Output: 8.0
print(math.sqrt(16))    # Output: 4.0
print(math.exp(1))      # Output: 2.718281828459045 (which is e)

3.3. Logarithmic Functions

  • math.log(x, base): Returns the logarithm of x to the specified base. If no base is specified, it returns the natural logarithm (base e).
  • math.log10(x): Returns the base-10 logarithm of x.
  • math.log2(x): Returns the base-2 logarithm of x.
import math

print(math.log(100))       # Output: 4.605170185988092 (natural log)
print(math.log(100, 10))   # Output: 2.0
print(math.log10(100))     # Output: 2.0
print(math.log2(8))        # Output: 3.0

3.4. Trigonometric Functions

  • math.sin(x): Returns the sine of x (in radians).
  • math.cos(x): Returns the cosine of x (in radians).
  • math.tan(x): Returns the tangent of x (in radians).
  • math.asin(x): Returns the arc sine of x (in radians).
  • math.acos(x): Returns the arc cosine of x (in radians).
  • math.atan(x): Returns the arc tangent of x (in radians).
import math

print(math.sin(math.pi/2))  # Output: 1.0
print(math.cos(0))          # Output: 1.0
print(math.tan(math.pi/4))  # Output: 1.0

3.5. Angular Conversion

  • math.degrees(x): Converts angle x from radians to degrees.
  • math.radians(x): Converts angle x from degrees to radians.
import math

print(math.degrees(math.pi))  # Output: 180.0
print(math.radians(180))      # Output: 3.141592653589793

4. Special Functions

The math module also includes several special functions:

4.1. Factorial

math.factorial(x) returns the factorial of x, which is the product of all positive integers less than or equal to x.

import math

print(math.factorial(5))  # Output: 120

4.2. GCD and LCM

  • math.gcd(a, b): Returns the greatest common divisor (GCD) of a and b.
  • math.lcm(a, b): Returns the least common multiple (LCM) of a and b. (Available in Python 3.9 and later.)
import math

print(math.gcd(48, 180))  # Output: 12
print(math.lcm(12, 15))   # Output: 60  (Available in Python 3.9+)

4.3. Combination and Permutation

  • math.comb(n, k): Returns the number of ways to choose k items from n items without repetition (combinations). (Available in Python 3.8 and later.)
  • math.perm(n, k): Returns the number of ways to arrange k items from n items (permutations). (Available in Python 3.8 and later.)
import math

print(math.comb(5, 2))  # Output: 10  (Available in Python 3.8+)
print(math.perm(5, 2))  # Output: 20  (Available in Python 3.8+)

5. Rounding Functions

  • math.ceil(x): Returns the smallest integer greater than or equal to x.
  • math.floor(x): Returns the largest integer less than or equal to x.
  • math.trunc(x): Returns the integer part of x (truncates the decimal part).
import math

print(math.ceil(4.2))   # Output: 5
print(math.floor(4.8))  # Output: 4
print(math.trunc(4.7))  # Output: 4

The math module in Python is a powerful tool that provides a wide range of mathematical functions and constants. Whether you need to perform basic arithmetic, work with trigonometric functions, or handle complex mathematical operations, the math module has you covered. By mastering these functions, you can perform mathematical operations more efficiently and accurately in your Python programs.