Technology3 min read

Python abs() Method - Python Library Function

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

In Python there are many built-in functions and abs() method is one of them. The main purpose of this function is to return the absolute value of the number(variable) passed as argument to it.

This is a detailed post on abs() method that touches every aspect of it.

Python abs() method




Syntax of abs() method:

The abs() method takes only one argument and hence the syntax is pretty simple,

# X can be an integer or a float or a complex number
abs(X)

If, X (the argument) is an Integer or Float then the method will return the absolute value in Integer or Float

else if X is a Complex Number then the method will return the magnitude of that complex number.




What is an Absolute value?

The absolute value for a given number is the numerical value of that number without considering whether the sign of the number is positive (+ve) or negative (-ve ).

Simply put, an absolute number is always a positive(+ve) number.

For complex numbers, the absolute value is their magnitude.

Example for Integer:

The absolute value of -5 is 5.

Example for Float:

The absolute value of -5.5 is 5.5

Example for Complex Numbers:

The absolute value of 3+4j is 5.0

Lets see, how to find the magnitude of a complex number,

The magnitude of complex number a+bj is sqrt( a^2 + b^2 ).

For example: The magnitude of 3+4j is sqrt( 3^2 + 4^2 ) = sqrt( 9 + 16 ) = sqrt(25) = 5




Python Program using abs() Function

Below we have a simple program where we have used the abs() method,

Pheww! This is it. I hope that you enjoyed the post and learned about the implementation of abs() built-in function available in Python Standard Language. If you feel that this post is useful, please share it with your friends and colleagues.

Thanks for reading it till the end.


SHARE YOUR THOUGHTS WITH US!



Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next