free stats

Python abs() Function

Last Update : 09 Dec, 2022 Python, Programming, Example

This is a Python built-in function tutorial. In this tutorial, you will teach to use the Python abs() function with examples.

The Python abs() function return the absolute value of the given number and also removes the negative sign of a number in Python. 

When the given number is a complex number, the abs() function returns the magnitude of a given complex number.

 

Python abs() Function Syntax

The following shows the syntax of the abs() function.

Syntax: abs(number)

This function takes a single argument.

number -: This is a required argument. This argument accepts an Integer, a floating-point number, or a complex number.

 

Python abs() Function Example

 

Example 01: Get the absolute value of an integer number

In the following example, we pass the int value into the abs() function. Then, the function returns the absolute value for the provided input value.

# An integer value
x = -100
print('Absolute value is:', abs(x))

This program produces the following result -:

Absolute value is: 100

 

Example 02: Get the absolute value of a floating point number

In the following example, we pass the floating point value into the abs() function. Then, the function returns the absolute value for the provided input value.

# A floating point value
float_num = -55.44
print('Absolute value is:', abs(float_num))

This program produces the following result -:

Absolute value is: 55.44

 

Example 03: Get the absolute value of a complex number

In the following example, we pass Python complex data value into the abs() function. Then, the function returns the absolute value for the provided input value.

# A complex number
complex_num = (4 - 5j)
print('Absolute value is:', abs(complex_num))

This program produces the following result -:

Absolute value is: 5.0

 

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.