2 min read•june 24, 2024
Python's math is a powerful tool for mathematical operations. It offers a wide range of functions and constants beyond basic arithmetic, enabling complex calculations in scientific and engineering applications.
From trigonometry to logarithms, the math module enhances Python's mathematical capabilities. It provides precise constants like pi and e, and functions for advanced operations, making it essential for solving numerical problems efficiently.
Built-in functions readily available in Python without importing any modules (
abs()
, round()
, min()
, max()
, pow()
). Math module functions require importing the math module: import math
provide additional mathematical functions and constants ([math.sqrt()](https://www.fiveableKeyTerm:math.sqrt())
, [math.sin()](https://www.fiveableKeyTerm:math.sin())
, [math.log()](https://www.fiveableKeyTerm:math.log())
, [math.pi](https://www.fiveableKeyTerm:math.pi)
, [math.e](https://www.fiveableKeyTerm:math.e)
). Use math module functions when dealing with (sin()
, cos()
, tan()
), performing logarithmic or exponential calculations (log()
, exp()
), or requiring like pi () or e. Use built-in functions for basic arithmetic and rounding operations (such as round()
, ceil()
, and floor()
).
Constants in the math module include
math.pi
, the mathematical constant pi (), and math.e
, the mathematical constant e (Euler's number, ). The math module provides various mathematical constants for precise calculations. Functions in the math module:
math.sqrt(x)
returns the square root of x
math.pow(x, y)
returns x raised to the power of y
math.exp(x)
returns e raised to the power of x
math.log(x[, base])
returns the logarithm of x to the given base (default is natural logarithm, base e)
Trigonometric functions: math.sin(x)
, math.cos(x)
, math.tan(x)
accept angles in radians
Inverse trigonometric functions: math.asin(x)
, math.acos(x)
, math.atan(x)
return angles in radians
Example usage:
import math # Calculate the area of a circle with radius 5 area = math.pi * math.pow(5, 2) print(area) # Output: 78.53981633974483 # Calculate the logarithm of 100 with base 10 log_value = math.log(100, 10) print(log_value) # Output: 2.0
Angle units: degrees commonly used in everyday life (0° to 360°) and radians used in mathematical calculations (0 to 2). Converting between degrees and radians:
math.radians(x)
converts angle x from degrees to radians
math.degrees(x)
converts angle x from radians to degrees
Conversion formulas: Degrees to radians: Radians to degrees:
Example usage:
import math # Convert 45 degrees to radians angle_rad = math.radians(45) print(angle_rad) # Output: 0.7853981633974483 # Convert pi/4 radians to degrees angle_deg = math.degrees(math.pi/4) print(angle_deg) # Output: 45.0
The math module provides a range of functions for more complex mathematical operations:
math.log()
, math.log10()
, math.log2()
for natural, base-10, and base-2 logarithms[math.exp()](https://www.fiveableKeyTerm:math.exp())
, math.expm1()
for e^x and e^x - 1math.sin()
, [math.cos()](https://www.fiveableKeyTerm:math.cos())
, [math.tan()](https://www.fiveableKeyTerm:math.tan())
, and their inverse functionsmath.sinh()
, math.cosh()
, math.tanh()
, and their inverse functions
These functions are essential for various applications in scientific computing and .