Post

Python abs() Function

The abs() is a built-in function available in python. Python abs() functions are used to return the python absolute value of the given number.

Python abs() Function

The syntax of abs() method:

1
abs(number)

Python abs() Parameters

We can only give a single argument in the abs() function.

number - A number can be an integer, floating number, or complex number whose absolute value of python will return.

Let see an example of the abs() function.

Example 1:

1
2
3
4
5
6
7
8
#Integer Number
integer = -32
print("The Absolute value of the integer variable is:", abs(integer))

#Floating Number
floating = -12.54
print("The Absolute value of the floating variable is:", abs(floating))

The output will be as follow:

1
2
The Absolute value of the integer variable is: 32
The Absolute value of the floating variable is: 12.54

Unlike integer and floating numbers, complex numbers will not return any absolute value in python, but instead, they will return a magnitude of the complex number.

Example 2:

1
2
3
4
5
#Complex Number

complex = (10 - 7j)

print("The Magnitude of 10 - 7j is:", abs(complex))

The output is as follows:

1
The Magnitude of 10 - 7j is: 12.206555615733702

Rules of abs()

  • If the integer value is passed it will return an integer value only.
  • If the floating value is passed it will return an absolute floating value.
  • If the complex number is passed it will return a magnitude of the given number.
This post is licensed under CC BY 4.0 by the author.