Fabs in Python

The fabs method in Python is used to return the absolute value of a number.

It can be used by importing the math module.

The math module in Python can be used to implement different basic mathematical operations like addition, subtraction, division, and multiplication.

It can also be used to perform calculations involving exponents, logarithms, and trigonometry.

Syntax

The syntax for using fabs in Python is-

It's time to have a look at some programs for a better understanding.

Program 1:

In the first program, we will discuss the basics of the fabs method.

Output:

The fabs of a is:  20.3
The fabs of b is:  30.66
The fabs of c is:  19.13

Explanation:

Let's have a look at the explanation of this program-

  1. Since we have to use fabs(), we have imported the math module.
  2. After this, we have initialized the variables a, b, and c with positive and negative values.
  3. In the next step, we have passed these values to fabs() and printed them.
  4. On executing the program, it displays the desired output.

Program 2:

In the next program, we will perform some basic operations on these numbers that involve the usage of fabs().

The following program illustrates the same-

Output:

The sum of a and b is:  -50.96
The sum of b and c is:  -11.530000000000001
The sum of d and e is:  50.96
The sum of e and f is:  49.79

Explanation:

Let us see what we have done in the above program-

  1. Since we have to use fabs(), we have imported the math module.
  2. After this, we have initialized the variables a, b, and c with positive and negative values.
  3. In the next step, we have printed the sum of a + b and b + c.
  4. Now we have passed these three variables in fabs() and stored the returned values in variables d, e, and f.
  5. We have calculated the sum again to check the difference between the results obtained using the values that were used for evaluation before and after passing into fabs.
  6. On executing the program, it displays the desired output.

Program 3:

Finally, in the last program, we will perform one more mathematical operation on the numbers.

Output:

The product of a and b is:  622.398
The product of b and c is:  -586.5258
The product of d and e is:  622.398
The product of e and f is:  586.5258

Explanation:

Let's have a look at the explanation of this program-

  1. Since we have to use fabs(), we have imported the math module.
  2. After this, we have initialized the variables a, b, and c with positive and negative values.
  3. In the next step, we have printed the product of a * b and b * c.
  4. Now we have passed these three variables in fabs() and stored the returned values in variables d, e, and f.
  5. We have calculated the product again to check the difference between the results obtained using the values that were used for evaluation before and after passing into fabs.
  6. On executing the program, it displays the desired output.

Conclusion

In this Tutorial, we learned fabs() and how they can be used in Python programs to make our work much easier.






Latest Courses