BlogsDope image BlogsDope

Python abs() Function

Jan. 22, 2021 FUNCTION PYTHON 145

The Python abs() function

The Python library consists of a large number of in-built functions that help in performing numerous operations. One of these functions is the abs() function. The Python abs() is a built-in function that comes with the standard library of python and it returns the absolute value of a number that is passed as a parameter to this method. The absolute value of a number is the value without considering its sign i.e. the non-negative value of a number. For example, the absolute value of -35 is 35 and the absolute value of 35 is 35 as well since the sign is not considered in absolute value.

Syntax of abs() method:

The following is the syntax of the abs() method:


What values does Python abs() function works on (Parameter values)?

Being a mathematical function, the abs() function only accepts numeric values. The following are the numbers on which the abs() function works:
  • integers, e.g. 9, -9, etc.
  • floating point numbers, e.g. 3.14, 6.67, etc.
  • complex numbers, e.g. 2+5i, 7+9i, etc. 

Return Value of abs() function:

The abs() method takes a single argument and returns its absolute value. If the argument passed is an integer it returns an integer value and if it is a float value, the return value is also a float. However, if a complex number is passed as a parameter, the function returns the magnitude of the complex number as a float value.


Passing an integer value:

# Absolute value of integer 

x = -95
print("Absolute value of -95 is", abs(x))

y = 30
print("Absolute value of 30 is", abs(y)


​Absolute value of -95 is 95

Absolute value of 30 is 30

Passing a float value:

# Absolute value of float

x = -3.14 

print("Absolute value of -3.14 is", abs(x)) 

y = 9.8

print("Absolute value of 9.8 is", abs(y) 


 Absolute value of -3.14 is 3.14

 Absolute value of 9.8 is 9.8 

Passing a complex value:

# Absolute value of complex number
# abs() method return magnitude of a complex number

x = 3+4j
print("Absolute value of 3+4j is", abs(x))

y = 7-5j
print("Absolute value of 7-5j is", abs(y))


Absolute value of 3+4j is 5.0
Absolute value of 7-5j is 8.602325267042627

Uses of abs():

The abs() method is very useful in mathematical operations that require the absolute value of a certain parameter. Also, it can be used for finding the magnitude of a given complex number.

Liked the post?
Zealous Learner.
Editor's Picks

Please login to view or add comment(s).