abs(): Absolute Value Function

abs

LanguagePython
CategoryFunction
Part OfBuilt In Functions
Named Arguments Count0
Unnamed Arguments Count1
Official Documentation Absolute Value Function

1 Description

The Python abs function will return the absolute value of the operand.

2 Prototype

Below is the function prototype for the abs function.

abs(x)

3 Arguments

Abs Function Arguments
Name Type Default Value Category Description
x Numeric Unnamed argument(s) The value you would like to obtain the absolute value of. This value must be a numeric type, meaning float or int.

4 Returns

Abs Function Returns
Return Type Explanation
int When you pass a value of type int to the abs function the return type will be int.
float When you pass a value of type float to the abs function the return type will be float.

5 Examples

5.1 Float and Integer Example

In the example file below the abs function is used to obtain the absolute value of both a positive and negative float value, and a positive and negative integer value.

#!/usr/bin/python3

def main():
    var_1 = 5
    var_2 = 5.9
    var_3 = -4
    var_4 = -4.9

    print("absolute value of var_1: ", str(abs(var_1)))
    print("absolute value of var_2: ", str(abs(var_2)))
    print("absolute value of var_3: ", str(abs(var_3)))
    print("absolute value of var_4: ", str(abs(var_4)))

if __name__ == "__main__":
    main()
user-1@vm:~/Documents$ ./abs_example_1.py
absolute value of var_1: 5 absolute value of var_2: 5.9 absolute value of var_3: 4 absolute value of var_4: 4.9
user-1@vm:~/Documents$

This document was last updated: