Python math function


In Python, a math function is a function provided by the math module that allows you to perform mathematical operations such as trigonometry, logarithms, exponentiation, and more.

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

Importing the math module

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

import math

Common Math Functions

Square root: math.sqrt(x) returns the square root of x.

import math
print(math.sqrt(16))  # Output: 4.0

Power: math.pow(x, y) returns x raised to the power of y.

import math
print(math.pow(2, 3))  # Output: 8.0

1.Basic Arithmetic 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.fabs(x): Returns the absolute value of x.
  • math.factorial(x): Returns the factorial of x, where x is a non-negative integer.
import math

print(math.ceil(4.2))    # Output: 5
print(math.floor(4.8))   # Output: 4
print(math.fabs(-5.3))   # Output: 5.3
print(math.factorial(5)) # Output: 120

2. Power and Logarithmic Functions

  • math.exp(x): Returns e raised to the power of x.
  • math.log(x[, base]): Returns the logarithm of x to the given base. If no base is specified, returns the natural logarithm (base e).
  • math.log10(x): Returns the base-10 logarithm of x.
  • math.pow(x, y): Returns x raised to the power of y.
import math
print(math.exp(1))       # Output: 2.718281828459045
print(math.log(10))      # Output: 2.302585092994046 (natural logarithm)
print(math.log(100, 10)) # Output: 2.0 (base-10 logarithm)
print(math.log10(100))   # Output: 2.0
print(math.pow(2, 3))    # Output: 8.0

3.Trigonometric Functions

  • math.sin(x): Returns the sine of x radians.
  • math.cos(x): Returns the cosine of x radians.
  • math.tan(x): Returns the tangent of x 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.atan2(y, x): Returns atan(y / x), in radians. The result is between -pi and pi.
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: 0.9999999999999999 (which is close to 1.0)
print(math.asin(1))           # Output: 1.5707963267948966 (which is π/2)
print(math.acos(1))           # Output: 0.0
print(math.atan(1))           # Output: 0.7853981633974483 (which is π/4)
print(math.atan2(1, 1))       # Output: 0.7853981633974483 (which is π/4)

4.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(0))   # Output: 0.0
print(math.cosh(0))   # Output: 1.0
print(math.tanh(0))   # Output: 0.0

Constants

The math module also provides some useful mathematical constants:

  • math.pi: The value of π (pi), approximately 3.14159.
  • math.e: The value of e (Euler's number), approximately 2.71828.
import math
print(math.pi)  # Output: 3.141592653589793
print(math.e)   # Output: 2.718281828459045

How to use 

Here is an example that uses a combination of these functions to solve a practical problem.

Suppose we want to calculate the length of the hypotenuse of a right-angled triangle given the lengths of the other two sides using the Pythagorean theorem:

import math

# Lengths of the other two sides
a = 3
b = 4

# Calculating the hypotenuse
c = math.sqrt(math.pow(a, b) + math.pow(b, b))

print(f"The length of the hypotenuse is {c}")  # Output: 18.35

This is just a brief overview. The math module includes many more functions, such as those for handling special functions, angle conversion, and complex numbers.

For a comprehensive list, you can refer to the official Python documentation for the math module.