import math
# 1. Square root and power
num = 16
print(f"sqrt({num}) = {math.sqrt(num)}")
print(f"pow({num}, 3) = {math.pow(num, 3)}")
# 2. Trigonometric functions (radians)
angle = math.pi / 4 # 45 degrees
print(f"sin(45°) = {math.sin(angle):.4f}")
print(f"cos(45°) = {math.cos(angle):.4f}")
# 3. Constants
print(f"Pi = {math.pi}")
print(f"Euler's number = {math.e}")
# 4. Floor and ceiling
x = 3.7
print(f"floor({x}) = {math.floor(x)}")
print(f"ceil({x}) = {math.ceil(x)}")
# 5. Greatest common divisor
a, b = 48, 18
print(f"gcd({a}, {b}) = {math.gcd(a, b)}")
This code demonstrates several functions from Python's math module. It shows how to compute square roots, powers, trigonometric values, constants, floor/ceiling rounding, and greatest common divisors.
- The `math.pow` function returns a float, but for integer powers you could use the `**` operator instead, which may be more efficient and returns an integer when both operands are integers.
- The trigonometric functions expect angles in radians. Your conversion from degrees to radians is correct using `math.pi / 4` for 45 degrees, but if you need to convert other angles, consider using `math.radians()` for clarity.