๐Ÿงฎ Python Tutorial โ€” Math Module

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

๐Ÿ’ก This module is optimized in C โ†’ extremely fast
๐Ÿ’ก 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
ConstantMeaning
math.piฯ€ (3.14โ€ฆ)
math.eEulerโ€™s number
math.tau2ฯ€
math.infInfinity
math.nanNot 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.0

Inverse Trigonometric Functions

inverse_trig.py

print(math.asin(1))       # pi/2
print(math.acos(1))       # 0
print(math.atan(1))       # pi/4

Convert Angle Units

degrees_radians.py

print(math.degrees(math.pi))   # 180
print(math.radians(180))      # 3.14

7. 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))     # True

12. 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 ๐Ÿ“˜

CategoryFunctions
Roundingceil, floor, trunc
Powerpow, sqrt
Logslog, log10, log2
Trigsin, cos, tan
Inverse Trigasin, acos, atan
Conversiondegrees, radians
Combinatoricsfactorial, comb, perm
Precision Mathfsum, prod
Checkisnan, 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 ๐ŸŽ‰

>>โ€œThe math module turns Python into a powerful scientific calculator โ€” enabling precise, fast, and elegant mathematical operations.โ€ โœจ

You now fully understand the Math module in Python! Want the next topic? Try Random Module, Statistics Module, Fractions, or Decimal. Just tell me! ๐Ÿ˜Š