The abs function

The function abs returns the absolute value of a number, i.e. it turns a negative number into a positive one.

Syntax

pythonabs(number)

Example

Let's derive the modulus of the number -5:

pythonnum = -5
print(abs(num))

Result of code execution:

python5

Example

Let's derive the absolute value of the number 10:

pythonnum = 10
print(abs(num))

Result of code execution:

python10

Example

Now let's print the absolute value of the floating-point number -2.5:

pythonnum = -2.5
print(abs(num))

Result of code execution:

python2.5

See also

  • function min,
    which returns the minimum number
  • function max,
    which returns the maximum number
  • function round,
    which rounds off the number
  • method sqrt of module math,
    which returns the square root of a number