Python absolute value – with example

  • Reading time:16 mins read

Python absolute value – basics

An absolute value of a number describes the distance from zero that a number is on the number line. It can also be defined as the value without considering its sign. In Python the abs() function allows us to return the absolute value of a number.

 

How to use the abs() function

The abs() function is a built-in function that takes in one argument and follows the syntax below.

 

abs(num)

 

Where num represents the number whose absolute value we want to return. This number can be an integer, floating-point number or a complex number. In this article, we will explore how to return the absolute values of these numeric types.

Please note that when using the abs() function on positive numbers they remain unaffected. 

 

Python absolute value

 

Finding the Python absolute value of an Integer

An integer is a positive or negative whole number without a fractional part including zero. We are going to create two variables containing positive and negative integers of the same value named num and num1. We have also created two variables to hold the absolute values returned by the abs() function namely abval and abval1.  

 

 

As shown in the output above the absolute value of -15 is 15, while the 15 remains unaffected.

Finding the Python Absolute Value of a Floating point number

A floating-point number in Python is a number with decimal points. We can also use the abs() function to find the absolute value of floating-point numbers. In such a scenario the abs() function returns a floating-point absolute value.

 

 

In a similar manner, the absolute value of the negative floating-point number is the number without the negative sign. While the absolute value of the positive absolute number is that same number unaffected.

 

Python absolute value

 

Finding the Python Absolute Value of Complex Numbers

Complex numbers consist of real and imaginary parts. The imaginary part is normally represented by appending a j after the number.

We can also use the abs() function to return the absolute values of complex numbers. In the case of complex numbers, the absolute function returns the magnitude part that can also be a floating-point number.

 

 

As mentioned the magnitude can also be a floating-point number as shown below.

 

Using the max() function

We can use the abs() function to work out the largest absolute value from a list of numbers. We will also use the built-in function max()  which returns the item with the highest value in an iterable.

 

Python absolute value

 

When used alone the max() function simply returns the highest value as shown below.

 

 

To get the number with the furthest distance from zero we have to specify the key as abs. Now when we run this we are going to get the furthest figure from zero which is -15 in this list.

 

 

Summary

So this is how you can use Python absolute value methods in a nutshell. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English. In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.