AppDividend
Latest Code Tutorials

Python Absolute Value Tutorial With Example | Python abs() Function

0

Python Absolute Value Tutorial With Example | Python abs() Function is today’s topic. Python abs() method returns the absolute value of the given number argument. If the number is a complex number, abs() method returns its magnitude. Python abs() takes only one argument. The parameter can be the integer, a floating point number or a complex number. If an argument is an integer or floating point number, abs() returns the absolute value in integer or float. In the case of a complex number, abs() returns only the magnitude part, and that can also be a floating point number.

Python Absolute Value Tutorial

Python abs() method syntax is following.

abs(n)

The n is the required parameter, and it is the number. It can be a float or a complex number.

See the following example.

# app.py

x = abs(-19.21)

print(x)

The output is following.

Python Absolute Value Tutorial

 

Python abs() with complex number

Let’s see the case where we pass the complex number as an argument. See the following code.

# app.py

y = abs(19 + 21j)

print(y)

The output is following.

Python abs() Function

 

In the above example, we get the magnitude of the complex number.

You can get the magnitude of complex number z = x + yj using the following formula.

\textstyle r=|z|=\sqrt{x^2+y^2}.\,

The abs() method returns the absolute value of the given number.

  1. For integers, the absolute integer value is returned.
  2. For floating numbers, the floating absolute value is returned.
  3. For complex numbers,  the magnitude of the number is returned.

Conclusively, Python Absolute Value Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.