Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

abs()

Computes an absolute value or the magnitude of a complex number.

Version: Python 3.14; repository image, production runtime not attested

Syntax

abs(number, /)

Computes an absolute value or the magnitude of a complex number.

For a complex input the result is a real distance from the origin.

For an integer, the distance from zero keeps the integer type. For the complex point (3, 4), the Euclidean magnitude is √(3²+4²) = 5.0. User-defined numbers can implement __abs__ to participate in the same call.

assert abs(-7) == 7 and isinstance(abs(-7), int)
assert abs(3 + 4j) == 5.0 and isinstance(abs(3 + 4j), float)

Examples

print(abs(-7))
print(abs(3 + 4j))

Result

7
5.0

For an integer, the distance from zero keeps the integer type. For the complex point (3, 4), the Euclidean magnitude is √(3²+4²) = 5.0. User-defined numbers can implement `__abs__` to participate in the same call.

Sources