Python Math
Python Math Module
The math module in Python provides a variety of mathematical functions and constants that are commonly used for numerical operations. It includes functions for performing basic operations like addition, subtraction, multiplication, and division, as well as more advanced functions such as trigonometry, logarithms, and statistical operations.
To use the math module, you first need to import it:
import math
Common Functions in the math Module
1. Basic Mathematical Operations
math.sqrt(x): Returns the square root ofx.print(math.sqrt(16)) # Output: 4.0math.pow(x, y): Returnsxraised to the power ofy.print(math.pow(2, 3)) # Output: 8.0math.fabs(x): Returns the absolute value ofx.print(math.fabs(-5)) # Output: 5.0math.ceil(x): Returns the smallest integer greater than or equal tox(rounds up).print(math.ceil(4.2)) # Output: 5math.floor(x): Returns the largest integer less than or equal tox(rounds down).print(math.floor(4.8)) # Output: 4
2. Trigonometric Functions
math.sin(x): Returns the sine ofx(angle in radians).print(math.sin(math.pi / 2)) # Output: 1.0math.cos(x): Returns the cosine ofx(angle in radians).print(math.cos(math.pi)) # Output: -1.0math.tan(x): Returns the tangent ofx(angle in radians).print(math.tan(math.pi / 4)) # Output: 1.0math.asin(x): Returns the arcsine (inverse sine) ofxin radians.print(math.asin(1)) # Output: 1.5707963267948966 (π/2)math.acos(x): Returns the arccosine (inverse cosine) ofxin radians.print(math.acos(1)) # Output: 0.0math.atan(x): Returns the arctangent (inverse tangent) ofxin radians.print(math.atan(1)) # Output: 0.7853981633974483 (π/4)
3. Logarithmic Functions
math.log(x, base): Returns the logarithm ofxto the specifiedbase.print(math.log(8, 2)) # Output: 3.0 (log base 2 of 8)math.log10(x): Returns the base-10 logarithm ofx.print(math.log10(100)) # Output: 2.0math.log2(x): Returns the base-2 logarithm ofx.print(math.log2(8)) # Output: 3.0
4. Constants
The math module includes several mathematical constants:
math.pi: The mathematical constant π (3.141592653589793).print(math.pi) # Output: 3.141592653589793math.e: The mathematical constant e (2.718281828459045), the base of the natural logarithm.print(math.e) # Output: 2.718281828459045math.inf: Positive infinity.print(math.inf) # Output: infmath.nan: Represents “Not a Number” (NaN).print(math.nan) # Output: nan
5. Factorial and Combinations
math.factorial(x): Returns the factorial ofx.print(math.factorial(5)) # Output: 120math.comb(n, k): Returns the number of ways to choosekitems fromnitems (combinations).print(math.comb(5, 2)) # Output: 10math.perm(n, k): Returns the number of ways to arrangekitems fromnitems (permutations).print(math.perm(5, 2)) # Output: 20
6. Hyperbolic Functions
math.sinh(x): Returns the hyperbolic sine ofx.print(math.sinh(1)) # Output: 1.1752011936438014math.cosh(x): Returns the hyperbolic cosine ofx.print(math.cosh(1)) # Output: 1.5430806348152437math.tanh(x): Returns the hyperbolic tangent ofx.print(math.tanh(1)) # Output: 0.7615941559557649
Example: Using the math Module in a Program
Here’s an example that uses multiple functions from the math module to perform calculations:
import math
# Calculate the area of a circle
radius = 5
area = math.pi * math.pow(radius, 2)
print(f"Area of the circle: {area:.2f}")
# Calculate the logarithm of a number
number = 1000
log_value = math.log(number, 10)
print(f"Log base 10 of {number}: {log_value:.2f}")
# Calculate the factorial of a number
number = 6
factorial = math.factorial(number)
print(f"Factorial of {number}: {factorial}")
# Calculate the sine of an angle
angle_rad = math.radians(30) # Convert 30 degrees to radians
sine_value = math.sin(angle_rad)
print(f"Sine of 30 degrees: {sine_value:.2f}")
Conclusion
The math module is a great tool for performing mathematical operations in Python. It provides functions for basic arithmetic, trigonometry, logarithms, and more. You can use constants like math.pi and math.e for mathematical calculations, and you can also work with special functions such as factorials and combinations.