Online Tutorials & Training Materials | STechies.com
Register Login

Python Absolute Value abs()

|| || 0

Python Absolute Value abs()
Stechies

How to get Absolute value of a number in Python?

We can use abs() function in order to the absolute value in python of the any given number. If in case the input number is a complex number, then abs() method returns its magnitude.

Parameter of Python abs()

With the help of a single argument, Python abs() method enables you to find the absolute value of integers, floating numbers as well as the magnitude of complex numbers.

Syntax of Python abs()

To return the absolute value of any number, you can use the abs() function.

Return Value of Python abs()

You can return the absolute value of an integer or a floating number in Python. In case the number you enter is a complex number, abs() will return the magnitude of the number.

An example of a program to return the absolute value of an integer is as follows-

Input

#random integer

i = -36
print('The absolute value of -36 is:', abs(i))

Output

The absolute value of -36 is: 36         

If you wish to find out the absolute value of a floating number, the program remains the same. An example of that is-

Input

#random floating number

fn = -3.144
print('The absolute value of -3.144 is:', abs(fn))

Output

The absolute value of -3.144 is: 3.144

If you want to find the magnitude of a complex number, you can follow this example.

Input

#random complex number

cn = (3 - 5j)
print('The magnitude of 3 - 5j is:', abs(cn))

Output

The magnitude of 3 - 5j is: 5.830951894845301


Related Articles