At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Python Absolute Value Function- The Complete Tutorial

  • By Murali Krishnan
  • March 26, 2020
  • 3408 Views

This is a short piece of writing about Python’s Absolute value function. Absolute value function abs() is one of the built-in functions in the Python builtins module which is quite easy to learn. Python Module is basically a python script file that comprises variables, functions, and classes. Python modules help us to organize the code and then referencing them in other classes or python scripts.
The abs() function [absolute value function] is used to return the absolute value of a number. The abs() takes only one argument as an input, a number whose absolute value is to be returned. The argument can be of any type. It can be a complex number, an integer or a floating-point number. In the cases when the argument is a float or integer, the abs() function returns an absolute value despite that it is an integer or float. When it comes to complex numbers, the magnitude of the number will be returned. 

  • Python Absolute Value using abs()
  • Python abs() with integers
  • Python absolute value of float
  • Python abs() with complex numbers
  • Python abs() with different format numbers

Python Absolute Value using abs()

Syntax 
The syntax of the abs() function is  abs(num).
Note: It can be a number or an expression that evaluates to a number.

ALSO READ: MongoDB In Golang With Examples – A Beginner’s Guide

Parameters
num- A number whose absolute value will be returned. 
The data type of this argument can be an integer, floating number or a complex number. 
Return Value
The abs() function returns the absolute value of the number which we passed as an argument. 
For an integer value, the abs() function returns the absolute value of the integer.
For floating values, the abs() function returns the absolute value of the floating number.
For Complex numbers, the function returns the absolute value of magnitude which can also be a floating-point number.
For numbers in different formats, abs() returns the absolute value in the decimal system even if numbers are defined in binary, octal, hexadecimal or in an exponential form.
Login into the python console. In the terminal, type the command python and click enter. It will open a python console. 

What is Console in Python?

The console is essentially a command-line interpreter that takes input from the user i.e. one command at a time and interprets it. This can also be called Shell. If the console is error-free, it runs the command. It should give the required output or displays an error. The Console should look like as shown below. 
Open the terminal and type python to access the python console.

Python abs() with integers

int = -94
print('The absolute value of the integer is:', abs(int))

Output:
The absolute value of the integer is: 94

Python absolute value of float

float = -54.26
print('Absolute value of integer is:', abs(float))

Output:
The absolute value of float is: 54.26

Python abs() with complex numbers

complex = (3 - 4j) 
print ('Absolute value or Magnitude of complex is:', abs(complex))

Output:
Absolute value or Magnitude of the complex is: 5.0
In the above example, we get the magnitude of the complex number.

ALSO READ: How To Develop A Healthcare Mobile Application For Hospitals

You can get the magnitude of complex number z = x + yj using the following formula.
r = |Z| = sqrt(x^2+y^2)

Python abs() with different format numbers

Exponential

x =  19.21e1/2
print(abs(x))

The Output is 96.05
Binary

y = 0b1100
print(abs(y))

The Output is 12
Octal

z = 0o11
print(abs(z))

The Output should be 12
Hexadecimal

w = 0xF
print(abs(w))

The Output will be 15 

Python absolute value of a list

Let us learn how to obtain the absolute value of numbers of a list.
Let’s say, I have a list of numbers that looks like the one below.

inputList = [21, 1, -19, 46]

We are using the map() and list() functions to convert it to the absolute value.

mappedList = map(abs, inputList)
print(list(mappedList))

The output will be [21, 1, 19, 46]
We had gone through a few quick examples of python absolute values of numbers using abs() function. By following the instructions in this article, you will find using the abs() function simple and easy. 
Do you find it interesting? you might also like these articles. Top 10 Best Tech Companies For Employees To Work In The USA In 2020 and Top 10 IT Staffing and Recruiting Agencies in the USA.
If you have a business idea in your mind and in search of a reliable web development company, you are in the right place. Hire the best web developers in the industry from Agira technologies.

Looking for a Tech partner to dominate the digital world?

Murali Krishnan

Murali is a Software Developer. He has great exposure in Python, AWS, Docker Tools, Linux, Networking and more. This tech enthusiast loves to share his ideas and guides with the readers.