abs() in Python

1

The abs() function is used to return the absolute value of a number.
Syntax:

abs(number)

number : Can be integer, a floating point
number or a complex number

The abs() takes only one argument, a number whose absolute value is to be returned. The argument can be an integer, a floating point number or a complex number.

  • If the argument is an integer or floating point number, abs() returns the absolute value in integer or float.
  • In case of complex number, abs() returns only the magnitude part and that can also be a floating point number.
# Python code to illustrate 
# abs() built-in function

# floating point number
float = -54.26
print('Absolute value of integer is:', abs(float))

# An integer
int = -94
print('Absolute value of float is:', abs(int))

# A complex number
complex = (3 - 4j)
print('Absolute value or Magnitude of complex is:', abs(complex))

Output:

Absolute value of integer is: 54.26
Absolute value of float is: 94
Absolute value or Magnitude of complex is: 5.0

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.