Skip to content

Python Math API

Python built-in numeric functions — abs, round, min, max, sum, pow, divmod and conversions.

1 class · 11 methods

Built-in Math Functions

11 methods

Built-in numeric functions available without import.

abs(x)

Return the absolute value of x.

Parameters

NameTypeDescription
xint | float | complexNumber.

Returns

int | float

Example

python
abs(-5)      # 5
abs(-3.14)   # 3.14
abs(3 + 4j)  # 5.0
round(number[, ndigits])

Round number to ndigits precision (default 0). Uses banker's rounding.

Parameters

NameTypeDescription
numberfloatNumber to round.
ndigitsintDecimal places (default 0).

Returns

int | float

Example

python
round(3.14159, 2)  # 3.14
round(2.5)         # 2 (banker's rounding)
round(3.5)         # 4
min(iterable, *[, key, default])

Return the smallest item in an iterable or among arguments.

Parameters

NameTypeDescription
iterableIterableIterable of items.
keyCallable | NoneFunction extracting comparison key.
defaultAnyReturned if iterable is empty.

Returns

Any

Example

python
min([3, 1, 2])              # 1
min(3, 1, 2)                 # 1
min(['bb', 'a'], key=len)    # 'a'
max(iterable, *[, key, default])

Return the largest item in an iterable or among arguments.

Parameters

NameTypeDescription
iterableIterableIterable of items.
keyCallable | NoneFunction extracting comparison key.
defaultAnyReturned if iterable is empty.

Returns

Any

Example

python
max([3, 1, 2])              # 3
max('hello')                # 'o'
max(['bb', 'aaa'], key=len) # 'aaa'
sum(iterable[, start])

Sum items of iterable, starting from start (default 0).

Parameters

NameTypeDescription
iterableIterable[number]Iterable of numbers.
startnumberInitial value.

Returns

number

Example

python
sum([1, 2, 3])        # 6
sum([1, 2, 3], 10)    # 16
sum([[1], [2]], [])   # [1, 2]
pow(base, exp[, mod])

Return base**exp. If mod is given, return base**exp % mod (efficient modular exponentiation).

Parameters

NameTypeDescription
basenumberBase.
expnumberExponent.
modintModulus.

Returns

number

Example

python
pow(2, 10)        # 1024
pow(2, 10, 1000)  # 24  (== 1024 % 1000)
divmod(a, b)

Return (a // b, a % b) as a tuple.

Parameters

NameTypeDescription
anumberDividend.
bnumberDivisor.

Returns

tuple[number, number]

Example

python
divmod(7, 3)   # (2, 1)
divmod(10, 2)  # (5, 0)
chr(i)

Return the string representing a character whose Unicode code point is i.

Parameters

NameTypeDescription
iintUnicode code point (0..0x10FFFF).

Returns

str

Example

python
chr(65)   # 'A'
chr(97)   # 'a'
chr(8364) # '€'
ord(c)

Return the Unicode code point of a single character.

Parameters

NameTypeDescription
cstrSingle character.

Returns

int

Example

python
ord('A')  # 65
ord('a')  # 97
ord('€')  # 8364
hex(x)

Return the hexadecimal string representation of an integer, prefixed with '0x'.

Parameters

NameTypeDescription
xintInteger to convert.

Returns

str

Example

python
hex(255)   # '0xff'
hex(16)    # '0x10'
hex(-10)   # '-0xa'
bin(x)

Return the binary string representation of an integer, prefixed with '0b'.

Parameters

NameTypeDescription
xintInteger to convert.

Returns

str

Example

python
bin(10)   # '0b1010'
bin(255)  # '0b11111111'
bin(-5)   # '-0b101'