Python abs() Function

Overview
The standard Python library has a function abs(), short for "absolute" value. It returns the value given as input without the sign with it, that is, the positive value.
What is Python abs() Function?
abs() is a mathematical function. The function abs() is just short for absolute, which returns the absolute value of the input passed in it. What does that mean? Absolute value means the value of the input passed in, without its sign or essentially the value with the sign removed or can say positive value, while if the given number is a complex number, then it returns its magnitude.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
How Does the Python abs() Function Work?
The abs() functions take a single argument and return its absolute(magnitude) values. The absolute value of a number is its distance from zero on the number line, regardless of the direction. example - the distance of -5 from origin(zero) is 5, the distance of 3+4i from origin is 5.
Examples of Python abs() Function
- Get absolute value of a number we can use abs in python to get absolute value of a number, as shown below:
Output
- Get magnitude of a complex number
In a plane of complex numbers, magnitude is the distance between the origin and the point on the plane. So for a complex number a + bi, its magnitude is :
Output
Syntax of abs() in Python
The abs in python takes a single argument. It will take either an actual or complex number and return the absolute and magnitude values, respectively.
y= abs(x)
Turn Learning into Career Growth
Parameters of abs() in Python
The abs in python takes the following parameter:
- value: It can be an integer, float, or complex type.
Return values of abs() in Python
Return Type: int or float or complex
Since the only inputs abs in python takes are of two types, either integer/float value or complex numbers, the return value of the it is also of 2 types :
-
If the input is integer or floating-point, then the return value is the absolute value (magnitude, or same value without the sign)
-
If the input is a complex number, then abs() returns the magnitude of the complex number. It can be an integer or a floating point.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example
Output:
Explanation: As you can see here, the negative values, whether integer(-21) or float (-16.51) have been returned as positive(float returned as: 16.51, int returned as 21) from the abs() function in Python, their signs have been removed.
Exception of abs() function in Python
What happens if any other data type is passed?
Output
As we can see, we passed the 'str' data type to abs() function, and the result is an error - bad operand type. That's the exact output we will get if any datatype other than the expected one is given as input to the abs() function.
Did you know?
We can also use another function to calculate the absolute value. It is the fabs() function. This function is a part of the math module in Python, and it must be imported from there before use.
There are two differences between the fabs() and abs() function.
- The abs in python is part of the standard template library of Python, whereas fabs() has to be imported from the math module
- fabs() will always return a float value, no matter what type of number is given as input. However, as we noticed above, the abs() function returns an integer if the input is an integer; otherwise, float always.
Conclusion
- The abs() function returns the absolute value of the input passed to it.
- Only one parameter is passed to the abs() function.
- If the input is a complex number, then the magnitude of the number is returned.
- You can make use of the abs() function in Python when writing code for the following scenarios:
- Used for distance measurements (like the example in the beginning).
- Determining speed from velocity (velocity is just speed with direction so that it can have different signs).