Math
Python - A Quick Start for existing Programers
2 min read
This section is 2 min read, full guide is 44 min read
Published Sep 16 2025, updated Aug 17 2026
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
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 mathMain Features & Methods
Constants:
math.pi→ 3.14159…math.e→ 2.71828…math.tau→ 6.28318… (2π)math.inf→ Infinitymath.nan→ Not a Number
Rounding / Absolute:
ceil(x)→ Round upfloor(x)→ Round downtrunc(x)→ Truncate toward 0fabs(x)→ Absolute valuefmod(x, y)→ Remainder (like%but float-safe)modf(x)→ Splits into fractional and integer partscopysign(x, y)→xwith the sign ofy
Exponential & Logarithms:
exp(x)→ eˣexpm1(x)→ eˣ - 1 (better for small x)log(x, base)→ Logarithm (default base = e)log10(x)→ Base-10 loglog2(x)→ Base-2 loglog1p(x)→ log(1 + x), accurate for small xpow(x, y)→ xʸ (like**)sqrt(x)→ Square root
Trigonometry:
sin(x), cos(x), tan(x)→ in radiansasin(x), acos(x), atan(x)→ inverse trigatan2(y, x)→ atan(y/x) considering quadranthypot(x, y)→ √(x² + y²)
Hyperbolic Functions:
sinh(x), cosh(x), tanh(x)asinh(x), acosh(x), atanh(x)
Angular Conversion:
degrees(x)→ radians → degreesradians(x)→ degrees → radians
Factorials & Combinatorics:
factorial(n)→ n!comb(n, k)→ n choose kperm(n, k)→ n permute k
Number Theory & Special Functions:
gcd(a, b)→ greatest common divisorlcm(a, b)→ least common multipleisqrt(n)→ integer square rootprod(iterable)→ product of iterable elementsfsum(iterable)→ precise float sumsumvsfsum:fsumis more accurate for floats
Demonstration of the math module
import math# ----------------------------------------# Constantsprint("pi:", math.pi)print("e:", math.e)print("tau:", math.tau)print("inf:", math.inf)print("nan:", math.nan)# ----------------------------------------# Rounding & Absoluteprint("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 & Logsprint("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))# ----------------------------------------# Trigonometryprint("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))# ----------------------------------------# Hyperbolicprint("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 conversionsprint("degrees(pi):", math.degrees(math.pi))print("radians(180):", math.radians(180))# ----------------------------------------# Factorials & Combinatoricsprint("factorial(5):", math.factorial(5))print("comb(5, 2):", math.comb(5, 2))print("perm(5, 2):", math.perm(5, 2))# ----------------------------------------# Number Theoryprint("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