Looking for python absolute value? Or working with numbers in python?

Need to compare the magnitude of two numbers irrespective of their sign?

This article will guide you on how to find the absolute value of a number in Python with different numeric data types.

For example, a Magnitude of -10 is greater than the magnitude of 1. But when -10 and 1 are compared, 1 is declared as a greater number due to its positive sign.

Likewise, to compare the magnitude of the two numbers.

  • First, we need to find the absolute value of the numbers.
  • And then we finally compare the absolute values to compare the magnitude of the numbers.

How to calculate the absolute value of a number in Python?

We can calculate the absolute value of any number in python using the abs() function. The abs() function takes a number as its only argument and returns the absolute value of that number.

The input argument can be a floating-point number, an integer, or a complex number. We can also pass a binary number, an octal number or a hexadecimal number as input to abs() function.

We can understand the abs() function’s working to find the absolute value of a number in Python from the examples below.

Python Absolute Value of a Number – Using abs() function

You can find an absolute value of an integer using the abs() function as follows.


myNum=10
absoluteVal=abs(myNum)
print("Absolute value of {} is {}.".format(myNum,absoluteVal))

myNum=-10
absoluteVal=abs(myNum)
print("Absolute value of {} is {}.".format(myNum,absoluteVal))

Output:

Absolute value of 10 is 10.

Absolute value of -10 is 10.

You can find the absolute value of a floating-point number using the abs() function as follows.


absoluteVal=abs(myNum)
print("Absolute value of {} is {}.".format(myNum,absoluteVal))

myNum=-10.5
absoluteVal=abs(myNum)
print("Absolute value of {} is {}.".format(myNum,absoluteVal))

Output:


Absolute value of 10.5 is 10.5.

Absolute value of -10.5 is 10.5.

Moreover, to find the magnitude of a complex number, we can do it using the abs() function. The abs() function takes a complex number as input and returns the magnitude of the complex number as follows.


myNum=3+5j

absoluteVal=abs(myNum)

print("Absolute value of {} is {}.".format(myNum,absoluteVal))

Absolute value of (3+5j) is 5.83095189.

We can also determine the absolute value of a number in the decimal number system using the abs() function if the number has been represented in a binary, octal or hexadecimal number system as follows.


#hexadecimal number

myNum=0x123
absoluteVal=abs(myNum)
print("Absolute value {} is {}.".format(myNum,absoluteVal))

#binary number

myNum=0b1001
absoluteVal=abs(myNum)
print("Absolute value of {} is {}.".format(myNum,absoluteVal))

#octal number

myNum=0o123
absoluteVal=abs(myNum)
print("Absolute value of {} is {}.".format(myNum,absoluteVal))

Absolute value of 291 is 291.

Absolute value of 9 is 9.

Absolute value of 83 is 83.

Handling errors during the calculation of Python absolute value

Python is a programming language with dynamic typing. It means that the python interpreter determines the data type of a variable at the runtime.

Due to this, it is possible that when we pass a variable as input to the abs() function, it may not be having value with the correct data type to produce the correct output.

For example, when we pass a string as an input argument to the abs() function, The function will raise a TypeError exception as follows.


myNum="Aditya"

absoluteVal=abs(myNum)

print("Absolute value {} is {}.".format(myNum,absoluteVal))

Output:

Traceback (most recent call last):

File "/home/aditya1117/PycharmProjects/pythonProject/main.py", line 3, in <module>

absoluteVal=abs(myNum)

TypeError: bad operand type for abs(): 'str'

We know that when any part of a program raises an exception, the program is terminated immediately. This will result in loss of data written to the file or any significant calculation performed in the program.

We can avoid this loss by using exception handling using try-except blocks. The abs() function raises an exception whenever we pass a variable with an incorrect type as an input argument to the function. We can handle the exception, save necessary data, and close the files except block if needed.

Conclusion

In this article, We have studied using the abs() function in python to find absolute values of numbers. We have also studied how to find the absolute value of numbers in binary, octal and hexadecimal numbers in a decimal number system. Finally, we saw how to handle any exception generated by the abs() function using try-except blocks

We hope this tutorial helped you understand – absolute value? Keep learning keep sharing. Follow us on Facebook and Instagram.