The abs() function in Python is a built-in function used to return the absolute value of a number. The absolute value of a number is its distance from zero on the number line, without considering the sign.
In other words, abs() converts negative numbers into positive numbers, while positive numbers remain unchanged.
This function works with integers, floating-point numbers, and complex numbers.
abs(number)
print(abs(10))
Output
10
Explanation
print(abs(-15))
Output
15
Explanation
-15 is converted into its positive value 15.print(abs(-7.5))
Output
7.5
Explanation
-7.5 is 7.5.print(abs(3 + 4j))
Output
5.0
Explanation
abs() returns the magnitude of the number.[ |a + bj| = \sqrt{a^2 + b^2} ]
For example:
[ \sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5 ]
temperature = -12
print("Temperature difference:", abs(temperature))
Output
Temperature difference: 12
Explanation
abs() function gives the distance from zero, ignoring the negative sign.abs() is a built-in Python function used to find the absolute value of a number.