Python abs() FunctionLast Updated : 16 Feb 2026 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:Parametersnum: 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 Examplesabs() Function with an Integer ArgumentWe 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. Output: Absolute value of -100 is: 100 abs() Function with a Floating-Point NumberSimilarly, 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. Output: Absolute value of float number -69.96 is: 69.96 abs() Function with a Complex NumberThe 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. Output: Magnitude of complex number (5+12j) is: 13.0 Time-Distance Calculation Using Python abs() FunctionAs 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: ExampleCompile and RunOutput: 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() FunctionWe 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. ExampleOutput:
---------------------------------------------------------------------------
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. ConclusionThe 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. Next TopicPython Built in Functions |
We request you to subscribe our newsletter for upcoming updates.