Python abs() Function

Returns the absolute value of a number

Usage

The abs() method returns the absolute value of a number.

Syntax

abs(number)

Python abs() function parameters
ParameterConditionDescription
numberRequiredAny number (integer, float or a complex number)

Examples

Example: Find absolute value of -10

x = -10
print(abs(x))   # 10

The number may be a floating point number.

Example: abs() on floating point number

# integer
x = -20.5
print(abs(x))   # 20.5

If the number is a complex number, its magnitude is returned.

Example: Find magnitude of a complex number

x = 3 + 4j
print(abs(x))   # 5.0