Python Math

Python Math Module

The math module in Python provides a variety of mathematical functions and constants that are commonly used for numerical operations. It includes functions for performing basic operations like addition, subtraction, multiplication, and division, as well as more advanced functions such as trigonometry, logarithms, and statistical operations.

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

import math

Common Functions in the math Module

1. Basic Mathematical Operations

  • 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.fabs(x): Returns the absolute value of x.
    print(math.fabs(-5))  # Output: 5.0
    
  • math.ceil(x): Returns the smallest integer greater than or equal to x (rounds up).
    print(math.ceil(4.2))  # Output: 5
    
  • math.floor(x): Returns the largest integer less than or equal to x (rounds down).
    print(math.floor(4.8))  # Output: 4
    

2. Trigonometric Functions

  • math.sin(x): Returns the sine of x (angle in radians).
    print(math.sin(math.pi / 2))  # Output: 1.0
    
  • math.cos(x): Returns the cosine of x (angle in radians).
    print(math.cos(math.pi))  # Output: -1.0
    
  • math.tan(x): Returns the tangent of x (angle in radians).
    print(math.tan(math.pi / 4))  # Output: 1.0
    
  • math.asin(x): Returns the arcsine (inverse sine) of x in radians.
    print(math.asin(1))  # Output: 1.5707963267948966 (π/2)
    
  • math.acos(x): Returns the arccosine (inverse cosine) of x in radians.
    print(math.acos(1))  # Output: 0.0
    
  • math.atan(x): Returns the arctangent (inverse tangent) of x in radians.
    print(math.atan(1))  # Output: 0.7853981633974483 (π/4)
    

3. Logarithmic Functions

  • math.log(x, base): Returns the logarithm of x to the specified base.
    print(math.log(8, 2))  # Output: 3.0 (log base 2 of 8)
    
  • math.log10(x): Returns the base-10 logarithm of x.
    print(math.log10(100))  # Output: 2.0
    
  • math.log2(x): Returns the base-2 logarithm of x.
    print(math.log2(8))  # Output: 3.0
    

4. Constants

The math module includes several mathematical constants:

  • math.pi: The mathematical constant π (3.141592653589793).
    print(math.pi)  # Output: 3.141592653589793
    
  • math.e: The mathematical constant e (2.718281828459045), the base of the natural logarithm.
    print(math.e)  # Output: 2.718281828459045
    
  • math.inf: Positive infinity.
    print(math.inf)  # Output: inf
    
  • math.nan: Represents “Not a Number” (NaN).
    print(math.nan)  # Output: nan
    

5. Factorial and Combinations

  • math.factorial(x): Returns the factorial of x.
    print(math.factorial(5))  # Output: 120
    
  • math.comb(n, k): Returns the number of ways to choose k items from n items (combinations).
    print(math.comb(5, 2))  # Output: 10
    
  • math.perm(n, k): Returns the number of ways to arrange k items from n items (permutations).
    print(math.perm(5, 2))  # Output: 20
    

6. 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
    

Example: Using the math Module in a Program

Here’s an example that uses multiple functions from the math module to perform calculations:

import math

# Calculate the area of a circle
radius = 5
area = math.pi * math.pow(radius, 2)
print(f"Area of the circle: {area:.2f}")

# Calculate the logarithm of a number
number = 1000
log_value = math.log(number, 10)
print(f"Log base 10 of {number}: {log_value:.2f}")

# Calculate the factorial of a number
number = 6
factorial = math.factorial(number)
print(f"Factorial of {number}: {factorial}")

# Calculate the sine of an angle
angle_rad = math.radians(30)  # Convert 30 degrees to radians
sine_value = math.sin(angle_rad)
print(f"Sine of 30 degrees: {sine_value:.2f}")

Conclusion

The math module is a great tool for performing mathematical operations in Python. It provides functions for basic arithmetic, trigonometry, logarithms, and more. You can use constants like math.pi and math.e for mathematical calculations, and you can also work with special functions such as factorials and combinations.

Leave a Reply 0

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