The Python abs() function is used to return the absolute value of a number. The absolute function returns a positive number if the original number is negative.
Syntax:
abs (num)
Parameters
num: A number whose absolute value is to be returned, such as an integer, a floating number, or a complex number.
Return: It returns the absolute value of the specified number.
Python abs() Function Examples
abs() Function with an Integer Argument
We can use the abs() function to convert a negative integer into a positive integer.
Example
Let’s see an example, where we will see, a negative integer getting converted to a positive integer.
#integer number
neg_integer = -100
#finding the absolute value using the abs() function
print('Absolute value of -100 is:', abs(neg_integer))
Output:
Absolute value of -100 is: 100
abs() Function with a Floating-Point Number
Similarly, we can convert a negative floating number into a positive floating number by using the abs() function.
Example
Let’s see an example, where we will see, a negative floating-point number getting converted to a positive floating-point number.
# below is a floating-point number
float_number = -69.96
#finding the absolute value using the abs() function
print("Absolute value of float number", float_number, "is: ",abs(float_number))
Output:
Absolute value of float number -69.96 is: 69.96
abs() Function with a Complex Number
The complex number consists of a real part and an imaginary part. We can pass the complex number through the abs() function, and we will get an absolute value returned.
Example
Let’s see an example, where we will pass a complex number through abs() function and get an absolute value.
# complex number
complex_num = (5 + 12j)
#finding the absolute value using abs() function
print("Magnitude of complex number", complex_num,"is: ", abs(complex_num))
Output:
Magnitude of complex number (5+12j) is: 13.0
Time-Distance Calculation Using Python abs() Function
As we already know, the abs() function returns the positive value, and we are also aware that speed, distance, and time can’t be negative. So, we can use the abs() method to calculate the exact time, distance, and speed.
The formula we are going to use is as follows:
- Distance = Speed * Time
- Time = Distance / Speed
- Speed = Distance / Time
Example
# Functions for basic motion calculations
# Calculate speed
def find_speed(distance, hours):
print("Distance (km):", distance)
print("Time (hr):", hours)
speed = distance / hours
return speed
# Calculate distance
def find_distance(rate, hours):
print("Speed (km/hr):", rate)
print("Time (hr):", hours)
distance = rate * hours
return distance
# Calculate time
def find_time(distance, rate):
print("Distance (km):", distance)
print("Speed (km/hr):", rate)
time_required = distance / rate
return time_required
# Main Program
# Using absolute values to avoid negative inputs
print("Calculated Speed (km/hr):",
find_speed(abs(46.2), abs(-2.0)))
print()
print("Calculated Distance (km):",
find_distance(abs(-60.5), abs(3.0)))
print()
print("Calculated Time (hr):",
find_time(abs(50.0), abs(5.0)))
Output:
Distance (km): 46.2
Time (hr): 2.0
Calculated Speed (km/hr): 23.1
Speed (km/hr): 60.5
Time (hr): 3.0
Calculated Distance (km): 181.5
Distance (km): 50.0
Speed (km/hr): 5.0
Calculated Time (hr): 10.0
Exception of the abs() Function
We cannot use any other data type except numerical ones with the abs() function.
Let’s see what happens when we use a string with the abs() function.
Example
abs("PythonApp")
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 1
----> 1 abs("TpoinTech")
TypeError: bad operand type for abs(): 'str'
It clearly defines that the abs() function can’t be used with any other data type, as it would result in an error.
Conclusion
The Python abs() function is used to return the absolute value of a number. The absolute function returns a positive number if the original number is negative. The syntax is abs(number). We discussed the abs() function using integer arguments, floating-point numbers, and complex numbers. We also learnt how to do Time-Distance calculation using the Python abs() Function.
Leave a Reply