Python

Python Absolute Value

Python Absolute Value

Numbers are a very important aspect of any programming language. The numbers include integer, floats, and complex numbers. The numbers can be positive or negative. Sometimes we need to get the absolute value of a number to perform mathematical operations like addition, subtraction, etc. The absolute value denotes the non-negative value of a given number.  Python provides a built-in abs() function to find the absolute value of a number. This article explains the use of the abs() function with simple examples.

Syntax of abs() function

The abs() function takes the number and returns the absolute value. The syntax of the abs() function is as follows:

abs(number)

The number could be an integer, floating-point number, or complex number. In the case of a complex number, the abs() function yields the magnitude value.

Finding the absolute value of integers and floating-point number

Now let's use the abs() function to find the absolute value of integers and floating-point numbers.

#Python program to find the absolute value
#using abs() function
print("The absolute value of -20 is:",abs(-20))
print("The absolute value of -40.5 is:",abs(-40.5))
print("The absolute value of -10.9867 is:",abs(-10.9867))
print("The absolute value of 50.09 is:",abs(50.09))
print("The absolute value of -101.98 is:",abs(-101.98))
print("The absolute value of 30.08 is:",abs(30.08))
print("The absolute value of -200 is:",abs(-200))
print("The absolute value of -10 is:",abs(-10))
print("The absolute value of -90 is:",abs(-90))

Output

The output displays the absolute value of various numbers.

The abs() function only takes the number as an argument. If we pass any character or string value as an argument, then the Python interpreter throws an error “bad operand type for abs()”.

#Python program to find the absolute value
#using abs() function
print(abs("a"))

Output

Now let's find the absolute value of two numbers and calculate the sum.

#Python program to find the absolute value
#using abs() function
#decalring first number
num1 = -10
#declaring second number
num2 = -20.04
#printing the sum

Output

Finding the absolute value of complex numbers

As we previously discussed, in the case of a complex number, the abs() function returns the magnitude value. Let's use the abs() function to find the magnitude of a complex number in our script.

#Python program to find the absolute value
#using abs() function
num = -4-3j
print("The magnitude of -4-3j is: ",abs(num))
num = 4+3j
print("The magnitude of 4+3j is: ",abs(num))
num = 5-7j
print("The magnitude of 5-7j is: ",abs(num))
num = -1+6j
print("The magnitude of -1+6j is: ",abs(num))

Output

Conclusion

The abs() function calculates and returns the absolute value of a given number. In the case of complex numbers, the abs() function return the magnitude value. This article demonstrates the use of the abs() function with examples.

Battle For Wesnoth 1.13.6 Development Released
Battle For Wesnoth 1.13.6 released last month, is the sixth development release in the 1.13.x series and it delivers a number of improvements, most no...
League of Legendsin asentaminen Ubuntu 14 een.04
Jos olet League of Legendsin fani, tämä on sinulle mahdollisuus testata League of Legendsia. Huomaa, että PlayOnLinux tukee LOLia, jos olet linux-käyt...
Asenna uusin OpenRA-strategiapeli Ubuntu Linuxiin
OpenRA on ilmainen / ilmainen reaaliaikainen strategiapelimoottori, joka luo uudet Westwood-pelit, kuten klassinen Command & Conquer: Red Alert. Hajau...