math
abs()
Returns the absolute value of a number. For complex numbers, returns the magnitude.
abs(x) Parameters
| Parameter | Purpose |
|---|---|
| x | int, float, complex, or object with __abs__ |
| returns | Non-negative number; float for complex input (the magnitude) |
Examples
abs(-5) Returns 5
abs(-3.14) Returns 3.14
abs(3 + 4j) Returns 5.0 (magnitude)
Gotcha
abs() of a complex returns a float, not a complex — that's the magnitude sqrt(re^2 + im^2).
Related built-ins
round()
Rounds a number to ndigits decimal places. When ndigits is None (default), returns an int rounded to nearest integer using banker's rounding.
int()
Constructs an integer from a number or string. When x is a string, base selects the numeric base (0 auto-detects from prefix).
float()
Constructs a floating-point number from a number or string. Accepts 'inf', '-inf', 'nan' (case-insensitive).