Matplotlib

Top 10 easy python examples for math module

The math module in Python provides a collection of mathematical functions and constants. It offers essential arithmetic operations, trigonometric functions, logarithmic functions, and more. The module is part of Python’s standard library, so you don’t need to install any external packages to use it. Followings are some python examples for math module.

Python Examples of math Module

Importing the Math Module

To use the functions and constants in the math module, you’ll need to import it first.

Python
import math

Constants

The math module defines some useful constants:

  • math.pi: The mathematical constant (\pi) (approximately 3.14159)
  • math.e: Euler’s number (approximately 2.71828)

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 )

Exponential and Logarithmic Functions

  • math.exp(x): Returns ( e^x )
  • math.log(x[, base]): Returns the logarithm of ( x ) to the given base (natural logarithm if no base is provided)
  • math.log10(x): Returns the base-10 logarithm of ( x )
  • math.log2(x): Returns the base-2 logarithm of ( x )

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 arcsine of ( x )
  • math.acos(x): Returns the arccosine of ( x )
  • math.atan(x): Returns the arctangent of ( x )

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 )

Angle Conversion

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

Special Functions

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

Example Usage

1. Calculating the Square Root

Calculate the square root of a number using math.sqrt:

import math

result = math.sqrt(25)
print(result)  # Output: 5.0

2. Rounding Up with ceil

Round a floating-point number up to the nearest integer:

result = math.ceil(4.7)
print(result)  # Output: 5

3. Rounding Down with floor

Round a floating-point number down to the nearest integer:

result = math.floor(4.7)
print(result)  # Output: 4

4. Trigonometric Functions

Calculate the sine of an angle (in radians):

angle = math.radians(30)  # Converting degrees to radians
result = math.sin(angle)
print(result)  # Output: 0.49999999999999994

5. Exponential Function

Calculate the exponential of a number:

result = math.exp(2)
print(result)  # Output: 7.3890560989306495

6. Logarithmic Functions

Calculate the natural logarithm:

result = math.log(7.3890560989306495)
print(result)  # Output: 2.0

7. Factorial

Calculate the factorial of a number:

result = math.factorial(5)
print(result)  # Output: 120

8. Greatest Common Divisor (GCD)

Find the greatest common divisor of two numbers:

result = math.gcd(60, 48)
print(result)  # Output: 12

9. Power Function

Raise a number to a given power:

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

10. Constants

Access mathematical constants like (\pi):

pi_value = math.pi
print(pi_value)  # Output: 3.141592653589793

These examples demonstrate a variety of mathematical operations that can be performed using the math module in Python. From basic arithmetic to more complex trigonometric and logarithmic functions, the math module offers a wide range of capabilities for numerical computations.