Absolute Value in Python: In Python, we calculate the absolute value with the help of the abs() function, which is one of the most simple and useful functions in Python.

As we know that the absolute value of any number is its distance from 0.

So, the absolute value of any negative number gets converted into positive. On the other hand, the absolute value of any positive number remains as it is.

So, the absolute value of -8 is 8 and the absolute value of 6 is 6.

Now that you understand the working and process of calculating the absolute value.

Let’s take a look at the code syntax of calculating absolute value in Python.

Table of Contents

Syntax of abs() Function in Python

Here is a basic syntax of abs() function in Python:

				
					abs(number)
				
			

As you can see that we have used the abs keyword to define absolute function.

Followed by that inside the parenthesis, we have used the number as an argument for which we have to calculate the absolute value.

The point to note is that we can use only one argument inside the abs function.

Parameter of abs() Function

Let’s talk about the type of values, we can use in the parameter of abs() function in Python.

We can use 3 types of numbers inside the abs() function, which are an integer value, float value, and complex number.

Now, let’s take a look at the return type of abs() function with these 3 types of numbers.

Return Type of abs() Function

Here is the return type of absolute function in Python:

1. Integer Number – On using the integer value inside the abs() function, we get the integer number as a return type.

2. Float Number – On using the float value inside the abs() function, we get the floating-point number as a return type.

3. Complex Number – On using the complex number inside the abs() function, we get the magnitude of a complex number as a return type.

Now that you understood the theoretical part. Let’s take a dive into the practical section with the help of the below examples.

Best Course

-> Best Course for learning Python: Python for Everybody Specialization

Examples of Absolute Value In Python

Here are some of the basic examples which will help you in understanding the working of abs() function in Python:

Example 1: Calculating Absolute Value of Integer Number

Here is the basic example of Absolute Function with integer value in Python:

				
					iNum1 = 6
iNum2 = -4

absOfiNum1 = abs(iNum1)
absOfiNum2 = abs(iNum2)

print("Absolute Value of iNum1 is: ", absOfiNum1)
print("Absolute Value of iNum2 is: ", absOfiNum2)
				
			
Output:
Absolute Value of iNum1 is: 6
Absolute Value of iNum2 is: 4

As you can see from the above example that we have created 2 variables named “iNum1” and “iNum2” which holds the positive and negative integer value.

Then in the next line, we have used these variables as a parameter inside the abs() function and store the resultant absolute value inside another variable.

Finally, we printed the absolute value with the help of the print() method.

So, from the above result, we can see that the absolute value of 6 is 6. On the other hand, the absolute value of -4 is 4.

Now, let’s take a look at another example of an Absolute Function with a Floating Point Number.

Example 2: Calculating Absolute value of Floating Point Number

Here is the basic example of Absolute Function with Floating Point Number:

				
					fNum1 = 57.36
fNum2 = -74.26

absOffNum1 = abs(fNum1)
absOffNum2 = abs(fNum2)

print("Absolute Value of fNum1 is: ", absOffNum1)
print("Absolute Value of fNum2 is: ", absOffNum2)
				
			

Output:
Absolute Value of fNum1 is: 57.36
Absolute Value of fNum2 is: 74.26

You can see from the above example that we have created 2 variables for storing the positive and negative floating point numbers.

Then we have used the abs() function to convert these values into the absolute value.

At last, we printed the result on the screen with the help of the abs() method.

So, like the previous example, here also the absolute function didn’t change anything, if we use positive Floating Point Number as a parameter.

However, the negative Floating Point Number gets converted into the positive Floating Point Number with the help of the abs() function.

Now, here is the last and most important example of Absolute Value which is using Complex Numbers.

Example 3: Calculating Absolute Value of Complex Number

Here is the example to show the working of absolute function with complex number:

				
					cNum1 = (4 + 7j)
cNum2 = (3 - 5j)

absOfcNum1 = abs(cNum1)
absOfcNum2 = abs(cNum2)

print("Absolute Value of cNum1 is: ", absOfcNum1)
print("Absolute Value of cNum2 is: ", absOfcNum2)
				
			

Output:
Absolute Value of cNum1 is: 8.06225774829855
Absolute Value of cNum2 is: 5.830951894845301

With the help of the above example, you can understand that how easy it is to calculate the absolute value of a complex number with the abs() method.

We just have to put the Complex Number inside the abs() function and it will give you its absolute value.

Hope you like the article on Absolute Value in Python. We also have an article on Python String to Int and Int to String Conversion, which you must take a look at.

Leave a Reply