Python math module


The math module in Python provides a wide range of mathematical functions and constants that are useful for performing complex mathematical operations. It is part of the standard library, which means you can use it without any additional installations.

Importing the Math Module

To use the functions in the math module, you need to import it:

import math

Key Functions and Constants

  1. Mathematical Constants:

    • math.pi: The mathematical constant Ï€ (pi), approximately 3.14159.
    • math.e: The mathematical constant e (Euler's number), approximately 2.71828.
  2. Basic Functions:

    • math.ceil(x): Returns the smallest integer greater than or equal to x.
      print(math.ceil(3.2)) # Output: 4
    • math.floor(x): Returns the largest integer less than or equal to x.
      print(math.floor(3.8)) # Output: 3
  3. Power and Logarithmic 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.
      print(math.pow(2, 3)) # Output: 8.0
    • math.log(x[, base]): Returns the logarithm of x to the specified base (default is e).
      print(math.log(10)) # Output: 2.302585092994046 (natural log) print(math.log(100, 10)) # Output: 2.0 (log base 10)
  4. Trigonometric Functions:

    • math.sin(x): Returns the sine of x (x in radians).
    • math.cos(x): Returns the cosine of x.
    • math.tan(x): Returns the tangent of x.
      angle = math.radians(30) # Convert degrees to radians print(math.sin(angle)) # Output: 0.49999999999999994
  5. Angle Conversion:

    • math.radians(x): Converts degrees to radians.
    • math.degrees(x): Converts radians to degrees.
      print(math.degrees(math.pi)) # Output: 180.0
  6. Factorial and Combinatorial Functions:

    • math.factorial(x): Returns the factorial of x (x must be a non-negative integer).
      print(math.factorial(5)) # Output: 120
    • math.comb(n, k): Returns the number of ways to choose k items from n items without repetition.
    • math.perm(n, k): Returns the number of ways to choose k items from n items with repetition.
  7. Hyperbolic Functions:

    • math.sinh(x): Returns the hyperbolic sine of x.
    • math.cosh(x): Returns the hyperbolic cosine of x.
    • math.tanh(x): Returns the hyperbolic tangent of x.
  8. Other Functions:

    • math.isqrt(x): Returns the integer square root of x.
    • math.gcd(x, y): Returns the greatest common divisor of x and y.
    • math.dist(p, q): Returns the Euclidean distance between points p and q.

Example Usage

Here’s an example demonstrating the use of several functions from the math module:

import math # Constants print(math.pi) # Output: 3.141592653589793 print(math.e) # Output: 2.718281828459045 # Basic calculations print(math.sqrt(16)) # Output: 4.0 print(math.pow(2, 3)) # Output: 8.0 print(math.factorial(5)) # Output: 120 # Trigonometric functions angle_rad = math.radians(45) print(math.sin(angle_rad)) # Output: 0.7071067811865475 print(math.cos(angle_rad)) # Output: 0.7071067811865476 # Logarithmic functions print(math.log(100, 10)) # Output: 2.0

Summary

  • The math module provides a variety of mathematical functions and constants.
  • Useful for performing complex mathematical operations like trigonometry, logarithms, and combinatorial calculations.
  • Functions can be used with integers and floating-point numbers, making it a versatile module for mathematical computations.