Python abs() function



The Python abs() function returns the absolute value of a number. In simple words, an absolute value of a number 'x' is calculated as the (positive) distance between x and 0.

This function accepts all types of numbers, both real and complex numbers, as arguments. The absolute value is concerned with the value or magnitude of a number instead of the sign attached to the number. That means, if a number is a positive value, the function returns the same value; but if the number is a negative value, the negation of this number is returned. Hence, it is even more useful for complex numbers where the calculation of magnitude involves many steps.

Syntax

Following is the syntax for the Python abs() function −

abs( x )

Parameters

  • x − This is a numeric value.

Return Value

This function returns absolute value of x.

Example

The following example shows the usage of the Python abs() function. In here, let us try to calculate the absolute values of positive real numbers.

# Create positive Integer and Float objects
inr = 45
flt = 100.12

# Calculate the absolute values of the objects
abs_int = abs(inr)
abs_flt = abs(flt)

# Print the values
print("Absolute Value of an Integer:", abs_int)
print("Absolute Value of an Float:", abs_flt)

When we run above program, it produces following result −

Absolute Value of an Integer: 45
Absolute Value of an Float: 100.12

Example

As we have already discussed, the absolute value only considers the magnitude of a number. Therefore, in this example, we are creating number objects with negative values and passing them as arguments to the abs() function to calculate their absolute values.

# Create negative Integer and Float objects
inr = -34
flt = -154.32

# Calculate the absolute values of the objects
abs_int = abs(inr)
abs_flt = abs(flt)

# Print the values
print("Absolute Value of an Integer:", abs_int)
print("Absolute Value of an Float:", abs_flt)

Let us compile annd run the program above, the output is displayed as follows −

Absolute Value of an Integer: 34
Absolute Value of an Float: 154.32

Example

If we pass a complex number as an argument to this function, the return value will be the magnitude of this complex number.

In the following example, we are creating two objects holding complex numbers, one positive and the other negative. Using the abs() function, the absolute values of these objects are calculated.

# Create positive and negative complex number objects
pos_cmplx = 12-11j
neg_cmplx = -34-56j

# Calculate the absolute values of the objects created
abs1 = abs(pos_cmplx)
abs2 = abs(neg_cmplx)

# Print the return values
print("Absolute Value of a positive complex number:", abs1)
print("Absolute Value of a negative complex number:", abs2)

Compile and run the program above, and the output is obtained as given below −

Absolute Value of a positive complex number: 16.278820596099706
Absolute Value of a negative complex number: 65.5133574166368

Example

When a None value is passed as an argument to the function, a TypeError is raised. But when we pass a zero as the argument, the function returns zero as well.

# Create negative Integer and Float objects
zero = 0
null = None

# Calulate and Print the absolute values
print("Absolute Value of Zero:", abs(zero))
print("Absolute Value of a Null:", abs(null))

On executing the program above, the result is displayed as follows −

Absolute Value of Zero: 0
Traceback (most recent call last):
  File "main.py", line 7, in 
print("Absolute Value of a Null:", abs(null))
TypeError: bad operand type for abs(): 'NoneType'
python_built_in_functions.htm
Advertisements