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 Count the Number of Digits Present In a Number
Python Program to Trim Whitespace From a String
Python sum()
Python String join()
Python Program to Swap Two Variables
Python compile()
Python Program to Find Armstrong Number in an Interval
Python Operators
Python Set clear()
Python map()
Python String isnumeric()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python List
Python Program to Make a Flattened List from Nested List
Python Program to Return Multiple Values From a Function
Python Program to Check if a Number is Odd or Even
Python Set intersection()
Python input()
Python String strip()
Python Anonymous / Lambda Function
Python File I/O Operation
Python String isspace()
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Create a Countdown Timer
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python String isdecimal()
Python datetime
Deep Learning in Python - LazyProgrammer
Python Program to Create Pyramid Patterns
Python chr()
Python String expandtabs()
Python Recursion