수학 함수와 기초 통계는 표준 라이브러리의 math와 statistics 모듈로 처리합니다.
NumPy 같은 외부 라이브러리 없이도 일반적인 수학·통계 작업을 할 수 있습니다.
math 모듈 — 수학 함수.
math.sqrt(16) — 제곱근(4.0).
math.pi — π(3.14159...).
math.e — 자연로그의 밑(2.71828...).
math.sin·cos·tan — 삼각함수.
math.log(100, 10) — 로그.
math.factorial(5) — 5!=120.
math.gcd(12, 18) — 최대공약수.
유용한 수학 함수.
math.floor(3.7) — 내림(3).
math.ceil(3.2) — 올림(4).
math.isnan(x), math.isinf(x) — 특수값 검사.
math.dist((0,0), (3,4)) — 두 점 사이 거리(5.0).
math.hypot(3, 4) — 빗변(5.0).
math.comb(5, 2) — 조합 C(5,2)=10.
statistics 모듈 — 기초 통계.
statistics.mean([1,2,3,4,5]) — 평균(3).
statistics.median(...) — 중앙값.
statistics.mode(...) — 최빈값.
statistics.stdev(...) — 표준편차.
statistics.variance(...) — 분산.
외부 라이브러리와의 차이.
NumPy는 「수만~수억 개의 숫자를 빠르게」 다룰 때, math·statistics는 「작은 양의 일반 수학·통계」에 적합합니다.
큰 데이터에는 NumPy가 100~1000배 빠르지만, 작은 데이터에서는 math·statistics가 추가 의존성 없이 충분합니다.
한 줄 요약
math는 sqrt·pi·log·삼각함수 등 수학 함수, statistics는 mean·median·stdev 같은 기초 통계를 표준 라이브러리로 제공합니다.
큰 데이터는 NumPy가 효율적입니다.
더 알아볼 것
- math.fsum — 부동소수점 정확한 합
- decimal — 정확한 십진수 연산
- fractions — 분수 연산