40 of 151 menu

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

abs(number)

Example

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

num = -5 print(abs(num))

Result of code execution:

5

Example

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

num = 10 print(abs(num))

Result of code execution:

10

Example

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

num = -2.5 print(abs(num))

Result of code execution:

2.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
byenru