In this Python tutorial, we will learn about Python Absolute Value. Also, we will cover these topics.

- Python Absolulte Value
- Python Absolute Value syntax
- Python Absolute Value of List
- Python Absolute Value Function
- Python Absolute Value Numpy
- Python Absolute Value of Complex Number
- Python Absolute Value Math
- Python Absolute Value Without ABS
- Python Absolute Value DataFrame

## Python Absolulte Value

Python absolute value of the number is the number’s distance from the 0 towards the right direction. In other words, only a positive number of a number is called absolute value. for example, the absolute value of -5 will be 5.

- In absolute method negative signs are ignored and only the number is considered.
- Using the python
**abs()**method we can find the absolute number of any integer, float and complex numbers. - The alternative method of abs() function could be by using python exponents. Apply exponent of 2 on a number and then apply 0.5 exponent on the result. This is a quick hack to find the absolute value without using abs() method in python.
- We have explained this hack in detail later in section.

Also, check: Python Pretty Print JSON

## Python Absolute Value syntax

In the previous section, we have understood what is absolute number is in python and we came across the **abs()** method in python using which we can find the absolute value of a number.

- In this section, we will learn about the syntax of python
method.`abs()`

- Python
methods accepts only one parameter and that is the number whose absolute value is to find.`abs()`

- You can provide value than can be integer, float or complex number. Here is the syntax of python absolute value.

`abs(value)`

## Python Absolute Value of List

List holds the information of multiple datatypes under one variable. In this section, we will learn about the python absolute value of the list.

- Usng python abs() method we will find the absolute value of each item in the list.
- Before using this method make sure that all the items in the list are either integer, float or complex numbers. Python abs() method do not work on other datatypes.
- In our example, we have created a list of values that are integer, float and complex. The result will be python absolute value of list.

**Source Code**:

In this code, we have created a list with numbers (integers, float, and complex), and then using abs() method we have converted these values inside the list. We have passed the list through a for loop and stored the value in an empty list and then printed that list as a result.

```
list_without_abs = [1, 2.3, -4, -2.71j]
print('List without Absolute Value: ', list_without_abs)
list_with_abs = []
for item in list_without_abs:
list_with_abs.append(abs(item))
print("List with Absolute value",str(list_with_abs))
```

**Output:**

In this output, values inside of the list are converted to absolute values. Absolute captures only the value and ignores the sign. The final result has no negative value in it.

Read: Square Root in Python

## Python Absolute Value Function

In this section, we will learn about the python absolute value function. The python **abs()** function is used to find the absolute value of a number.

- Absolute function ignores the sign on the number and returns the number only. In other words, if negative value is passed then it will return the positive value of it.
- The value passed in python abs() function can be integer, float or complex.
- In our example we have created a function that will ask for a number from the user and then it will return the absolute value of that number.

**Source Code:**

In this code, we have created a function that accepts a value from the user. The program is put inside that python exception handling blocks. The function returns an absolute value of the number. Also, we have called this function multiple times to make things more understandable.

```
def findabsolute(val):
try:
return print("Result:",abs(val))
except ValueError:
return print("Only integer, float or complex values allowed")
#complex number
findabsolute(-2.7j)
findabsolute(2.71j)
#integer number
findabsolute(6)
findabsolute(-6)
#float number
findabsolute(7.4)
findabsolute(-7.4)
# string
findabsolute('United States of America')
```

**Output:**

In this output, we have displayed the result for each datatype (integer, float, and complex). The program threw an error when we have passed the python string value in the function.

Read: Python Number Guessing Game

## Python Absolute Value Numpy

Using ** numpy.absolute()** module in python numpy we can find the absolute value of the number. We have written a detailed blog on this topic Python NumPy absolute value with examples.

## Python Absolute Value of Complex Number

A complex number is the combination of a real number and an imaginary number. In this section, we will learn how to find python absolute value of complex numbers.

- You might be wondering what is the imaginary number in the complex number. Immaginary numbers are those that are still unsolved in mathematics. And these numbers are represented by adding
**j**or**J**after the real number(s). - Example of complex number could be value of PI (π) = 3.1428514 and it keep on going. So to simplify this we can write in this way
**3.14+0j** - In our example, we will convert complex number into an absolute value in python.

**Source code:**

In this code, we have asked for user input in the form of complex numbers and then this number is converted into absolute value using the python built-in **abs()** method.

```
num = complex(input('Enter Complex Number: '))
print(type(num))
print("Result:",abs(num))
```

**Output:**

In this output, the program is run 2 times. In the first time, we have input 2+3j and the positive result is displayed and the 2nd time we have provided -2+3j. This value is similar to the previous one but it has a negative sign but the absolute value is the same as the first one. the negative sign is ignored.

Read: If not condition in python

## Python Absolute Value Math

Python Math module provides a wide variety of methods that help in solving mathematical problems in python. In this section, we will learn about python absolute value using the math module.

- Using
**fabs()**method in python math module we can calculate the absolute value of a number. - first step in the process is to import the python built-in math module.

```
import math
print("result:",math.fabs(-21))
```

**Output:**

In this output, user has entered -21 and as a result 21.0 is return by math.fabs() module in python.

Read: Python Return Function

## Python Absolute Value Without ABS

In this section, we will learn about python absolute value without abs function.

- There are multiple ways of finding absolute value without using python built-in abs() method.
- These other ways are called hacks and they do not guarantee to work in all the conditions.
- You can use if-else ladder to print the positive value only for the number as that is also the the task that complex number do but it may not work with complex number.
- Other way could be 2 raise to the power of the number and perform 0.5 raise to the power of the result. This way in the first part negative sign is removed and in the second part the number is restored to its original value.

**Source Code:**

In this code, we have created a function manual_abs(). This function accepts a number as an input and returns the absolute value of a number. It first calculates the 2 raise the power of number and then 0.5 raise to the power result.

```
def manual_abs(num):
return (num**2)**(0.5)
print(manual_abs(-5))
```

**Output:**

In this output, -5 is entered as a parameter and the result 5.0 is produced as an output.

Read: Python find index of element in list

## Python Absolute Value DataFrame

In this section, we will learn about python absolute value dataframe. For demonstration, we have created a dataset for Traffic Incident report. Since Absolute value can be calculated only of numbers (integer, float, complex) so we will be performing operations on Latitude and Longitude columns only. So in other words, we will cover the Python Absolute Value of Column.

- Using
`pandas.DataFrame.abs`

method in Python Pandas we can calculate the absolute value of column. - This function can be applied only on the columns with numeric values.

**Source Code:**

In this source code, we have used a dataset ‘traffic_incident_report.csv’ that is downloaded from Kaggle website. we have modified this dataset as per our requirement.

**# import module**
import pandas as pd
**# read csv file**
df = pd.read_csv('Traffic_incident_report.csv')
**# absolute value of Latitude column**
lat = df.Latitude.abs()
**# absolute value of Longitute column**
long = df.Longitude.abs()
# adding column to exsiting dataframe
df['abs_lat'] = lat
df['abs_long'] = long
**# display **
df

**Output:**

In this output, we have implemented the program on the jupyter notebook. In case you are not using a jupyter notebook then use `print(df)`

it to display information. Here we have calculated the absolute value of the latitude and longitude column and then added that column into the existing dataframe.

You may also like to read the following Python tutorials.

- Python find number in String
- Python Tkinter Separator
- How to Convert Python DataFrame to JSON
- Remove non-ASCII characters Python
- Python convert binary to decimal
- Python Dictionary duplicate keys
- How to get unique values from a list in Python

In this tutorial, we have learned about python absolute value. Also, we have covered these topics.

- Python Absolulte Value
- Python Absolute Value syntax
- Python Absolute Value of List
- Python Absolute Value Function
- Python Absolute Value Numpy
- Python Absolute Value of Complex Number
- Python Absolute Value Math
- Python Absolute Value Without ABS
- Python Absolute Value DataFrame

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.