Math

Python - A Quick Start for existing Programers

2 min read

Published Sep 16 2025, updated Sep 30 2025


21
0
0
0

Python

The math module is part of Python’s standard library.

  • Provides mathematical functions and constants for real numbers (not complex numbers).
  • Useful for:
    • Trigonometry (sin, cos, tan)
    • Exponentials & logarithms (exp, log)
    • Rounding & absolute values (ceil, floor, fabs)
    • Factorials, combinatorics (factorial, comb, perm)
    • Special functions (gcd, lcm, hypot)
    • Constants (pi, e, tau, inf, nan)

It can imported by:

import math




Main Features & Methods

Constants:

  • math.pi → 3.14159…
  • math.e → 2.71828…
  • math.tau → 6.28318… (2π)
  • math.inf → Infinity
  • math.nan → Not a Number

Rounding / Absolute:

  • ceil(x) → Round up
  • floor(x) → Round down
  • trunc(x) → Truncate toward 0
  • fabs(x) → Absolute value
  • fmod(x, y) → Remainder (like % but float-safe)
  • modf(x) → Splits into fractional and integer parts
  • copysign(x, y)x with the sign of y

Exponential & Logarithms:

  • exp(x) → eˣ
  • expm1(x) → eˣ - 1 (better for small x)
  • log(x, base) → Logarithm (default base = e)
  • log10(x) → Base-10 log
  • log2(x) → Base-2 log
  • log1p(x) → log(1 + x), accurate for small x
  • pow(x, y) → xʸ (like **)
  • sqrt(x) → Square root

Trigonometry:

  • sin(x), cos(x), tan(x) → in radians
  • asin(x), acos(x), atan(x) → inverse trig
  • atan2(y, x) → atan(y/x) considering quadrant
  • hypot(x, y) → √(x² + y²)

Hyperbolic Functions:

  • sinh(x), cosh(x), tanh(x)
  • asinh(x), acosh(x), atanh(x)

Angular Conversion:

  • degrees(x) → radians → degrees
  • radians(x) → degrees → radians

Factorials & Combinatorics:

  • factorial(n) → n!
  • comb(n, k) → n choose k
  • perm(n, k) → n permute k

Number Theory & Special Functions:

  • gcd(a, b) → greatest common divisor
  • lcm(a, b) → least common multiple
  • isqrt(n) → integer square root
  • prod(iterable) → product of iterable elements
  • fsum(iterable) → precise float sum
  • sum vs fsum: fsum is more accurate for floats




Demonstration of the math module

import math

# ----------------------------------------
# Constants
print("pi:", math.pi)
print("e:", math.e)
print("tau:", math.tau)
print("inf:", math.inf)
print("nan:", math.nan)

# ----------------------------------------
# Rounding & Absolute
print("ceil(2.3):", math.ceil(2.3))
print("floor(2.7):", math.floor(2.7))
print("trunc(-3.9):", math.trunc(-3.9))
print("fabs(-5):", math.fabs(-5))
print("fmod(7, 3):", math.fmod(7, 3))
print("modf(3.14):", math.modf(3.14))
print("copysign(3, -10):", math.copysign(3, -10))

# ----------------------------------------
# Exponentials & Logs
print("exp(2):", math.exp(2))
print("expm1(1e-5):", math.expm1(1e-5))
print("log(8):", math.log(8))
print("log(100, 10):", math.log(100, 10))
print("log10(1000):", math.log10(1000))
print("log2(16):", math.log2(16))
print("log1p(1e-5):", math.log1p(1e-5))
print("pow(2, 5):", math.pow(2, 5))
print("sqrt(25):", math.sqrt(25))

# ----------------------------------------
# Trigonometry
print("sin(pi/2):", math.sin(math.pi/2))
print("cos(0):", math.cos(0))
print("tan(pi/4):", math.tan(math.pi/4))
print("asin(1):", math.asin(1))
print("acos(0):", math.acos(0))
print("atan(1):", math.atan(1))
print("atan2(1, 1):", math.atan2(1, 1))
print("hypot(3, 4):", math.hypot(3, 4))

# ----------------------------------------
# Hyperbolic
print("sinh(1):", math.sinh(1))
print("cosh(1):", math.cosh(1))
print("tanh(1):", math.tanh(1))
print("asinh(1):", math.asinh(1))
print("acosh(2):", math.acosh(2))
print("atanh(0.5):", math.atanh(0.5))

# ----------------------------------------
# Angle conversions
print("degrees(pi):", math.degrees(math.pi))
print("radians(180):", math.radians(180))

# ----------------------------------------
# Factorials & Combinatorics
print("factorial(5):", math.factorial(5))
print("comb(5, 2):", math.comb(5, 2))
print("perm(5, 2):", math.perm(5, 2))

# ----------------------------------------
# Number Theory
print("gcd(48, 18):", math.gcd(48, 18))
print("lcm(12, 15):", math.lcm(12, 15))
print("isqrt(17):", math.isqrt(17))
print("prod([1,2,3,4]):", math.prod([1,2,3,4]))
print("fsum([0.1]*10):", math.fsum([0.1]*10)) # precise float sum
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact