Python abs - Syntax
mixed abs(mixed n);
The abs() function in Python is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
class MathExample:
def __init__(self):
pass
def getAbsoluteValue(self):
# Positive integer value
n = abs(9)
print("The absolute value of 9 is " + str(n))
# Negative integer value
x = abs(-6)
print("The absolute value of -6 is " + str(x))
obj = MathExample()
obj.getAbsoluteValue()
Convert the positive and negative float to an absolute value.
class MathExample:
def __init__(self):
pass
def getAbsoluteValue(self):
# Positive floating point value
n = abs(9.3)
print("The absolute value of 9.3 is " + str(n))
# Negative floating point value
x = abs(-6.3)
print("The absolute value of -6.3 is " + str(x))
obj = MathExample()
obj.getAbsoluteValue()
Convert the positive and negative Infinity to an absolute value.
class MathExample:
def __init__(self):
pass
def getAbsoluteValue(self):
# Positive ∞ infinite value
n = abs(float("inf"))
print("The absolute value of inf is " + str(n))
# Negative ∞ infinite value
x = abs(-float("inf"))
print("The absolute value of -inf is " + str(x))
obj = MathExample()
obj.getAbsoluteValue()