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

number = -20

absolute_number = abs(number)
print(absolute_number) # Output: 20

abs() Syntax

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 or complex number.

abs() Return Value

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

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

Example 1: Get the Absolute Value of a Number

# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))
#random floating number floating = -30.33
print('Absolute value of -30.33 is:', abs(floating))

Output

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

Example 2: Get the Magnitude of a Complex Number

# random complex number
complex = (3 - 4j)
print('Magnitude of 3 - 4j is:', abs(complex))

Output

Magnitude of 3 - 4j is: 5.0

Also Read:

Did you find this article helpful?