Absolute Value in Python

The absolute value in Python | Python abs() is a built-in function available with the standard library of python. The abs() function is used to return the absolute value of the given number. If the number is a complex number, the abs() function returns its magnitude. The absolute value of a number is a value without considering its sign. Example:- absolute value of the 5 is 5 and -5 is also 5.

The syntax of of abs() function:-

abs(number)

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

  • integer number
  • floating-point number
  • complex number

The argument can be an integer, a floating number, or a complex number. Then, the abs() function returns the absolute value of a number.

  • Integer number:- abs() return the absolute value in integer.
  • Floating number:- abs() return the absolute value in float.
  • Complex number:- abs() return only the magnitude part and that can be a floating number.

abs() Function in Python for Integer Number

This python program using the abs() method to find the absolute value of a given number. We will take integer numbers while declaring the variables. Then, find absolute value using the abs() function and finally, the result will be displayed on the screen.

# Python program to find absolute value of number

# take input
num = int(input('Enter the integer number: '))

# display result
print('Absolute value of number is =',abs(num))

Output for the different input values:-

Enter the integer number: -5
Absolute value of number is = 5

Enter the integer number: -13
Absolute value of number is = 13

Enter the integer number: 10
Absolute value of number is = 10

abs() in Python for Floating Number

In the previous program, we will find the absolute value of integer numbers but in this program, find the absolute value of a floating-point number.

# Python program to find absolute value of number

# take input
num = float(input('Enter a float number: '))

# display result
print('Absolute value of number is =',abs(num))

Output for the different input values:-

Enter a float number: -5.31
Absolute value of number is = 5.31

Enter a float number: -23.1
Absolute value of number is = 23.1

Absolute Value in Python for Complex Number

The absolute value or magnitude of a complex number, x+yi is defined as the distance between the origin (0,0) and the point (x,y) in the complex plane.

# Python program to find absolute value of complex number

# take input
num = complex(input('Enter a complex number: '))

# display result
print('Magnitude of complex number is =',abs(num))

Output for the different input values:-

Enter a complex number: 3-4j
Magnitude of complex number is = 5.0

Enter a complex number: 2+6j
Magnitude of complex number is = 6.324555320336759

Enter a complex number: -5-10j
Magnitude of complex number is = 11.180339887498949

Enter a complex number: -15j
Magnitude of complex number is = 15.0

Also See:- Fibonacci Series in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *