Built-in Math Functions
11 methodsBuilt-in numeric functions available without import.
abs(x)Return the absolute value of x.
Parameters
| Name | Type | Description |
|---|---|---|
| x | int | float | complex | Number. |
Returns
int | float
Example
abs(-5) # 5
abs(-3.14) # 3.14
abs(3 + 4j) # 5.0round(number[, ndigits])Round number to ndigits precision (default 0). Uses banker's rounding.
Parameters
| Name | Type | Description |
|---|---|---|
| number | float | Number to round. |
| ndigits | int | Decimal places (default 0). |
Returns
int | float
Example
round(3.14159, 2) # 3.14
round(2.5) # 2 (banker's rounding)
round(3.5) # 4min(iterable, *[, key, default])Return the smallest item in an iterable or among arguments.
Parameters
| Name | Type | Description |
|---|---|---|
| iterable | Iterable | Iterable of items. |
| key | Callable | None | Function extracting comparison key. |
| default | Any | Returned if iterable is empty. |
Returns
Any
Example
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
| Name | Type | Description |
|---|---|---|
| iterable | Iterable | Iterable of items. |
| key | Callable | None | Function extracting comparison key. |
| default | Any | Returned if iterable is empty. |
Returns
Any
Example
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
| Name | Type | Description |
|---|---|---|
| iterable | Iterable[number] | Iterable of numbers. |
| start | number | Initial value. |
Returns
number
Example
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
| Name | Type | Description |
|---|---|---|
| base | number | Base. |
| exp | number | Exponent. |
| mod | int | Modulus. |
Returns
number
Example
pow(2, 10) # 1024
pow(2, 10, 1000) # 24 (== 1024 % 1000)divmod(a, b)Return (a // b, a % b) as a tuple.
Parameters
| Name | Type | Description |
|---|---|---|
| a | number | Dividend. |
| b | number | Divisor. |
Returns
tuple[number, number]
Example
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
| Name | Type | Description |
|---|---|---|
| i | int | Unicode code point (0..0x10FFFF). |
Returns
str
Example
chr(65) # 'A'
chr(97) # 'a'
chr(8364) # '€'ord(c)Return the Unicode code point of a single character.
Parameters
| Name | Type | Description |
|---|---|---|
| c | str | Single character. |
Returns
int
Example
ord('A') # 65
ord('a') # 97
ord('€') # 8364hex(x)Return the hexadecimal string representation of an integer, prefixed with '0x'.
Parameters
| Name | Type | Description |
|---|---|---|
| x | int | Integer to convert. |
Returns
str
Example
hex(255) # '0xff'
hex(16) # '0x10'
hex(-10) # '-0xa'bin(x)Return the binary string representation of an integer, prefixed with '0b'.
Parameters
| Name | Type | Description |
|---|---|---|
| x | int | Integer to convert. |
Returns
str
Example
bin(10) # '0b1010'
bin(255) # '0b11111111'
bin(-5) # '-0b101'