python函数示例_abs()函数以及Python中的示例

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函数示例

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值