Built-in Python Functions for Working With Numbers

By Hemanta Sundaray on 2021-09-09

abs(x)

Returns the absolute value of number x. (converts negative numbers to positive).

print(abs(3.14))
# 3.14

print(abs(-3.14))
# 3.14

float(x)

Converts the number x to a float data type.

print(float(3))
# 3.0

print(float(-3))
# -3.0

int(x)

Converts the number x to the integer data type by truncating (not rounding) the decimal point and any digits after it.

print(int(3.1426))
# 3

max(x, y, z, ….)

Takes any number of numeric arguments and returns whichever one is the largest.

print(max(3.1426, 12, 20, -9.876))
# 20

min(x, y, z, ...)

Takes any number of numeric arguments and returns whichever one is the smallest.

print(min(3.1426, 12, 20, -9.876))
# -9.876

round(x, y)

Rounds the number x to y number of decimal places.

print(round(3.9, 2))
# 3.9

str(x)

Converts number x to the string data type.

string_type = str(3.9)

print(type(string_type))
# <class 'str'>

type(x)

Returns a string indicating the data type of x.

print(type(26))
# <class 'int'>

Join the Newsletter