Python abs() function
Python built-in abs() function returns the positive value of a negative number. The function accepts a single parameter which can be
integer, float, or complex number. If the number is passed in form of a string then it returns TypeError
an exception.
Syntax
abs(number)
There is also a built-in object method __abs__()
that you can use instead of abs()
functions. Details of __abs__()
the method are 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
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 an 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 is shown below programs.
Using object __abs__() method
__abs__()
is a built-in object method for integer, float, and complex numbers. strings do not have this method and using this method is
better than use abs()
the 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
Check if the method exists in the object and then call __abs__()
method.
Python built-in function hasattr()
is used to check whether a particular attribute or method exists in an object. It takes two arguments object and attribute name to check and returns a boolean value.
If true
is returned then an object has the attribute else returns false
.
In the below program, we’ll be using hasattr()
a function. If the method __abs__()
exists in the object x
then the absolute value is returned else a 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 the absolute value of a 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
You can learn in more detail related to python functions by clicking on the official python website link.
Key Takeaways
abs()
function returns a positive value of the negative number passed.- abs function also works on complex numbers
- Built-in object method __abs__ if available to an integer, float, and complex numbers. It is an alternative to using the abs() function.
Conclusion
We have come to the end of our post on the Python abs() function. Motivate us by sharing this post and if you have any doubts about changes to be made then comment below.
Related Posts
- What is Python keyword with Statement? Simplified for beginners
- What is __init__ in python and what does it mean?