Python abs() Function with Examples

In the previous article, we have discussed Python Program for any() Function
abs() Function in Python:

The abs() function calculates the absolute value of the given number.

If the number is a complex number, the abs() function returns the magnitude of the number.

Syntax:

abs(n)

Parameters

n: This is required. It is a number. It can be an integer, floating number, or complex number.

Return value:

The abs() method returns the absolute value of a number.

  • For integers, the absolute value of the integer is returned.
  • The floating absolute value is returned for floating numbers.
  • The magnitude of the number is returned for complex numbers.

Examples:

Example1:

Input:

Given first number = -10
Given second number = 2+6j

Output:

The absolute value of the given number -10 = 10
The absolute value of the given number (2+6j) = 6.324555320336759

Example2:

Input:

Given first number = -12.55
Given second number = 30

Output:

The absolute value of the given number -12.55 = 12.55
The absolute value of the given number 30 = 30

Program for abs() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Apply abs() function to the given number to get the absolute value of the given number.
  • Store it in another variable.
  • Print the absolute value of the given number.
  • Similarly, do the same for the other number and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb1 = -10
# Apply abs() function to the given number to get the absolute value of
# the given number and store it in another variable.
rslt_charctr1 = abs(gvn_numb1)
# Print the absolute value of the given number.
print("The absolute value of the given number",
      gvn_numb1, "=", rslt_charctr1)
# Similarly, do the same for the other number and print the result.
gvn_numb2 = 2+6j
rslt_charctr2 = abs(gvn_numb2)
print("The absolute value of the given number",
      gvn_numb2, "=", rslt_charctr2)

Output:

The absolute value of the given number -10 = 10
The absolute value of the given number (2+6j) = 6.324555320336759

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the float(input()) function and store it in a variable.
  • Apply abs() function to the given number to get the absolute value of the given number.
  • Store it in another variable.
  • Print the absolute value of the given number.
  • Similarly, do the same for the other number and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply abs() function to the given number to get the absolute value of
# the given number.
# store it in another variable.
rslt_charctr1 = abs(gvn_numb1)
# Print the absolute value of the given number.
print("The absolute value of the given number",
      gvn_numb1, "=", rslt_charctr1)
# Similarly, do the same for the other number and print the result.
gvn_numb2 = int(input("Enter some random number = "))
rslt_charctr2 = abs(gvn_numb2)
print("The absolute value of the given number",
      gvn_numb2, "=", rslt_charctr2)

Output:

Enter some random number = -12.55
The absolute value of the given number -12.55 = 12.55
Enter some random number = 30
The absolute value of the given number 30 = 30

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.