python函数示例
Python abs()函数 (Python abs() function)
abs() function is a library function in Python, it is used to get the absolute value of a number. It accepts a number (can be an integer number, float number or a complex number) and returns the absolute value of the given number.
abs()函数是Python中的库函数,用于获取数字的绝对值。 它接受一个数字(可以是整数,浮点数或复数),并返回给定数字的绝对值。
Syntax:
句法:
abs(number)
Parameter: number – it can be an integer number, float number of a complex number.
参数: number –它可以是整数,可以是复数的浮点数。
Return value: It returns absolute value of number.
返回值:返回number的绝对值。
Example:
例:
Input:
num = -10
print(abs(num))
Output:
10
Input:
num = 10.23
print(float(num))
Output:
10.23
Python code to demonstrate example of abs() function
Python代码演示abs()函数的示例
# python code to demonstrate an example
# of abs() function
# integer number
iNum1 = 10
iNum2 = -10
print("Absolute value of ", iNum1, " is = ", abs(iNum1))
print("Absolute value of ", iNum2, " is = ", abs(iNum2))
# float number
fNum1 = 10.23
fNum2 = -10.23
print("Absolute value of ", fNum1, " is = ", abs(fNum1))
print("Absolute value of ", fNum2, " is = ", abs(fNum2))
# complex number
cNum1 = (10 + 5j)
cNum2 = (10 - 5j)
print("Absolute value of ", cNum1, " is = ", abs(cNum1))
print("Absolute value of ", cNum2, " is = ", abs(cNum2))
Output
输出量
Absolute value of 10 is = 10
Absolute value of -10 is = 10
Absolute value of 10.23 is = 10.23
Absolute value of -10.23 is = 10.23
Absolute value of (10+5j) is = 11.180339887498949
Absolute value of (10-5j) is = 11.180339887498949
翻译自: https://www.includehelp.com/python/abs-function-with-example.aspx
python函数示例