# Python abs() Method

## abs() in Python

Python abs() function is used to return the absolute value of a number, i.e., it will remove the negative sign of the number.

Syntax: abs(number)
number: Can be an integer, a floating-point

number or a complex number

The abs() takes only one argument, a number whose absolute value is to be returned. The argument can be an integer, a floating-point number, or a complex number.

• If the argument is an integer or floating-point number, abs() returns the absolute value in integer or float.
• In the case of a complex number, abs() returns only the magnitude part and that can also be a floating-point number.

## What does the abs() function do in Python?

The abs() function make any negative number to positive, while positive numbers are unaffected. The absolute value of any number is always positive. For any positive number, the absolute value is the number itself and for any negative number, the absolute value is (-1) multiplied by the negative number.

### Example 1: Python abs() function example

In this example, we pass float, int, and complex data into abs() function and it will return absolute value.

 `# Python code to illustrate` `# abs() built-in function` `# floating point number` `float` `=` `-``54.26` `print``(``'Absolute value of float is:'``, ``abs``(``float``))` `# An integer` `int` `=` `-``94` `print``(``'Absolute value of integer is:'``, ``abs``(``int``))` `# A complex number` `complex` `=` `(``3` `-` `4j``)` `print``(``'Absolute value or Magnitude of complex is:'``, ``abs``(``complex``))`

Output:

```Absolute value of float is: 54.26
Absolute value of integer is: 94
Absolute value or Magnitude of complex is: 5.0```

### Example 2: Absolute value Python, Distance calculation

This equation shows the relationship between speed, distance traveled and time taken:

Speed is distance divided by the time taken.

And we know speed, time and distance are never negative, for this we will use abs() methods to calculate the exact time, distance, and speed.

Formula used:

```Distance  = Speed * Time
Time = Distance / Speed
Speed = Distance / Time```

Examples:

```Input : distance(km) : 48.5  time(hr) : 2.6
Output : Speed(km / hr) : 18.653846153

Input : speed(km / hr) : 46.0  time(hr) : 3.2
Output : Distance(km) : 147.2

Input : distance(km) : 48.5  speed(km / hr) : 46.0
Output : Time(hr) : 1.0543```

Code:

 `# Python3 Program to calculate speed,` `# distance and time` `# Function to calculate speed` `def` `cal_speed(dist, time):` `    ``print``(``" Distance(km) :"``, dist)` `    ``print``(``" Time(hr) :"``, time)` `    ``return` `dist ``/` `time` `# Function to calculate distance traveled` `def` `cal_dis(speed, time):` `    ``print``(``" Time(hr) :"``, time)` `    ``print``(``" Speed(km / hr) :"``, speed)` `    ``return` `speed ``*` `time` `# Function to calculate time taken` `def` `cal_time(dist, speed):` `    ``print``(``" Distance(km) :"``, dist)` `    ``print``(``" Speed(km / hr) :"``, speed)` `    ``return` `speed ``*` `dist` `# Driver Code` `# Calling function cal_speed()` `print``(``" The calculated Speed(km / hr) is :"``,` `      ``cal_speed(``abs``(``45.9``), ``abs``(``2.0``)))` `print``("")` `# Calling function cal_dis()` `print``(``" The calculated Distance(km) :"``,` `      ``cal_dis(``abs``(``62.9``), ``abs``(``2.5``)))` `print``("")` `# Calling function cal_time()` `print``(``" The calculated Time(hr) :"``,` `      ``cal_time(``abs``(``48.0``), ``abs``(``4.5``)))`

Output:

``` Distance(km) : 45.9
Time(hr) : 2.0
The calculated Speed(km / hr) is : 22.95

Time(hr) : 2.5
Speed(km / hr) : 62.9
The calculated Distance(km) : 157.25

Distance(km) : 48.0
Speed(km / hr) : 4.5
The calculated Time(hr) : 216.0```

Last Updated on October 28, 2021 by admin

## How to Convert images to NumPy array?How to Convert images to NumPy array?

How to Convert images to NumPy array? Images are an easier way to represent the

## Python set operations (union, intersection, difference and symmetric difference)Python set operations (union, intersection, difference and symmetric difference)

Python set operations (union, intersection, difference and symmetric difference) This article demonstrates different operations on Python

## Add column names to dataframe in PandasAdd column names to dataframe in Pandas

In this tutorial, we will learn how to add column names to a dataframe in

## Python Try ExceptPython Try Except

Python Try Except Error in Python can be of two types i.e. Syntax errors and

## Python – Pandas DataFrame.where()Python – Pandas DataFrame.where()

Python – Pandas DataFrame.where() Python is a great language for doing data analysis, primarily because

## Python PIL getpixel() MethodPython PIL getpixel() Method

Python PIL | getpixel() Method PIL is the Python Imaging Library which provides the python

## Creating a Pandas Series from ListsCreating a Pandas Series from Lists

Creating a Pandas Series from Lists   A Series is a one-dimensional labeled array capable

## Python – Get first and last elements of a listPython – Get first and last elements of a list

Python | Get first and last elements of a list Sometimes, there might be a