math.pi / math.e / math.log() / math.sin()
Mathematical constants, logarithms, and trigonometric functions provided by the math module. Used for scientific calculations and angle conversions.
Syntax
import math # Mathematical constants math.pi # Pi π (approximately 3.141592653589793) math.e # Euler's number e (approximately 2.718281828459045) # Compute logarithms. math.log(x) # Natural logarithm (base e) math.log(x, base) # Logarithm with a specified base math.log2(x) # Base-2 logarithm math.log10(x) # Base-10 logarithm # Trigonometric functions (arguments in radians) math.sin(x) math.cos(x) math.tan(x) # Convert between degrees and radians math.radians(degrees) # Degrees → radians math.degrees(radians) # Radians → degrees
Constants and Functions
| Constant / Function | Description |
|---|---|
| math.pi | Pi π (3.141592653589793). Used for calculating circle areas and trigonometric values. |
| math.e | Euler's number e (2.718281828459045). The base for exponential and logarithmic calculations. |
| math.log(x) | Returns the natural logarithm (base e) of x. Raises ValueError if x is zero or negative. |
| math.log(x, base) | Returns the logarithm of x with the specified base. |
| math.log2(x) | Returns the base-2 logarithm of x. Useful for calculating bit lengths. |
| math.log10(x) | Returns the base-10 (common) logarithm of x. |
| math.sin(x) | Returns the sine of x. The argument must be in radians. |
| math.cos(x) | Returns the cosine of x. The argument must be in radians. |
| math.tan(x) | Returns the tangent of x. The argument must be in radians. |
| math.radians(degrees) | Converts an angle from degrees to radians. |
| math.degrees(radians) | Converts an angle from radians to degrees. |
Sample Code
import math
# Use mathematical constants.
print(math.pi) # Outputs '3.141592653589793'.
print(math.e) # Outputs '2.718281828459045'.
# Calculate the area of a circle.
radius = 5
area = math.pi * radius ** 2
print(f'Area: {area:.2f}') # Outputs 'Area: 78.54'.
# Compute logarithms.
print(math.log(math.e)) # Natural logarithm: outputs '1.0'.
print(math.log(100, 10)) # Base-10 logarithm: outputs '2.0'.
print(math.log2(1024)) # Base-2 logarithm: outputs '10.0'.
print(math.log10(1000)) # Common logarithm: outputs '3.0'.
# Trigonometric functions take arguments in radians.
angle_deg = 30
angle_rad = math.radians(angle_deg) # Convert degrees to radians.
print(math.sin(angle_rad)) # Outputs '0.5' (sin 30° = 0.5).
print(math.cos(angle_rad)) # Outputs '0.8660254...' (cos 30°).
# You can also specify radians using math.pi.
print(math.sin(math.pi / 6)) # sin(π/6) = sin(30°) = 0.5.
print(math.cos(0)) # Outputs '1.0'.
print(math.cos(math.pi)) # Outputs '-1.0'.
# Convert radians back to degrees.
print(math.degrees(math.pi)) # Outputs '180.0'.
print(math.degrees(math.pi / 2)) # Outputs '90.0'.
Notes
math.pi and math.e are high-precision mathematical constants stored internally by Python. They are accessed as attributes directly — no function call parentheses are needed.
All Python trigonometric functions expect angles in radians. If you are working in degrees (0°–360°), convert the value with math.radians() before passing it. math.sin(90) computes the sine of 90 radians, not 90 degrees. Always convert degrees to radians first to get the correct result.
For square roots, ceiling, and floor operations, see math.sqrt() / math.ceil() / math.floor().
If you find any errors or copyright issues, please contact us.