How to find absolute value in python?

Short Tutorial: Introduction to Python

Now which function we used to identify the absolute number in python?

abs() is the function available in python to calculate the absolute number.

math.fabs() is another function available to calculate the absolute number but as a floating type.

Syntax

>>abs(value) #value is a parameter 
>>math.fabs(value) #value is a parameter

Short Tutorial: Variable and Data Types in Python

Examples of finding absolute value in python

  • Absolute number of positive integer
>>abs(87)
  • Absolute number of negative integer - it returns positive integer by dropping negative sign
>>abs(-67)
  • Absolute value of complex number - it returns magnitude of complex number
>>abs(4+5j)

  • What if calculation happens inside abs() function - it solves the equation and return positive numeral
>>abs((3*-4)+37)
  • absolute value of positive floating value - it returns the same value
>>abs(3234.323)
  • absolute value of negative floating value - it returns the same value by dropping the negative sign)
>>abs(-234.432)

Short Tutorial: Tuples, Sets and Dictionaries in Python

math.fabs() examples

>>import math
>>math.fabs(68)
>>math.fabs(-987)
>>math.fabs((3*4)+37))
>>math.fabs(98.763)
>>math.fabs(-378.3232)

Note: math.fabs() not able to identify absolute value of complex number.

Conclusion

abs() and math.fabs() are two in-built python functions that we can use to calculate the absolute number.

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

10 Blockchain Security Vulnerabilities OWASP API Top 10 - 2023 7 Facts You Should Know About WormGPT OWASP Top 10 for Large Language Models (LLMs) Applications Top 10 Blockchain Security Issues