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 Set remove()
Python Dictionary fromkeys()
Python Program to Reverse a Number
Python String capitalize()
Python String isnumeric()
Python hash()
Python String rstrip()
Python Program to Check if a Key is Already Present in a Dictionary
Python isinstance()
Python String isidentifier()
Python List
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Custom Exceptions
Python Program to Check Prime Number
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Create a Long Multiline String
Python Program to Measure the Elapsed Time in Python
Python Program to Copy a File
Python Program to Safely Create a Nested Directory
Python delattr()
Python Program to Check if a Number is Positive, Negative or 0
Python String swapcase()
Deep Learning with Python - Francois Chollet
Python Program to Find the Sum of Natural Numbers
Python vars()
Python Program to Count the Number of Digits Present In a Number
Introduction to Scientific Programming with Python - Joakim Sundnes
Python hex()
Python Program to Get the File Name From the File Path
Python Program to Catch Multiple Exceptions in One Line
Python property()