Python abs() Function: Absolute Value Examples

Python abs()

Python abs() is a built-in function available with the standard library of python. It returns the absolute value for the given number. Absolute value of a number is the value without considering its sign. The number can be integer, floating point number or complex number. If the given number is complex, then it will return its magnitude.

Syntax:

abs(value)

Parameters: (value)

The input value to be given to abs() to get the absolute value. It can be an integer, a float, or a complex number.

Return Value:

It will return the absolute value for the given number.

Examples:

Code Example 1: Integer and Float number

To get the absolute value of an integer and float number check this code:

# testing abs() for an integer and float

int_num = -25

float_num = -10.50

print("The absolute value of an integer number is:", abs(int_num))
print("The absolute value of a float number is:", abs(float_num))

Output:

The absolute value of an integer number is: 25
The absolute value of a float number is: 10.5

Example 2: Complex Number

To get absolute value of complex number

# testing abs() for a complex number

complex_num = (3+10j)

print("The magnitude of the complex number is:", abs(complex_num))

Output:

The magnitude of the complex number is: 10.44030650891055

Summary:

 

YOU MIGHT LIKE: