Python Glossary Русский

all Python 3mathterm

math

module

Mathematical functions for real and integer numbers: roots, logarithms, trigonometry, rounding and combinatorics.

Syntax

import math

math works with float and int and fails on complex results: the square root of a negative number raises ValueError rather than returning 1j, which is what cmath is for. Float results are inexact, so equality is tested with math.isclose, not ==. For integers there are exact functions such as isqrt, comb and gcd, which keep precision on big numbers where float-based sqrt does not.

Examples

import math
print(math.sqrt(16), math.pi > 3.14)  # → 4.0 True
print(0.1 + 0.2 == 0.3, math.isclose(0.1 + 0.2, 0.3))  # → False True
print(math.isqrt(10**20 + 1), math.comb(5, 2))  # → 10000000000 10
math.sqrt(-1)  # → ValueError: math domain error

See also

Open in the full glossarydocs.python.orgReport an inaccuracy