
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Absolute Value in Python
For example, the absolute value of -2 is 2, and 2 is simply 2. In this article, we will explore how to calculate the absolute value of a number in Python.
Calculating Absolute Value in Python
The following methods can be efficiently used to calculate the absolute value of a number.
Using User-Defined Function (Brute Method)
The following are steps for creating a function that will calculate the absolute value. This is a brute force approach (The brute method is a problem-solving approach that we will use to try all possible solutions for this).
- Create a function that returns the absolute value of the number passed to it as an argument.
- Use the if conditional statement to check whether the number is less than 0.
- Return the negative of the value if the condition is true i.e -(-) value becomes plus(+) which is the absolute value.
- Return the number passed if it is a positive number directly
Example
The following example is calculating absolute value using a user-defined function.
# creating a function that returns the absolute value def getAbsoluteValue(value): if(value<0): return -value return value print("Absolute value of 5 = ", getAbsoluteValue(5)) # calling the getAbsoluteValue() function by passing some random negative number print("Absolute value of -5 = ", getAbsoluteValue(-5))
Output
Absolute value of 5 = 5 Absolute value of -5 = 5
Using abs() function
The abs() function is a Python built-in function used to find the absolute value of a number. It is a simple way to calculate absolute value.
Example
The following program returns the absolute values of integers using the abs() function.
# input numbers(integers) num_1 = 4 num_2 = -6 num_3 = 0 num_4 = -875 # calculating absolute values of input integers print("absolute value of 4 = ", abs(num_1)) print("absolute value of -6 = ", abs(num_2)) print("absolute value of 0 = ", abs(num_3)) print("absolute value of -875 = ", abs(num_4))
Output
absolute value of 4 = 4 absolute value of -6 = 6 absolute value of 0 = 0 absolute value of -875 = 875
Using math.fabs() function
Python has the math.fabs() function in addition to the standard abs() function. Similar to the abs(), this function also accepts a single parameter representing an integer value and returns the absolute value of the given value as a floating-point value.
Example
The following program prints the absolute values of complex numbers using math.fabs() function.
# importing math module import math # input numbers num_1 = 4 num_2 = -6.5 num_3 = -5 num_4 = -8 # calculating absolute values of input numbers as floating-point numbers print("absolute value of 4 = ", math.fabs(num_1)) print("absolute value of -6.5 = ", math.fabs(num_2)) print("absolute value of -5 = ", math.fabs(num_3)) print("absolute value of -8.65 = ", math.fabs(num_4))
Output
absolute value of 4 = 4.0 absolute value of -6.5 = 6.5 absolute value of -5 = 5.0 absolute value of -8.65 = 8.0
Calculating the Absolute Value of Complex Numbers
We can also calculate the absolute value of Complex Numbers using the abs() function (we just need to pass a complex number as a parameter to this function).
A complex number is made up of both real and imaginary numbers. An imaginary number is one that is expressed in terms of the square root of a negative number.
They are typically stated in terms of value, which is the square root of -1. For example, ?6+?49 is a complex number where 6 is the real part and 49 is the imaginary part.
Example
In the following program, we are calculating the absolute values of complex numbers using the abs() function -
# input complex numbers num_1 = 4+5j num_2 = 2-3j num_3 = -3-4j # calculating absolute values of input complex numbers print("absolute value of 4+5j = ", abs(num_1)) print("absolute value of 2-3j = ", abs(num_2)) print("absolute value of -3-4j = ", abs(num_3))
Output
absolute value of 4+5j = 6.4031242374328485 absolute value of 2-3j = 3.605551275463989 absolute value of -3-4j = 5.0