Home »
Python »
Python Reference »
Python Built-in Functions
Python abs() Function
By IncludeHelp Last updated : April 19, 2024
The abs() function is a library function in Python, it is used to get the absolute value of a given number. It accepts a number (can be an integer number, float number, or a complex number) and returns the absolute value of the given number.
Syntax
The following is the syntax of abs() function:
abs(number)
Parameter(s):
The following are the parameter(s):
- number – it can be an integer number, float number of a complex number.
Return Value
The abs() function returns an absolute value of the given number.
Examples
Example 1
Get the absolute values of different integer, float, and complex numbers.
# python code to demonstrate an example
# of abs() function
# integer number
iNum1 = 10
iNum2 = -10
print("Absolute value of ", iNum1, " is = ", abs(iNum1))
print("Absolute value of ", iNum2, " is = ", abs(iNum2))
# float number
fNum1 = 10.23
fNum2 = -10.23
print("Absolute value of ", fNum1, " is = ", abs(fNum1))
print("Absolute value of ", fNum2, " is = ", abs(fNum2))
# complex number
cNum1 = 10 + 5j
cNum2 = 10 - 5j
print("Absolute value of ", cNum1, " is = ", abs(cNum1))
print("Absolute value of ", cNum2, " is = ", abs(cNum2))
Output
Absolute value of 10 is = 10
Absolute value of -10 is = 10
Absolute value of 10.23 is = 10.23
Absolute value of -10.23 is = 10.23
Absolute value of (10+5j) is = 11.180339887498949
Absolute value of (10-5j) is = 11.180339887498949
Example 2
Get the absolute value of the difference of two numbers.
# Finding the difference of two
# numbers using abs() method
x = 10
y = 20
print("Difference between", x, "and", y, "is", abs(x - y))
Output
Difference between 10 and 20 is 10
Related Pages