Python abs() function

Python built-in abs() function returns the positive value of negative number. The function accepts single parameter which can be
integer, float or complex number. If number is passed in form of string then it returns TypeError exception.

Syntax

abs(number)

There is also built-in object method __abs__() which you can use instead of abs() functions. Details of __abs__() method is explained below.

Table of Contents

Examples

x = -23
y = -1.287
z = (5-5j)

print("abs on integer number x : {}".format(abs(x)))
print("abs on float number y : {}".format(abs(y)))
print("abs on complex number z : {}".format(abs(z)))

#PYTHON OUTPUT
abs on integer number x : 23
abs on float number y : 1.287
abs on complex number z : 7.0710678118654755
Python abs() function example
Python abs() function example

All variables x,y,z are of different type and when passed through abs() function the output will always be a positive number.

num = "-25.21"
print(abs(num))

#PYTHON OUTPUT
Traceback (most recent call last):
  File "main.py", line 10, in 
    print(abs(num))
TypeError: bad operand type for abs(): 'str'

#-----------------------------

num = "-25.21"
print(abs(float(num))) 

#PYTHON OUTPUT
25.21

Passing number in string form will throw exception TypeError: bad operand type for abs(): 'str'. You can use a simple hook here
and type-cast value to float which will work fine. The correct way to deal with these exceptions are show in below programs.

Using object __abs__() method

__abs__() method is a built-in object method for integer, float and complex numbers. strings does not have these method and using this method is
better than use abs() function.

x = -23
y = -1.287
z = (5-5j)

print("abs on integer number x : {}".format(x.__abs__()))
print("abs on float number y : {}".format(y.__abs__()))
print("abs on complex number z : {}".format(z.__abs__()))

#PYTHON OUTPUT
abs on integer number x : 23
abs on float number y : 1.287
abs on complex number z : 7.0710678118654755
Python abs() function example 2
Python abs() function example 2

Check if method exists in object and then call __abs__() method.

Python built-in function hasattr() is used to check wheather particular attribute or method exists in an object.It takes two arguments object and attribute name to check and returns boolean value.
If true is returned than object has the attribute else returns false.

In below program we’ll be using hasattr() function. If the method __abs__() exists in object x than the absolute value is returned else message is displayed.

x = "-23"
if hasattr(x,'__abs__'):
    print("abs on integer number x : {}".format(x.__abs__()))
else:
    print("method abs does not exists.")

#PYTHON OUTPUT
method abs does not exists.

Program to display absolute value of number by taking input from user

print("Enter any number : ")
input = input();
if len(input)>0:
  num = float(input)
  if hasattr(num, '__abs__'):
    abs_value = num.__abs__()
    print("The absolute value of entered number is : {}".format(abs_value))
  else:
    print("You have entered invalid number")
else:
    print("You have not given input")

#PYTHON OUTPUT
Enter any number : 
-2.356
The absolute value of entered number is : 2.356
display absolute value of number by taking input from user

You can learn in more detail related to python functions by clicking on official python website link.

Key Takeways

  • abs() function return positive value of negative number passed.
  • abs function also works on complex numbers
  • Built-in object method __abs__ if available to integer, float and complex numbers. It is alternate of using abs() function.

Conclusion

We have come to end of our post on Python abs() function. Motivate us by sharing this post and if you have any doubts changes to be made than comment below.

Related Posts

Summary
Review Date
Reviewed Item
Python abs() function
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Langauage