Python Abs


Abs function

The abs() function return the absolute value of a number. The argument inside the function may be an integer or a floating point number.

Examples

Example 1

>>> x = 5
>>> abs(x)
5
>>> 

Example 2

>>> y = -2
>>> abs(y)
2
>>> 

Example 3

>>> z = -3.5
>>> abs(z)
3.5
>>> 

Example 4

>>> a = 'abc'
>>> abs(a)
Traceback (most recent call last):
  File "", line 1, in 
    abs(a)
TypeError: bad operand type for abs(): 'str'
>>>