DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

3 2

Python abs()

ItsMyCode |

The abs() function in Python returns the absolute value of a given number, which means the abs() method removes the negative sign of a number.

If the given number is complex, then the abs() function will return its magnitude.

*abs() Syntax *

The syntax of abs() method is

abs(number)

abs() Parameters

The abs() function takes a single argument, a number whose absolute value is to be returned. The number can be

  • integer
  • floating-point number
  • complex number

abs() Return Value

abs() method returns an absolute value of a given number.

  • For integers – absolute integer value is returned
  • For floating numbers – absolute floating-point value is returned
  • For complex numbers – the magnitude of the number is returned

What does the abs() function do in Python?

The abs() function will turn any negative number into positive, the absolute value of a negative number is multiplied by (-1) and returns the positive number, and the absolute value of a positive number is the number itself.

Example 1: Get absolute value of a number in Python

In this example, we will pass an integer, float into the abs() function, and returns the absolute value.

# Python code to illustrate abs() built-in function

# integer number
integer = -10
print('Absolute value of -10 is:', abs(integer))

# positive integer number
positive_integer =20
print('Absolute value of 20 is:', abs(positive_integer))

# floating point number
floating = -33.33
print('Absolute value of -33.33 is:', abs(floating))

Enter fullscreen mode Exit fullscreen mode

Output

Absolute value of -10 is: 10
Absolute value of 20 is: 20
Absolute value of -33.33 is: 33.33
Absolute value of 3-4j is: 5.0
Enter fullscreen mode Exit fullscreen mode

Example 2: Get the magnitude of a complex number

In this example, we will pass a complex number into the abs() function and returns the absolute value.

# Python code to illustrate abs() built-in function

# complex number
complex=(3-4j)
print('Absolute value of 3-4j is:', abs(complex))
Enter fullscreen mode Exit fullscreen mode

Output

Absolute value of 3-4j is: 5.0
Enter fullscreen mode Exit fullscreen mode

The post Python abs() appeared first on ItsMyCode.

Image of Quadratic

Python + AI + Spreadsheet

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay