abs() in Python - GeeksforGeeks

13 Sep, 2021
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-point
number 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.
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. 
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 numberfloat = -54.26print('Absolute value of float is:', abs(float)) # An integerint = -94print('Absolute value of integer is:', abs(int)) # A complex numbercomplex = (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
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 speeddef cal_speed(dist, time):    print(" Distance(km) :", dist)    print(" Time(hr) :", time)    return dist / time # Function to calculate distance traveleddef cal_dis(speed, time):    print(" Time(hr) :", time)    print(" Speed(km / hr) :", speed)    return speed * time # Function to calculate time takendef 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
AdityaPandey1
sheetal18june
kumar_satyam
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Reading and Writing to text files in Python
sum() function in Python