# Absolute Value in Python Using abs() Function With Example

## Getting Started

The abs() function is one of built-in functions in Python. In Python, abs() function is used to return the absolute value of a number taking as parameter.

Number can be of different types –

• Integer Number: 1, 2, 3, 4, -1, -2, -3 etc.
• Float Number: 1.2, -1.4, 2.0, -2.0 etc.
• Complex Number: It means number is in the form of a + bj, where a and b are either integer number or float number. When the parameter is a complex number, its magnitude is returned.

## Syntax of abs() Function

The syntax of abs() function in Python is shown below.

```abs(number)
```

We can see through this syntax that the abs() function takes a number as parameter. So, it is compulsory that we pass only a number as parameter to abs() function. Otherwise, we will get a TypeError as shown below.

```print(abs("mother"))
```

Output

```TypeError: bad operand type for abs(): 'str'
```

Here, we got TypeError as output because “mother” is a string object and not a number.

This output will be the same for any parameter of abs() function which is not a number.

## Parameters of abs() Function

As shown above in the syntax, abs() function takes only a single parameter and clearly one number. Number can be an integer number or floating number or complex number. Parameter must be the number whose absolute value we want.

## Return type of abs() Function

In Python, abs() function returns the absolute value according to the type of the given parameter. Thus,

• If given parameter is an integer, integer absolute value is returned.
• If parameter or argument is a float object, floating absolute value is returned.
• If the argument is neither integer nor float object but is a complex number, the magnitude of this complex number is returned as output.

## Examples of abs() Function in Python

Now, we will see how to use absolute value in python using abs() function with example for – integer number, float number and complex number.

• ### Absolute Value of Integer Number

Integer Number can be positive or Negative. Absolute value of both types of integer number can be obtained using abs() function in python as show below –

### Example 1

```number = 10
print(abs(number))
```

Output:

````10
```

The output is equal to 10 because number is a positive integer and also abs() function returns only positive value or absolute value. That’s why the variable number and the output are the same as shown above.

#### Example 2

```number = -30
print(abs(number))
```

Output:

````30
```

In this example, we are trying to get the absolute value the variable number that equal to -30. So the variable number is a negative integer. The output of this Python code is equal to 30 because the abs() function returns the absolute value. It means that the abs() function returns only positive value if parameter is positive or negative and is an integer or float object.

• ### Absolute Value Of Float Number

Float number can be of two types – positive float number and negative float number. Absolute value of both types of float number be obtained using abs() function in python as shown below –

#### Example 3

```number = 30.33
print(abs(number))
```

Output

```30.33
```

Here the output won’t be different from the value of the variable number because as we said above abs() function returns absolute value so positive value and in our case here variable number is a positive floating object. So we won’t observe changement after we apply abs() function.

### Example 4

```number = -40.33
print(abs(number))
```

Output

```40.33
```

In this Python code we are trying to get the absolute value of a negative floating object. And as you know the output must be a positive floating object because abs() function returns only absolute or positive value. That is the reason why we got 40.33 as output.

• ### Absolute Value of Complex Number

#### Example 5

```complex_number = (3 - 4j)
print(abs(complex_number))
```

Output

```5.0
```

Here, our purpose is to understand why we got 5.0 as output. First of all if we consider two integers a and b. The magnitude of integers a and b is equal to the positive square root a^2 + b^2. So, in our case, the magnitude of complex number will equal to the positive square root of 3^2 + (-4)^2. It means positive square root of 25. That’s why we got this output.

That’s how we get absolute value in python using abs() function. Visit to learn more