Last Updated:

Abs() function in Python

 

 

The Python function abs() returns the absolute value of a number. This is one of the built-in functions in the pythonbuiltins module.

Absolute Python value using abs()

Python abs() takes one argument, which must be a number, and returns its absolute value.

  • Integer, Long – returns an absolute value.
  • Float—Returns an absolute value.
  • Complex— returns its value.

Numbers in various formats return an absolute value in the decimal numeral system, even if the numbers are defined in binary, octal, hexadecimal, or exponential form.

Python abs() with integers

Import sys
x = 5 # int
print(abs(x))
x = sys.maxsize # long
print(abs(x))

Exit:

5
9223372036854775807

Python absolute float value

x = 50.23434 # float
print(abs(x))

Exit:

50.23434

Python abs() with complex numbers

x = 10 — 4j # complex
print(abs(x))
x = complex(10, 2) # another complex example
print(abs(x))

Exit:

10.770329614269007
10.198039027185569

Python abs() with different format numbers

# numbers in different formats
x = 10.23e1/2 # exponential
print(abs(x))
x = 0b1010 # binary
print(abs(x))
x = 0o15 # octal
print(abs(x))
x = 0xF # hexadecimal
print(abs(x))

Exit:

51.15
10
13
15

This is all for brief examples of the absolute values of python numbers using the abs() function.