Python built-in Method - abs()

www.ig‮aeditfi‬.com

The abs() method is a built-in function in Python that returns the absolute value of a number. The absolute value of a number is its distance from zero, regardless of whether it is positive or negative.

Here is the syntax for abs() method:

abs(num)

where num can be any number (integer, float, complex number).

Here are some examples of how to use abs():

print(abs(-5))   # Output: 5
print(abs(5))    # Output: 5
print(abs(3.14)) # Output: 3.14

You can also use abs() with complex numbers:

print(abs(3+4j)) # Output: 5.0

In this example, the abs() method returns the magnitude (distance from zero) of the complex number 3+4j, which is equal to the length of the hypotenuse of a right-angled triangle with sides 3 and 4.

The abs() method can be useful in many situations where you need to get the absolute value of a number, for example when calculating distances or magnitudes in scientific applications.