# Python abs()

The `abs()` function in Python returns the absolute value of a given number, which means the `abs()` method removes the negative sign of a number.

If the given number is complex, then the `abs()` function will return its magnitude.

## abs() Syntax

The syntax of `abs()` method is

abs(number)

## abs() Parameters

The `abs()` function takes a single argument, a number whose absolute value is to be returned. The number can be

• integer
• floating-point number
• complex number

## abs() Return Value

`abs()` method returns an absolute value of a given number.

• For integers – absolute integer value is returned
• For floating numbers – absolute floating-point value is returned
• For complex numbers – the magnitude of the number is returned

## What does the abs() function do in Python?

The `abs()` function will turn any negative number into positive, the absolute value of a negative number is multiplied by (-1) and returns the positive number, and the absolute value of a positive number is the number itself.

## Example 1: Get absolute value of a number in Python

In this example, we will pass an integer, float into the `abs()` function, and returns the absolute value.

``````# Python code to illustrate abs() built-in function

# integer number
integer = -10
print('Absolute value of -10 is:', abs(integer))

# positive integer number
positive_integer =20
print('Absolute value of 20 is:', abs(positive_integer))

# floating point number
floating = -33.33
print('Absolute value of -33.33 is:', abs(floating))
``````

Output

``````Absolute value of -10 is: 10
Absolute value of 20 is: 20
Absolute value of -33.33 is: 33.33
Absolute value of 3-4j is: 5.0``````

## Example 2: Get the magnitude of a complex number

In this example, we will pass a complex number into the `abs()` function and returns the absolute value.

``````# Python code to illustrate abs() built-in function

# complex number
complex=(3-4j)
print('Absolute value of 3-4j is:', abs(complex))``````

Output

``Absolute value of 3-4j is: 5.0``

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

Table of Contents Hide What is SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape?How to fix SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes… ## How to Reverse a String in Python

Table of Contents Hide Reverse a string in PythonUsing extended slice operatorUsing for loopUsing while loopUsing recursionUsing reversed() method Python has many functions for string manipulation. However, the Python string… ## How to Filter List Elements in Python?

The filter() method filters the given sequence of elements with the help of a function that tests each element in the sequence to be true or not. Syntax: filter(function, sequence)… ## How to check if a file exists in Python?

Table of Contents Hide Python check if a file exists using OS ModuleUsing os.path.exists()Using os.path.isfile()Using os.path.isdir()Using pathlib.Path.exists() When you perform a file operation such as reading from a file or… ## Python Remove Newline From String

Table of Contents Hide Python Remove Newline From StringUsing strip() method to remove the newline character from a stringUsing replace() method to remove newlines from a stringUsing regex to remove… ## [Solved] NameError: name ‘pd’ is not defined

Table of Contents Hide Solution NameError: name ‘pd’ is not defined Method 1 – Importing pandas with Alias as pdMethod 2 – Importing all the functions from pandasMethod 3 –…