(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Python abs() function
Updated on Jan 07, 2020
The abs()
function returns the absolute value (just magnitude without sign) of the number.
Its syntax is as follows:
abs(x) -> absolute value
Parameter | Description |
---|---|
x |
Any numeric value |
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>>
>>> abs(-45)
45
>>>
>>>
>>> abs(-3.14)
3.14
>>>
>>>
>>> abs(10)
10
>>>
>>>
>>> abs(2+4j)
4.47213595499958
>>>
|
The result is quite obvious for integers and floats. In case of a complex number
z = x + yi
the abs()
function calculates the absolute value as follows:
Absolute value = |z| = √x² + y²
1 2 3 4 5 6 7 | => 2+4j
=> √2² + 4²
=> √4 + 16
=> √20
=> 2√5
=> 2*2.236
=> 4.472
|
Other Tutorials (Sponsors)
This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!
View Comments