Introduction to Python Absolute Value
The absolute value of a number is the non-negative representation of that number. Like modulus, the absolute value of any number can never be negative. In programming, we come across multiple scenarios where we need to find the absolute value of a number. In Python, we can accomplish that either with a user-defined absolute function or with the help of the abs() function.
How does Absolute Value work in Python?
Below we have demonstrated how the absolute value works in Python:
1. User-Defined Absolute Function
Let us have a user-defined function and with the help of it, let us see how the absolute value works.
a. Passing a positive value
Code:
def absolute_value(number):
if(number<0):
abs_num = -number
else:
abs_num = number
return abs_num
print("The absolute value of 1 is {}".format(absolute_value(1)))
Output:
Code Explanation: Since we are passing 1, i.e., a positive number in the user-defined function absolute_value(), the flow of execution goes to the else part since the if a condition is not true, and hence we get the same number as the input.
b. Passing zero value
Code:
def absolute_value(number):
if(number<0):
abs_num = -number
else:
abs_num = number
return abs_num
print("The absolute value of 0 is {}".format(absolute_value(1)))
Output:
Code Explanation: Since we are passing 0, i.e. non-negative number in the user-defined function absolute_value(), the flow of execution goes to the else part since the if condition is not true, and hence we get the same number as the input.
c. Passing a negative value
Code:
def absolute_value(number):
if(number<0):
abs_num = -number
else:
abs_num = number
return abs_num
print("The absolute value of -1 is {}".format(absolute_value(1)))
Output:
Code Explanation: Since we are passing -1, i.e. negative number in the user-defined function absolute_value(), the flow of execution goes to the if part since the if condition is true, and hence we get the absolute value of the number that was given as input.
2. Python abs() Function
The abs() function helps us achieve to get the absolute value in Python.
Syntax:
abs(number)
i) Using abs() to find the absolute value of an integer
Code:
print("The absolute value of -1 is {}".format(abs(-1)))
Output:
Code Explanation: Since we are passing -1, i.e. negative number in the function abs(), we get the absolute value of the number that was given as input, i.e. 1.
ii) Using abs() to find the absolute value of a floating number
Code:
print("The absolute value of -4.2 is {}".format(abs(-4.2)))
Output:
Code Explanation: Since we are passing -4.2, i.e. negative floating-point number in the function abs(), we get the absolute value of the number that was given as input, i.e. 4.2
iii) Using abs() to find the absolute value of a complex number
Code:
print("The absolute value of 3+4j is {}".format(abs(3+4j)))
Output:
Code Explanation: For finding the absolute value of a complex number, we need to square the real and imaginary part of the complex number, add them and then take a square root of the result. Hence, we get the value 5.0.
iv) Using abs() to find the absolute value of a binary number
Code
bin_num = 0b0101010011
print("The absolute value is {}".format(abs(bin_num)))
Output:
Code Explanation: We can get the numerical representation of a binary number by using the abs() function. When we pass the binary number 0b0101010011 to the function abs(), we get the absolute value as 339.
v) Using abs() to find the absolute value of a octal number
Code:
oct_num = 0o75
print("The absolute value of is {}".format(abs(oct_num)))
Output:
Code Explanation: We can get the numerical representation of an octal number by using the abs() function. When we pass the octal number 0a75 to the function abs(), we get the absolute value as 61.
vi) Using abs() to find the absolute value of a hexadecimal number
Code:
hex_num = 0xFFC6F3
print("The absolute value of is {}".format(abs(hex_num)))
Output:
Code Explanation: We can get the numerical representation of a hexadecimal number by using the abs() function. When we pass the hexadecimal number 0xFFC6F3 to the function abs(), we get the absolute value as 16762611.
Conclusion
In this article, we have discussed the absolute value in general, mathematical interpretation of it. Moreover, we defined a custom function to find the absolute value of positive, zero and negative numbers. Thereby we understood how the absolute function works. Lastly, we discussed the abs() function in Python, which is used to find the absolute value, looked at its syntax, and saw three examples of finding the absolute value of an integer, floating and complex numbers, respectively.
Recommended Articles
This is a guide to Python Absolute Value. Here we discuss the introduction and how does absolute value work in Python?. You may also have a look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses