Home » Python Tutorial » Absolute Value in Python

# Absolute Value in Python

All real numbers (whether an integer or a decimal) can be positive or negative. But in situations where we deal with only non-negative (positive) values; then the term absolute value comes in. The absolute value of a number is the value of the number without considering its sign. Of course, a program can be written to return the absolute value in python.

Basically, there exists a built-in python function called the absolute function (written as abs ()) that returns the value of a number without considering the sign. In this tutorial, we will be discussing how to use this function to return the absolute value for an integer, floating-point number, and a complex number.

So, come on this journey with me.

## The abs() Function in Python

Absolute values of numbers in python can be gotten by using the abs() function. The function takes an argument of the number you wish to find its absolute value. It could be an integer, a floating number, or a complex number. If for instance, you pass the number -100 as the argument of the abs() function, it returns the absolute value which is 100. The process of determining the However, this process does not go for complex numbers. The reason will be discussed in the next section.

The abs() method is a method that returns the absolute value of any number. In the case of a complex number, the abs() method returns the magnitude of the number. Looking at the abs() parameters, the abs() method takes only one argument which is the number whose is to be returned.

Considering the return value from the abs(), the abs() method returns the absolute value of the given number. The integer absolute value and floating absolute value are returned for the integers and floating numbers respectively.

In the case of complex numbers, the abs() function works in a slightly different way which will be discussed momentarily. Let’s start with integers.

## Python Code for Finding the Absolute Values in Integers and Floats

In this section, we are going to write python codes on how to find the absolute values for integers, floating numbers, and complex numbers using the abs() method.

``````# A Python code to determine the absolute value of an integer using the abs() built-in function.

#choose a negative random number, say -3
integer = -3

#call the abs() function to determine the absolute value
abs_num = abs(integer)

#print the result
print('The absolute value of the integer is:', abs_num)``````

Output:

``````The absolute value of the integer is: 3
``````

The same thing goes for floating-point numbers. Let’s see an example.

``````# A Python code to determine the absolute value of a floating number using the abs() built-in function.

#select a random number
floating_number = -1.50

#call the absolute number
abs_num = abs(floating_number)

#Finally, we print the result
print('The absolute value of the floating number is:', abs_num)``````

## Python Code for Finding the Absolute Values in Complex Numbers

The absolute value of a complex number is the modulus of the number. Just like in a complex number, the modulus is the square root of the sum of the squares of the real and imaginary part. If we take a complex number say 12 – 5j, the modulus is the square root of 122 + 52. That is the square root of 169 which is 13.

``````#A Python code to determine the absolute value of a complex number using the abs() built-in function.

#define a complex number
complex_number = 12 - 5j

#Then, we use the abs() method to determine the absolute value
abs_num = abs(complex_number)
#Finally, we print the result
print('The absolute value of the complex number is:', abs_num)
``````

Output:

``The absolute value of the complex number is: 13.0``

To wrap up, you have understood what absolute values are and how to find the absolute value of a number in python using the abs() function. One thing to note is the process of finding the absolute value of an integer and float is quite different from a complex number.

## Some FAQs

How to do absolute value in python?

You can simply find the absolute value of a number by calling the abs() function and passing the number as argument.

Shares