Table of Contents
In this tutorial, we will learn about the Python abs() function with the help of examples.
The abs()
function returns the absolute value of the given number. If the number is a complex number, abs()
returns its magnitude.
1. Example
number = -20 absolute_number = abs(number) print(absolute_number) # Output: 20
2. abs() Syntax
The syntax of abs()
method is:
abs(num)
3. abs() Parameters
abs()
method takes a single argument:
- num – a number whose absolute value is to be returned. The number can be:
- integer
- floating number
- complex number
4. abs() Return Value
abs()
method returns the absolute value of the given number.
- For integers – integer absolute value is returned
- For floating numbers – floating absolute value is returned
- For complex numbers – magnitude of the number is returned
5. Example 1: Get absolute value of a number
# random integer integer = -20 print('Absolute value of -20 is:', abs(integer)) #random floating number floating = -30.33 print('Absolute value of -30.33 is:', abs(floating))
Output
Absolute value of -20 is: 20 Absolute value of -30.33 is: 30.33
6. Example 2: Get magnitude of a complex number
# random complex number complex = (3 - 4j) print('Magnitude of 3 - 4j is:', abs(complex))
Output
Magnitude of 3 - 4j is: 5.0
Related posts:
Python Program to Remove Duplicate Element From a List
Python String expandtabs()
Python Program to Compute all the Permutation of the String
Python Program to Remove Punctuations From a String
Python Program to Convert Celsius To Fahrenheit
Python Program to Randomly Select an Element From the List
Python Program to Calculate the Area of a Triangle
Python String rpartition()
Python Program to Multiply Two Matrices
Python isinstance()
Python Dictionary values()
Python Program to Find the Factors of a Number
Python List index()
Python Anonymous / Lambda Function
Python Program to Check Prime Number
Python Inheritance
Python len()
Python pass statement
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Find ASCII Value of Character
Python String isspace()
Python Get Current time
Python String capitalize()
Python ascii()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Set discard()
Python Exception Handling Using try, except and finally statement
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String swapcase()
Python Program to Compute the Power of a Number
Python Set remove()
Python Generators