Python Math Module

The math module in Python provides access to various mathematical functions, constants, and operations. These functions typically operate on floats or integers.

Importing the math Module

import math

Commonly Used Functions in math

  1. Basic Mathematical Functions:
    • math.sqrt(x): Returns the square root of x.
      print(math.sqrt(16))  # Output: 4.0
      
    • math.pow(x, y): Returns x raised to the power of y (x^y).
      print(math.pow(2, 3))  # Output: 8.0
      
    • math.exp(x): Returns e raised to the power of x (e^x).
      print(math.exp(1))  # Output: 2.718281828459045 (value of e)
      
    • math.log(x, base): Returns the logarithm of x to the specified base. If no base is given, it returns the natural logarithm (base e).
      print(math.log(10))         # Output: 2.302585092994046 (natural log)
      print(math.log(100, 10))    # Output: 2.0 (log base 10)
      
    • math.log10(x): Returns the base-10 logarithm of x.
      print(math.log10(1000))  # Output: 3.0
      
    • math.log2(x): Returns the base-2 logarithm of x.
      print(math.log2(8))  # Output: 3.0
      
  2. Trigonometric Functions:
    • math.sin(x): Returns the sine of x radians.
      print(math.sin(math.pi / 2))  # Output: 1.0
      
    • math.cos(x): Returns the cosine of x radians.
      print(math.cos(math.pi))  # Output: -1.0
      
    • math.tan(x): Returns the tangent of x radians.
      print(math.tan(math.pi / 4))  # Output: 1.0
      
    • math.asin(x): Returns the arc sine (inverse sine) of x.
      print(math.asin(1))  # Output: 1.5707963267948966 (pi/2)
      
    • math.acos(x): Returns the arc cosine (inverse cosine) of x.
      print(math.acos(-1))  # Output: 3.141592653589793 (pi)
      
    • math.atan(x): Returns the arc tangent (inverse tangent) of x.
      print(math.atan(1))  # Output: 0.7853981633974483 (pi/4)
      
  3. Hyperbolic Functions:
    • math.sinh(x): Returns the hyperbolic sine of x.
      print(math.sinh(1))  # Output: 1.1752011936438014
      
    • math.cosh(x): Returns the hyperbolic cosine of x.
      print(math.cosh(1))  # Output: 1.5430806348152437
      
    • math.tanh(x): Returns the hyperbolic tangent of x.
      print(math.tanh(1))  # Output: 0.7615941559557649
      
  4. Rounding and Remainder Functions:
    • math.ceil(x): Returns the smallest integer greater than or equal to x.
      print(math.ceil(4.3))  # Output: 5
      
    • math.floor(x): Returns the largest integer less than or equal to x.
      print(math.floor(4.8))  # Output: 4
      
    • math.trunc(x): Truncates the decimal part of x, returning the integer part only.
      print(math.trunc(4.9))  # Output: 4
      
    • math.fmod(x, y): Returns the remainder of x divided by y.
      print(math.fmod(17, 5))  # Output: 2.0
      
  5. Factorial and Combinatorics:
    • math.factorial(x): Returns the factorial of x (x!).
      print(math.factorial(5))  # Output: 120
      
    • math.comb(n, k): Returns the number of ways to choose k items from n without repetition and without order.
      print(math.comb(5, 3))  # Output: 10
      
    • math.perm(n, k): Returns the number of ways to choose k items from n without repetition but with order.
      print(math.perm(5, 3))  # Output: 60
      
  6. Special Functions:
    • math.gcd(a, b): Returns the greatest common divisor of a and b.
      print(math.gcd(8, 12))  # Output: 4
      
    • math.isqrt(x): Returns the integer square root of x.
      print(math.isqrt(16))  # Output: 4
      
    • math.lcm(a, b): Returns the least common multiple of a and b.
      print(math.lcm(8, 12))  # Output: 24
      
  7. Constants:
    • math.pi: The mathematical constant pi (π), approximately 3.14159.
      print(math.pi)  # Output: 3.141592653589793
      
    • math.e: The mathematical constant e, approximately 2.71828.
      print(math.e)  # Output: 2.718281828459045
      
    • math.tau: The constant τ, which is 2π (approximately 6.28318).
      print(math.tau)  # Output: 6.283185307179586
      
    • math.inf: Represents positive infinity.
      print(math.inf)  # Output: inf
      
    • math.nan: Represents “Not a Number” (NaN).
      print(math.nan)  # Output: nan
      

Example Usage:

import math

# Square root
print(math.sqrt(25))  # Output: 5.0

# Trigonometric functions
angle = math.pi / 4
print(math.sin(angle))  # Output: 0.7071067811865475
print(math.cos(angle))  # Output: 0.7071067811865476

# Logarithm
print(math.log(100, 10))  # Output: 2.0

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

# Greatest Common Divisor
print(math.gcd(48, 18))  # Output: 6

The math module is highly useful for various mathematical computations, especially when working with floating-point numbers, logarithms, trigonometric functions, and special functions like factorial and GCD.

Leave a Reply 0

Your email address will not be published. Required fields are marked *