Math Functions in Python

 


Python provides a built-in module called math that includes various mathematical functions and constants to perform mathematical operations. It covers a wide range of functionality such as trigonometry, logarithms, and more.

Importing the math Module

To use the math functions, you need to import the math module.

  • import math


Common Mathematical Functions

Here’s an overview of some common math functions and constants available in the math module:

1. Constants in math Module

  • math.pi: The value of pi (π), approximately 3.14159.

  • math.e: The base of the natural logarithm (e), approximately 2.71828.

  • import math


  • print(math.pi)  # Output: 3.141592653589793

  • print(math.e)   # Output: 2.718281828459045


2. Trigonometric Functions

The math module provides several trigonometric functions for angles expressed in radians:

  • math.sin(x): Returns the sine of x (where x is in radians).

  • math.cos(x): Returns the cosine of x (where x is in radians).

  • math.tan(x): Returns the tangent of x (where x is 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).

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

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

  • import math


  • angle_radians = math.radians(45)  # Convert 45 degrees to radians

  • print(math.sin(angle_radians))  # Output: 0.7071067811865475 (sine of 45 degrees)

  • print(math.degrees(math.pi/2))  # Output: 90.0 (pi/2 radians in degrees)


3. Exponential and Logarithmic Functions

  • math.exp(x): Returns e raised to the power of x (e^x).

  • math.log(x, base): Returns the logarithm of x to the given base. If the base is not 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.exp(2))       # Output: 7.3890560989306495 (e^2)

  • print(math.log(100))     # Output: 4.605170186000111 (natural log of 100)

  • print(math.log10(1000))  # Output: 3.0 (log base 10 of 1000)


4. Power and Root Functions

  • math.pow(x, y): Returns x raised to the power of y (x^y).

  • math.sqrt(x): Returns the square root of x.

  • math.cbrt(x): Returns the cube root of x (available in Python 3.11 and later).

  • import math


  • print(math.pow(2, 3))  # Output: 8.0 (2 raised to the power of 3)

  • print(math.sqrt(16))   # Output: 4.0 (square root of 16)

  • print(math.cbrt(27))   # Output: 3.0 (cube root of 27)


5. Rounding and Precision 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 (removes the fractional part).

  • math.fmod(x, y): Returns the remainder of x divided by y (similar to the modulus operator %).

  • math.isclose(a, b): Checks whether two floating point numbers a and b are close to each other.

  • import math


  • print(math.ceil(2.4))   # Output: 3 (round up)

  • print(math.floor(2.4))  # Output: 2 (round down)

  • print(math.trunc(2.9))  # Output: 2 (removes fractional part)

  • print(math.fmod(5, 2))  # Output: 1.0 (remainder of 5/2)

  • print(math.isclose(0.1 + 0.2, 0.3))  # Output: True (checks if the values are close)


6. Factorial and Combinations

  • math.factorial(x): Returns the factorial of x (x!).

  • math.comb(n, k): Returns the number of ways to choose k items from n items (combinations).

  • math.perm(n, k): Returns the number of ways to arrange k items from n items (permutations).

  • import math


  • print(math.factorial(5))    # Output: 120 (5! = 5*4*3*2*1)

  • print(math.comb(5, 2))      # Output: 10 (combinations of 2 from 5)

  • print(math.perm(5, 2))      # Output: 20 (permutations of 2 from 5)


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.

  • import math


  • print(math.sinh(1))  # Output: 1.1752011936438014

  • print(math.cosh(1))  # Output: 1.5430806348152437

  • print(math.tanh(1))  # Output: 0.7615941559557649


8. Absolute Value

  • math.fabs(x): Returns the absolute value of x (always positive).

  • import math


  • print(math.fabs(-7))  # Output: 7.0

  • print(math.fabs(3))   # Output: 3.0


9. Greatest Common Divisor (GCD)

  • math.gcd(a, b): Returns the greatest common divisor of a and b.

  • import math


  • print(math.gcd(48, 180))  # Output: 12 (the greatest common divisor of 48 and 180)


10. Copysign (Copy Sign from One Number to Another)

  • math.copysign(x, y): Returns a float x with the sign of y.

  • import math


  • print(math.copysign(3, -1))  # Output: -3.0 (sign of -1 copied to 3)

  • print(math.copysign(3, 1))   # Output: 3.0 (sign of 1 copied to 3)


Conclusion:

The math module in Python provides various useful functions and constants for performing mathematical operations, including trigonometric, logarithmic, and arithmetic calculations. It also supports advanced mathematical operations like factorials, combinations, and hyperbolic functions.

Let me know if you'd like to explore any specific math functions further! 😊


Post a Comment

0 Comments