Python 中内置函数(一) abs、any和all

abs()

abs() 返回输的绝对值,输入为数字,若输入为负数,则返回负数的幅值。
| 输入 | 输出
| ---- | ---- | ----
| abs(-3) | 3
| abs(3+4j) | 5.0
| abs("3") | TypeError

all()

all 输入为可迭代类型,如 list tuple 等,若 元素全为1 或 空 ,返回Ture ,否则 返回False.等价代码如下:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

| 输入 | 输出
| ---- | ---- | ----
| all([1,1,1]) | True
| all([1,1,0]) | False
| all(-3) | TypeError
| all([]) | True

any()

all 输入为可迭代类型,如 list tuple 等,若 元素任意为真 ,返回Ture ;若为空 返回False.等价代码如下:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

| 输入 | 输出
| ---- | ---- | ----
| any([1,1,1]) | True
| any([0,0,0]) | False
| any(-3) | TypeError

原文地址:https://www.cnblogs.com/Finding-bugs/p/14179704.html