⬅ Previous Topic
Python Built-in FunctionsNext Topic ⮕
Python String Methods⬅ Previous Topic
Python Built-in FunctionsNext Topic ⮕
Python String Methodsabs()
FunctionThe abs() function in Python returns the absolute value of a number.
abs(number)
number
– An integer, float, or a number that implements __abs__()
abs()
with Integersprint(abs(-5))
5
abs()
with Floatsprint(abs(-3.14))
3.14
abs()
Useful?print(abs(3 + 4j))
5.0
This is because abs()
returns the magnitude of a complex number.
TypeError
__abs__()
In coding interviews, abs()
is often used in problems involving distances, differences, or comparisons.
abs()
returns the absolute valueint
, float
, and complex
TypeError
if the argument is not numericWrite a program that reads a number from the user and prints its absolute value.
num = float(input("Enter a number: "))
print("Absolute value is:", abs(num))
⬅ Previous Topic
Python Built-in FunctionsNext Topic ⮕
Python String MethodsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.