Abs() Function In Python

Mathematical Background:

For any real number x, the absolute value is the magnitude of the number without the associated sign part.

 

Method Name:

abs(param)

 

Method Overview:

The abs() function returns the absolute value of the parameter.

 

Parameters:

param - Parameter can be an integer, a floating point value or a complex number.

 

Note:

When the parameter is a complex number its magnitude is returned.

 

Python Example Program using abs() function:

 

#find absolute value of a negative integer

NegativeInt = -7

absValue    = abs(-NegativeInt)

print("Absolute value of given negtive integer {} is {}".format(NegativeInt, absValue))

 

#find absolute value of a negative floating point number

NegativeReal = -7.5

absValue    = abs(-NegativeReal)

print("Absolute value of given negative real number {} is {}".format(NegativeReal, absValue))

 

#find absolute value of a complex number

complexNum = complex(-7.0,2.0)

absValue    = abs(-complexNum)

print("Absolute value of given complex number {} is {}".format(complexNum, absValue))

 

Output of Python Example Program that uses the abs() function:

Absolute value of given negative integer -7 is 7

Absolute value of given negative real number -7.5 is 7.5

Absolute value of given complex number (-7+2j) is 7.280109889280518

 


Copyright 2018 © pythontic.com