in

Python Absolute Value

python-absolute-value

Python Absolute Value – The abs () method returns the full value of your given number. If a number is a complex number, then abs () removes its complexity and returns its magnitude.

abs() Parameters

abs() method takes a single argument:

num – A number whose full value is to be returned. The number can be:

  • integer
  • floating number
  • complex number

Python Absolute Return value from abs()

abs() method returns the absolute value of the given number.

  • The return value also will be an integer – For integers
  • The return value will also be float – For floating numbers
  • Return value will be the magnitude of the input – For complex numbers

Example 1: Float number and Integer

# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))

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

Output:

Absolute value of -20 is: 20
Absolute value of -22.33 is: 22.33

Example 2: Get magnitude of a complex number

# random complex number
complex = (3 - 6j)
print('Magnitude of 3 - 6j is:', abs(complex))

Output:

Magnitude of 3 - 6j is: 6.708203932499369

Leave a Reply

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

c++-calculator

C++ Calculator Using Switch Case

python-to-lowercase

Python To Lowercase