Python abs()

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))

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

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))

Output

Absolute value of 3-4j is: 5.0
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
How To Fix Keyerror In Pandas

How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…
View Post
Python Print To File

Python Print to File

We always use print statements in Python to display the output in the console or command line terminal. However, sometimes we want to change this behavior to print to a…
View Post
Python String Isspace()

Python String isspace()

Table of Contents Hide What are whitespace characters in Python?isspace() Syntaxisspace() Parametersisspace() Return valueExample 1: Working with isspace() method Example 2: Program to check if the string has only whitespace…
View Post
Python Enumerate()

Python enumerate()

Table of Contents Hide enumerate() Syntax enumerate() Parametersenumerate() Return ValueExample 1: How enumerate() method works in Python?Example 2: Using Enumerate object in loops The enumerate() in Python is a built-in function that adds…
View Post
Python Print Variable

Python Print Variable

Table of Contents Hide How to use the print() function in Python?How to Print variable in Python?Method 1: Using comma , character to separate the variables in a print statementMethod…
View Post