I noticed that in python there are two similar looking methods for finding the absolute value of a number:

First

`abs(-5)`

Second

```import math
math.fabs(-5)```

How do these methods differ?

Sep 19, 2018 in Python 330 views

## 1 answer to this question.

math.fabs() converts its argument to float if it can (if it can't, it throws an exception). It then takes the absolute value, and returns the result as a float.

In addition to floats, abs() also works with integers and complex numbers. Its return type depends on the type of its argument.

```In [7]: type(abs(-2))
Out[7]: int

In [8]: type(abs(-2.0))
Out[8]: float

In [9]: type(abs(3+4j))
Out[9]: float

In [10]: type(math.fabs(-2))
Out[10]: float

In [11]: type(math.fabs(-2.0))
Out[11]: float

In [12]: type(math.fabs(3+4j))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/alexei/<ipython-input-12-8368761369da> in <module>()
----> 1 type(math.fabs(3+4j))

TypeError: can't convert complex to float```
• 58,100 points

+1 vote

## Difference between append vs. extend list methods in Python

append: Appends object at the end. x = ...READ MORE

## Difference for string comparison in Python: 'is' vs. ==

If we use "==" means both variables ...READ MORE

## Python vs Cpython

So what is CPython? CPython is the original ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## Lowercase in Python

You can simply the built-in function in ...READ MORE