abs() in Python
Python abs() function is used to return the absolute value of a number, i.e., it will remove the negative sign of the number.
Syntax: abs(number)
number: Can be an integer, a floating-pointnumber or a complex number
The abs() takes only one argument, a number whose absolute value is to be returned. The argument can be an integer, a floating-point number, or a complex number.
- If the argument is an integer or floating-point number, abs() returns the absolute value in integer or float.
- In the case of a complex number, abs() returns only the magnitude part and that can also be a floating-point number.
What does the abs() function do in Python?
The abs() function make any negative number to positive, while positive numbers are unaffected. The absolute value of any number is always positive. For any positive number, the absolute value is the number itself and for any negative number, the absolute value is (-1) multiplied by the negative number.
Example 1: Python abs() function example
In this example, we pass float, int, and complex data into abs() function and it will return absolute value.
- Python3
# Python code to illustrate # abs() built-in function # floating point number float = - 54.26 print ( 'Absolute value of float is:' , abs ( float )) # An integer int = - 94 print ( 'Absolute value of integer is:' , abs ( int )) # A complex number complex = ( 3 - 4j ) print ( 'Absolute value or Magnitude of complex is:' , abs ( complex )) |
Output:
Absolute value of float is: 54.26 Absolute value of integer is: 94 Absolute value or Magnitude of complex is: 5.0
Example 2: Absolute value Python, Distance calculation
This equation shows the relationship between speed, distance traveled and time taken:
Speed is distance divided by the time taken.
And we know speed, time and distance are never negative, for this we will use abs() methods to calculate the exact time, distance, and speed.
Formula used:
Distance = Speed * Time Time = Distance / Speed Speed = Distance / Time
Examples:
Input : distance(km) : 48.5 time(hr) : 2.6 Output : Speed(km / hr) : 18.653846153 Input : speed(km / hr) : 46.0 time(hr) : 3.2 Output : Distance(km) : 147.2 Input : distance(km) : 48.5 speed(km / hr) : 46.0 Output : Time(hr) : 1.0543
Code:
- Python3
# Python3 Program to calculate speed, # distance and time # Function to calculate speed def cal_speed(dist, time): print ( " Distance(km) :" , dist) print ( " Time(hr) :" , time) return dist / time # Function to calculate distance traveled def cal_dis(speed, time): print ( " Time(hr) :" , time) print ( " Speed(km / hr) :" , speed) return speed * time # Function to calculate time taken def cal_time(dist, speed): print ( " Distance(km) :" , dist) print ( " Speed(km / hr) :" , speed) return speed * dist # Driver Code # Calling function cal_speed() print ( " The calculated Speed(km / hr) is :" , cal_speed( abs ( 45.9 ), abs ( 2.0 ))) print ("") # Calling function cal_dis() print ( " The calculated Distance(km) :" , cal_dis( abs ( 62.9 ), abs ( 2.5 ))) print ("") # Calling function cal_time() print ( " The calculated Time(hr) :" , cal_time( abs ( 48.0 ), abs ( 4.5 ))) |
Output:
Distance(km) : 45.9 Time(hr) : 2.0 The calculated Speed(km / hr) is : 22.95 Time(hr) : 2.5 Speed(km / hr) : 62.9 The calculated Distance(km) : 157.25 Distance(km) : 48.0 Speed(km / hr) : 4.5 The calculated Time(hr) : 216.0
Last Updated on October 28, 2021 by admin