Python abs() Function with Examples

The abs() is built-in python function which is used to returns the absolute value of a specified number. In simple terms we can say that this....

Here a tutorial in which you will learn about the abs() function in python with the examples.

Python abs() Function

Python abs() function

The is built-in python function which is used to returns the absolute value of a specified number. In simple terms we can say that this function is used to remove the negative sign of the given number. The abs in python function will return the magnitude of a number if that number is a complex number. 

Syntax of Python abs() function

The syntax of abs() function is:

abs(number)

Parameter of abs() Function in Python

The in python takes only one argument i.e., numbers. Number is that specified number whose absolute value is returned by using the abs() function. The number will be of following types;

• Floating numbers

• Integers

• Complex numbers

Return value of Python abs() function

The python function is used to return the absolute value of the following numbers;

Floating numbers- The floating absolute value is returned by using abs() function.

Integers- The integer absolute value is returned by using this function.

Complex numbers- The abs in python function will return the magnitude of a complex number.

Example 1: Integer number 

To get the absolute value of integer number :

# Python abs() function for an integers number 

value = -5

print('The absolute value of -5 is:', abs(value))

Output

The absolute value of -5 is: 5

Example 2: Floating number

To get absolute value of floating number :

# Python abs() function for floating number

value = -14.09

print('The absolute value of -14.09 is:', abs(value))

Output

The absolute value of -14.09 is: 14.09

Example 3: Complex number

To get a magnitude of complex number :

# Python abs() function for Complex number

value = (7 - 8j)

print('The magnitude of 7 - 8j is:', abs(value))

Output

The magnitude of 7 - 8j is: 10.63014581273465