Python abs()

The abs() function returns the absolute value of the given number. If the number is a complex number, abs() returns its magnitude.

Example


abs() Syntax

The syntax of abs() method is:

abs(num)

abs() Parameters

abs() method takes a single argument:

  • num - a number whose absolute value is to be returned. The number can be:
    • integer
    • floating number
    • complex number

abs() Return Value

abs() method returns the absolute value of the given number.

  • For integers - integer absolute value is returned
  • For floating numbers - floating absolute value is returned
  • For complex numbers - magnitude of the number is returned

Example 1: Get absolute value of a number

Output

Absolute value of -20 is: 20
Absolute value of -30.33 is: 30.33

Example 2: Get magnitude of a complex number

Output

Magnitude of 3 - 4j is: 5.0