# What is Python abs() function?

The abs() function returns the absolute value of the given number in Python. The abs() function takes an argument which is the number that you want to get the absolute value. The argument value can be:

• An integer number (negative or positive)
• A float number (negative or positive)
• A complex number

In the case of a complex number, the abs() function returns its magnitude.

What is absolute value? In mathematics, the absolute value is a non-negative value that describes only how far a number is from zero.

### The syntax of abs() Python function

The general syntax for using the abs() function is:

abs(x)

For example:

abs_num = abs(10)

abs_num = abs(15.5)

The third example in the next section shows how to use a complex number in abs() function.

### An example of using integer number in abs() function

In this example, I used a positive and a negative number in the abs() function to get the absolute value.

```#The abs() function demo

neg_abs_num = abs(-17)

pos_abs_num = abs(17)

print(neg_abs_num)

print(pos_abs_num)```

The output:

17

17

### Using floating numbers in abs() function

Now, I used a negative and a positive floating number in the abs() function to get the absolute value. See the output:

```#The abs() function demo

neg_flt_num = abs(-5.55)

pos_flt_num = abs(5.55)

print(neg_flt_num)

print(pos_flt_num)```

The result:

5.55

5.55

### An example of using a complex number

In this example of Python abs function, I used a complex number.

```#The abs() function demo with complex number

comlx_num = abs(3+9j)

print(comlx_num)```

The output:

9.486832980505138

What is a complex number? A number that can be expressed in the form of a +bj is a complex number. In that case, a and b are the real numbers while j is the imaginary unit. Learn more about complex number here.

### Using a binary number in abs() function

As such, the binary number is represented with 0b in Python. You may also convert a binary number to an absolute value by using the abs() function. The code below shows how:

```bin_num = abs(0b0101010001)

print(bin_num)```

The result of the above code:

337

### An example of converting octal number to the absolute value

Just like a number with prefix ‘0b’ is considered a binary number, a number with the prefix ‘0o’ is considered an octal number.

The example below converts octal numbers to the absolute number by using abs() function:

```#Python program to convert octal numbers

oct_num1 = abs(0o74)

oct_num2 = abs(0o577)

oct_num3 = abs(0o457)

oct_num4 = abs(0o456)

print("The abs number for 0o74" ,oct_num1)

print("The abs number for 0o577" ,oct_num2)

print("The abs number for 0o457" ,oct_num3)

print("The abs number for 0o456" ,oct_num4)```

The output of the above code:

### The example of converting the hexadecimal number to absolute value

Similarly, a number with prefix ‘0x’ is considered as hexadecimal number. The example below shows the absolute value for four hexadecimal numbers by abs() function:

```#Python program: convert hexadecimal numbers to abs

hex_num1 = abs(0xFFC6E2)

hex_num2 = abs(0xFFFFFF)

hex_num3 = abs(0x000000)

hex_num4 = abs(0x008080)

print("The abs number for 0xFFC6E2: " ,hex_num1)

print("The abs number for 0xFFFFFF: " ,hex_num2)

print("The abs number for 0x000000: " ,hex_num3)

print("The abs number for 0x008080: " ,hex_num4)```

The output:

Note: Python has another function in the math library namely math.fabs() for getting the absolute value, however, it always returns float value.

##### Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!