In the previous article, we have discussed Python Program for copysign() Function
fabs() Function in Python:
The math. fabs() method returns a float containing the absolute value of a number.
The term absolute refers to a non-negative number. If the value has a negative sign, this removes it.
This method, unlike Python abs(), always converts the value to a float value.
Syntax:
math.fabs(x)
Parameters:
x: This is required. It is a number. If we try anything other than a number, we get a TypeError.
Return Value: It returns a float value which represents the absolute value of x.
Examples:
Example1:
Input:
Given list = [1, -2, -3, 4] Given first number = 45 Given second number = -70
Output:
The absolute value of above given number 45 = 45.0 The absolute value of above given number -70 = 70.0 The absolute value of given list element gvnlst[2] = 3.0
Example2:
Input:
Given list = [86, 34, 20, 10] Given first number = 90 Given second number = -40
Output:
The absolute value of above given number 90 = 90.0 The absolute value of above given number -40 = 40.0 The absolute value of given list element gvnlst[1] = 34.0
Program for fabs() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the list as static input and store it in a variable.
- Give the number as static input and store it in another variable.
- Apply math.fabs() function to the given number to get the absolute value of the given number as float and print it.
- Similarly, do the same for the other number.
- Apply math.fabs() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [1, -2, -3, 4]
# Give the number as static input and store it in another variable.
gvn_numb1 = 45
# Apply math.fabs() function to the given number to get the absolute value of
# the given number as float and print it.
print("The absolute value of above given number",
gvn_numb1, " = ", math.fabs(gvn_numb1))
# similarly do the same for the other number.
gvn_numb2 = -70
print("The absolute value of above given number",
gvn_numb2, " = ", math.fabs(gvn_numb2))
# Apply math.fabs() function to the given list element and print it.
print(
"The absolute value of given list element gvnlst[2] = ", math.fabs(gvn_lst[2]))
Output:
The absolute value of above given number 45 = 45.0 The absolute value of above given number -70 = 70.0 The absolute value of given list element gvnlst[2] = 3.0
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the number as user input using the int(input()) function and store it in a variable.
- Apply math.fabs() function to the given first number to get the absolute value of the given number as float and print it.
- Similarly, do the same for the other number.
- Apply math.fabs() function to the given list element and print it.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
'Enter some random List Elements separated by spaces = ').split()))
# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb1= int(input("Enter some random number = "))
# Apply math.fabs() function to the given first number to get the absolute value of
# the given number as float and print it.
print("The absolute value of above given number",
gvn_numb1, " = ", math.fabs(gvn_numb1))
# similarly do the same for the other number.
gvn_numb2 = int(input("Enter some random number = "))
print("The absolute value of above given number",
gvn_numb2, " = ", math.fabs(gvn_numb2))
# Apply math.fabs() function to the given list element and print it.
print(
"The absolute value of given list element gvnlst[1] = ", math.fabs(gvn_lst[1]))
Output:
Enter some random List Elements separated by spaces = 86 34 20 10 Enter some random number = 90 The absolute value of above given number 90 = 90.0 Enter some random number = -40 The absolute value of above given number -40 = 40.0 The absolute value of given list element gvnlst[1] = 34.0
Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.