Introduction to Python's math Module: Functions, Examples, and Usage
This article introduces Python's built‑in math module, explains its most commonly used functions—including basic arithmetic, trigonometry, logarithms, constants, and utility helpers—and provides clear code examples that demonstrate how to perform each operation.
1. Overview of the math module \nThe math module is a standard library in Python that provides a wide range of functions for complex mathematical calculations such as trigonometric, logarithmic, and exponential operations. To use its functions, you first need to import the module.
2. Common functions and examples
Basic arithmetic
import math
# Calculate square root
result = math.sqrt(16)
print(result) # 输出 4.0 import math
# Calculate power
result = math.pow(2, 3)
print(result) # 输出 8.0 import math
# Ceiling
result = math.ceil(3.14)
print(result) # 输出 4 import math
# Floor
result = math.floor(3.14)
print(result) # 输出 3 import math
# Truncate
result = math.trunc(3.14)
print(result) # 输出 3Trigonometric functions
import math
# Compute sine, cosine, tangent for an angle
angle = math.pi / 4
sin_val = math.sin(angle)
cos_val = math.cos(angle)
tan_val = math.tan(angle)
print(sin_val) # 输出约 0.707
print(cos_val) # 输出约 0.707
print(tan_val) # 输出约 1.0 import math
# Inverse trigonometric functions
arcsin_val = math.asin(0.707)
arccos_val = math.acos(0.707)
arctan_val = math.atan(1.0)
print(arcsin_val) # 输出约 0.785
print(arccos_val) # 输出约 0.785
print(arctan_val) # 输出约 0.785Logarithmic functions
import math
# Natural logarithm
log_val = math.log(2.71828)
log_base_10_val = math.log(100, 10)
print(log_val) # 输出约 1.0
print(log_base_10_val) # 输出 2.0 import math
# Base‑10 logarithm
log10_val = math.log10(100)
print(log10_val) # 输出 2.0Exponential function
import math
# e to the power of x
exp_val = math.exp(1)
print(exp_val) # 输出约 2.71828Angle‑radian conversion
import math
# Radians to degrees
radian = math.pi / 4
degrees = math.degrees(radian)
print(degrees) # 输出 45.0 import math
# Degrees to radians
degree = 45
radians = math.radians(degree)
print(radians) # 输出约 0.785Maximum, minimum and other utilities
import math
# Absolute value
abs_val = math.fabs(-3.14)
print(abs_val) # 输出 3.14 import math
# Remainder (modulo)
mod_val = math.fmod(10, 3)
print(mod_val) # 输出 1.0Constants
import math
# Pi
pi_val = math.pi
print(pi_val) # 输出约 3.14159 import math
# Euler's number e
e_val = math.e
print(e_val) # 输出约 2.71828Other useful functions
import math
# Check if two numbers are close
close_result = math.isclose(1.0000001, 1.0)
print(close_result) # 输出 True import math
# Check for infinity
inf_result = math.isinf(float('inf'))
print(inf_result) # 输出 True import math
# Check for NaN
nan_result = math.isnan(float('nan'))
print(nan_result) # 输出 True3. Summary
The math module offers a powerful toolbox for handling a wide variety of mathematical operations, from simple arithmetic to advanced scientific calculations, making it an essential resource for Python developers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
