19-Oct-2022
.
Admin
Hi Dev,
In this article we will cover on how to implement How to Find an Absolute Value in Python. we will help you to give example of abs() in Python. we will help you to give example of Python Absolute Value – abs() for real and complex numbers. step by step explain Calculate Absolute Value in Python Using abs() Function. Let's get started with Python Absolute Value: A Step-By-Step Guide.
The objective of this Python post, you will see various Python examples that cover the following:
let's see below simple example with output:
Python absolute Syntax
Python abs() method syntax is the following.
abs(n)
Here, n is the required parameter, and it is the number.
The abs() method will returns the absolute value of the given number.
See the first example of python abs() function following:
x = abs(-94)
print(x)
Output:
94
Python abs() function examples
Example 1: Python abs() with complex number
y = abs(3 - 4j)
print(y)
Output:
5.0
Example 2: Python abs() with different format numbers
x = 19.21e1/2 # exponential
print(abs(x))
y = 0b1100 # binary
print(abs(y))
z = 0o11 # octal
print(abs(y))
w = 0xF # hexadecimal
print(abs(w))
Output:
86.05
12
12
15
Example 3: Python absolute value of list
inputList = [21, 1, -19, 46]
Also. use the map() and list() python function to convert it to the absolute value.
inputList = [21, 1, -19, 46]
mappedList = map(abs, inputList)
print(list(mappedList))
Output:
[21, 1, -19, 46]
Example 4: write a program to find out the absolute value of an input number in python
#program to find out absolute value of an input number in python
num = int(input(" Please Enter the number : "))
abs = abs(num)
print("The absolute value of given number is : ", abs)
Output:
Please Enter the number : -10
The absolute value of given number is : 10
I hope it can help you...
#Python