import math
The math module in Python provides access to the mathematical functions defined by the C standard. It allows you to perform various mathematical operations, including trigonometric functions, logarithmic functions, and constants like pi and e.
Some key features of the math module include:
Trigonometric Functions: The
mathmodule provides functions for calculating trigonometric values, such as sine (sin()), cosine (cos()), and tangent (tan()). These functions take an angle in radians as input and return the corresponding trigonometric value.Logarithmic Functions: The
mathmodule offers functions for calculating logarithms, such as the natural logarithm (log()) and the base-10 logarithm (log10()). These functions take a positive number as input and return the logarithm of that number.Exponential Functions: The
mathmodule includes functions for calculating exponential values, such as the exponential function (exp()) and the power function (pow()). The exponential function calculates e raised to a given power, while the power function calculates a number raised to a specified power.Constants: The
mathmodule provides access to mathematical constants, such as pi (math.pi) and e (math.e). These constants are useful for calculations involving circles, exponential growth, and more.
Here’s a simple example of how to use the math module:
import math
# Calculate the sine of 30 degrees
angle_in_radians = math.pi / 6 # 30 degrees in radians
sine_value = math.sin(angle_in_radians)
print(f"The sine of 30 degrees is: {sine_value}")
# Calculate the natural logarithm of 10
log_value = math.log(10)
print(f"The natural logarithm of 10 is: {log_value}")
# Calculate 2 raised to the power of 8
power_value = math.pow(2, 8)
print(f"2 raised to the power of 8 is: {power_value}")
In this example, we first import the math module. We then calculate the sine of 30 degrees by converting the angle from degrees to radians using the math.pi constant. We use the math.sin() function to calculate the sine value.
Next, we calculate the natural logarithm of 10 using the math.log() function.
Finally, we calculate 2 raised to the power of 8 using the math.pow() function.
The math module provides a wide range of functions for performing mathematical operations in Python. It is a useful tool for scientific computing, data analysis, and various other applications that require mathematical calculations.
math.pi: The mathematical constant π
print(math.pi) # Output: 3.141592653589793math.e: The mathematical constant e
print(math.e) # Output: 2.718281828459045math.sqrt(): Square root
print(math.sqrt(16)) # Output: 4.0math.pow(): Raise a number to a power
print(math.pow(2, 3)) # Output: 8.0math.exp(): Exponential function
print(math.exp(1)) # Output: 2.718281828459045math.log(): Natural logarithm
print(math.log(math.e)) # Output: 1.0math.log10(): Base-10 logarithm
print(math.log10(100)) # Output: 2.0math.sin(): Sine of an angle (in radians)
print(math.sin(math.pi/2)) # Output: 1.0math.cos(): Cosine of an angle (in radians)
print(math.cos(math.pi)) # Output: -1.0math.tan(): Tangent of an angle (in radians)
print(math.tan(math.pi/4)) # Output: 0.9999999999999999math.degrees(): Convert angle from radians to degrees
print(math.degrees(math.pi)) # Output: 180.0math.radians(): Convert angle from degrees to radians
print(math.radians(180)) # Output: 3.141592653589793math.ceil(): Round up to the nearest integer
print(math.ceil(4.3)) # Output: 5math.floor(): Round down to the nearest integer
print(math.floor(4.7)) # Output: 4math.trunc(): Truncate decimal part
print(math.trunc(4.7)) # Output: 4math.fabs(): Absolute value
print(math.fabs(-4.7)) # Output: 4.7math.factorial(): Factorial of a number
print(math.factorial(5)) # Output: 120math.gcd(): Greatest common divisor
print(math.gcd(48, 18)) # Output: 6math.isfinite(): Check if number is finite
print(math.isfinite(10)) # Output: Truemath.isinf(): Check if number is infinite
print(math.isinf(math.inf)) # Output: Truemath.isnan(): Check if value is NaN (Not a Number)
print(math.isnan(math.nan)) # Output: Truemath.lcm(): Least common multiple (Python 3.9+)
print(math.lcm(4, 6)) # Output: 12math.comb(): Number of ways to choose k items from n items (Python 3.8+)
print(math.comb(5, 2)) # Output: 10math.perm(): Number of ways to arrange k items from n items (Python 3.8+)
print(math.perm(5, 2)) # Output: 20math.asin(): Arc sine
print(math.asin(1)) # Output: 1.5707963267948966math.acos(): Arc cosine
print(math.acos(0)) # Output: 1.5707963267948966math.atan(): Arc tangent
print(math.atan(1)) # Output: 0.7853981633974483math.hypot(): Euclidean norm
print(math.hypot(3, 4)) # Output: 5.0math.erf(): Error function
print(math.erf(1)) # Output: 0.8427007929497149math.gamma(): Gamma function
print(math.gamma(5)) # Output: 24.0
Citations:
[1] https://en.wikipedia.org/wiki/Math_library
[2] https://www.geeksforgeeks.org/c-library-math_h/
[3] https://www.lehigh.edu/~ineng2/clipper/notes/mathlibrary.html
[4] https://www.dataquest.io/blog/ python-math-module-and-functions/
[5] https://www.programiz.com/c-programming/library-function/math.h