Files
freeCodeCamp/guide/english/python/abs-function/index.md
Shane 522fce544e add return value which might be 0 (#20657)
if the input in abs is 0, then the return can't always be a positive value. 0 is not a positive value
2018-10-31 20:52:54 -05:00

1.3 KiB

title
title
Python Abs Function

abs() is a built-in function in Python 3, to compute the absolute value of any number. The absolute value of a number "means only how far a number is from 0" 1 It takes one argument x. The argument can even be a complex number, and in that case its modulus is returned.

Argument

It takes one argument x - an integer, or decimal, or a complex number.

Return Value

The return value would be a positive number or zero. Even if complex number is passed, it would return its magnitude, computed as per complex number algebra.

Code Sample

print(abs(3.4)) # prints 3.4
print(abs(-6)) # prints 6
print(abs(3 + 4j)) # prints 5.0, because |3 + 4j| = 5

🚀 Run Code

Official Docs

Sources

  1. Math Is Fun. Accessed: October 25, 2017