Introduction ๐
The math module in Python provides mathematical functions for performing complex calculations. It includes functions for trigonometry, logarithms, factorials, powers, constants, rounding, and more.
Note
๐ก Common uses: scientific computing, finance, physics, engineering, ML
1. Importing the Math Module ๐งฑ
import_math.py
import mathโ Simply import and use functions as math.function()
2. Math Constants ๐ข
math_constants.py
import math
print(math.pi) # 3.141592...
print(math.e) # 2.718281...
print(math.tau) # 6.283185...
print(math.inf) # Infinity
print(math.nan) # Not-a-Number| Constant | Meaning |
|---|---|
| math.pi | ฯ (3.14โฆ) |
| math.e | Eulerโs number |
| math.tau | 2ฯ |
| math.inf | Infinity |
| math.nan | Not a Number |
3. Rounding Functions ๐
rounding.py
print(math.ceil(4.1)) # 5
print(math.floor(4.9)) # 4
print(math.trunc(4.8)) # 4โ ceil() โ round up
โ floor() โ round down
โ trunc() โ remove decimal
4. Power & Square Root Functions โก
power_sqrt.py
print(math.pow(2, 3)) # 8.0
print(math.sqrt(16)) # 4.0โ pow() returns float
โ sqrt() gives square root
5. Logarithmic Functions ๐ฅ
logarithms.py
print(math.log(10)) # Natural log (base e)
print(math.log10(1000)) # Base 10 log
print(math.log2(8)) # Base 2 logโ Essential for ML, finance, scientific work
6. Trigonometric Functions ๐บ
trigonometry.py
print(math.sin(math.pi/2)) # 1.0
print(math.cos(0)) # 1.0
print(math.tan(math.pi/4)) # 1.0Inverse Trigonometric Functions
inverse_trig.py
print(math.asin(1)) # pi/2
print(math.acos(1)) # 0
print(math.atan(1)) # pi/4Convert Angle Units
degrees_radians.py
print(math.degrees(math.pi)) # 180
print(math.radians(180)) # 3.147. Factorial & Combinations ๐งฌ
factorial_comb.py
print(math.factorial(5)) # 120
print(math.comb(5, 2)) # 10
print(math.perm(5, 2)) # 20โ Used in probability, combinatorics, permutations
8. Absolute & Copysign Functions โโ
abs_copysign.py
print(math.fabs(-4.2)) # 4.2
print(math.copysign(2, -5)) # -2.0โ fabs() gives absolute value
9. Sum & Product Helpers ๐งฎ
sum_prod.py
nums = [1, 2, 3, 4]
print(math.fsum(nums)) # precise sum
print(math.prod(nums)) # productโ fsum() avoids floating-point errors
โ prod() multiplies all values
10. Hypotenuse Function (Pythagorean) ๐
hypot.py
print(math.hypot(3, 4)) # 5โ Calculates โ(xยฒ + yยฒ)
11. Useful Boolean Check Functions โ๏ธโ
boolean_checks.py
print(math.isnan(math.nan)) # True
print(math.isinf(math.inf)) # True
print(math.isfinite(10)) # True12. Complete Example โ Scientific Calculation ๐ก๏ธ
scientific_example.py
import math
angle = math.radians(45)
height = 10
distance = height * math.tan(angle)
print("Distance:", distance)โ Converts degrees โ radians
โ Calculates distance using trigonometry
Math Module Cheat Sheet ๐
| Category | Functions |
|---|---|
| Rounding | ceil, floor, trunc |
| Power | pow, sqrt |
| Logs | log, log10, log2 |
| Trig | sin, cos, tan |
| Inverse Trig | asin, acos, atan |
| Conversion | degrees, radians |
| Combinatorics | factorial, comb, perm |
| Precision Math | fsum, prod |
| Check | isnan, isinf, isfinite |
Best Practices ๐ก
- โ Use the math module instead of manually writing formulas
- โ Prefer fsum for precise floating-point addition
- โ Use trigonometric functions only with radians
- โ Combine with datetime for scheduling or physics calculations
Conclusion ๐
You now fully understand the Math module in Python! Want the next topic? Try Random Module, Statistics Module, Fractions, or Decimal. Just tell me! ๐