Python Mathematical Funcations - Math Modules

PYTHON MATH MODULE:

The Python 'math' module is a built-in library offering various mathematical functions and constants. It assists in mathematical calculations and operations in Python programs. 

reload ! diplomawaale.blogspot.com
Math Modules



The module encompasses functions for basic arithmetic, trigonometry, logarithms, and more. It also provides mathematical constants like π (pi) and e (Euler's number). By importing and utilizing the math module, programmers can access a comprehensive set of tools to execute advanced mathematical tasks within their Python code. This module proves invaluable in scenarios where precise mathematical calculations are required, enhancing the capabilities of Python as a versatile programming language.

EXAMPLE:

                       import math

                        # Calculate the square root
                        number = 25
                        square_root = math.sqrt(number)
                        print(f"Square root of {number} is {square_root}")

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

                       # Calculate the logarithm base 2
                       value = 8
                       log_base_2 = math.log2(value)
                       print(f"Log base 2 of {value} is {log_base_2}")

                      # Calculate the value of Ï€ (pi)
                      pi_value = math.pi
                      print(f"Value of Ï€: {pi_value}")

                      # Calculate the factorial of a number
                      factorial_number = 5
                      factorial = math.factorial(factorial_number)
                      print(f"Factorial of {factorial_number} is {factorial}")

MATH MODULE WITH DIR FUNCTION:

Certainly! Here's how you can use the dir() function to list the names of all the functions, classes, and variables available in the Python math module:

EXAMPLE:

                                  import math
                                   # Get the list of names in the math module
                                   math_module_names = dir(math)
                                   # Print the list of names
                                   for name in math_module_names:
                                   print(name)

OUTPUT:


['__doc__',

 '__loader__',

 '__name__',

 '__package__',

 '__spec__',

 'acos',

 'acosh',

 'asin',

 'asinh',

 'atan',

 'atan2',

 'atanh',

 'ceil',

 'comb',

 'copysign',

 'cos',

 'cosh',

 'degrees',

 'dist',

 'e',

 'erf',

 'erfc',

 'exp',

 'expm1',

 'fabs',

 'factorial',

 'floor',

 'fmod',

 'frexp',

 'fsum',

 'gamma',

 'gcd',

 'hypot',

 'inf',

 'isclose',

 'isfinite',

 'isinf',

 'isnan',

 'isqrt',

 'ldexp',

 'lgamma',

 'log',

 'log10',

 'log1p',

 'log2',

 'modf',

 'nan',

 'perm',

 'pi',

 'pow',

 'prod',

 'radians',

 'remainder',

 'sin',

 'sinh',

 'sqrt',

 'tan',

 'tanh',

 'tau',

 'trunc']


DESCRIPTION OF ALL THE FUNCTION PRESENT IN MATH MODULE: 


Function

Description

ceil(x)

The lowest integer bigger than or equal to x is returned.

copysign(x, y)

gives x back with the sign of y.

fabs(x)

gives x's absolute value back.

factorial(x)

provides the x factorial back.

floor(x)

gives back the biggest integer that is less than or equal to x.

fmod(x, y)

returns the leftover value after dividing x by y.

frexp(x)

returns the pair of the mantissa and exponent of x. (m, e)

fsum(iterable)

returns the iterable's correct floating point sum of all values.

isfinite(x)

If x is neither an infinity nor a NaN, it returns True (Not a Number)

isinf(x)

If x is a positive or negative infinity, it returns True.

isnan(x)

If x is a NaN, it returns True.

ldexp(x, i)

gives back x * (2**i).

modf(x)

gives x's fractional and integer components back.

trunc(x)

x's shortened integer value is returned.

exp(x)

delivers e**x

expm1(x)

yields e**x - 1

log(x[, b])

gives back the x logarithm in base b. (defaults to e)

log1p(x)

the natural logarithm of 1 + x is returned.

log2(x)

gives x's base-2 logarithm back.

log10(x)

provides x's base-10 logarithm.

pow(x, y)

gives x raised to the power of y back.

sqrt(x)

gives x's square root back.

acos(x)

gives the arc cosine of x back.

asin(x)

gives the arc sine of x back.

atan(x)

gives the arc tangent of x back.

atan2(y, x)

gives back atan(y / x).

cos(x)

returns the x's cosine.

hypot(x, y)

returns sqrt(x*x + y*y), the Euclidean norm.

sin(x)

gives the sine of x back.

tan(x)

gives the tangent of x back.

degrees(x)

Angle x is transformed from radians to degrees.

radians(x)

Angle x is transformed from degrees to radians.

acosh(x)

x's inverse hyperbolic cosine is returned.

asinh(x)

x's inverse hyperbolic sine is returned.

atanh(x)

x's inverse hyperbolic tangent is returned.

cosh(x)

gives x's hyperbolic cosine.

sinh(x)

gives x's hyperbolic cosine.

tanh(x)

gives x's hyperbolic tangent back.

erf(x)

the error function at x is returned.

erfc(x)

a function that gives the complementary error at x

gamma(x)

the Gamma function at x is returned.

lgamma(x)

gives the natural logarithm of the gamma function's absolute value at x.

pi

The ratio of a circle's circumference to its diameter is a mathematical constant (3.14159...)

e

e is a constant in mathematics (2.71828...)

EXAMPLE:

import math
# Trigonometric functions
angle_radians = math.radians(45)
sin_value = math.sin(angle_radians)
cos_value = math.cos(angle_radians)
tan_value = math.tan(angle_radians)

# Exponential and logarithmic functions
exp_value = math.exp(2)
log_value = math.log(10)
log10_value = math.log10(100)
log2_value = math.log2(16)

# Power and rounding functions
power_value = math.pow(3, 4)
sqrt_value = math.sqrt(25)
ceil_value = math.ceil(4.3)
floor_value = math.floor(7.8)

# Mathematical constants
pi = math.pi
euler = math.e

# Factorial and absolute value
factorial_value = math.factorial(5)
absolute_value = math.fabs(-10)

# Print the results
print("Trigonometric functions:")
print("Sine:", sin_value)
print("Cosine:", cos_value)
print("Tangent:", tan_value)

print("\nExponential and logarithmic functions:")
print("Exponential:", exp_value)
print("Natural Logarithm:", log_value)
print("Base 10 Logarithm:", log10_value)
print("Base 2 Logarithm:", log2_value)

print("\nPower and rounding functions:")
print("Power:", power_value)
print("Square Root:", sqrt_value)
print("Ceiling:", ceil_value)
print("Floor:", floor_value)

print("\nMathematical constants:")
print("Ï€ (pi):", pi)
print("e (Euler's number):", euler)

print("\nFactorial and absolute value:")
print("Factorial:", factorial_value)
print("Absolute Value:", absolute_value)


Post a Comment

0 Comments