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

python函数示例

Python all()函数 (Python all() function)

all() function is used to check whether all elements of an iterable are True or not. It accepts an iterable container and returns True if all elements are True, else it returns False.

all()函数用于检查iterable的所有元素是否为True。 它接受一个可迭代的容器,如果所有元素为true返回true,否则返回False。

Syntax:

句法:

    all(iterable)

Parameter: iterable – an iterable container like list, tuple, dictionary.

参数: iterable –可迭代的容器,如列表,元组,字典。

Return value: bool – a Boolean value

返回值: bool-布尔值

Example:

例:

    Input:
    val = [10, 20, 30, 40]
    print(all(val))
    val = [10, 20, 0, 40]
    print(all(val))

    Output:
    True
    False

Python code to check whether all elements of an iterable are true or not (printing return values)

Python代码检查iterable的所有元素是否为真(打印返回值)

# python code to demonstrate example
# of all() function

val = [10, 20, 30, 40]  #list with all true values
print(all(val))

val = [10, 20, 0, 40]  #list with a flase value
print(all(val))

val = [0, 0, 0, 0.0]  #list with all false values
print(all(val))

val = [10.20, 20.30, 30.40]  #list with all true(float) values
print(all(val))

val = [] #an empty list
print(all(val))

val = ["Hello", "world", "000"] #list with all true values
print(all(val))

Output

输出量

True
False
False
True
True
True 

Python code to check whether all elements of an iterable are true or not (checking using condition)

Python代码检查iterable的所有元素是否为真(使用条件检查)

# python code to demonstrate example
# of all() function

list1 = [10, 20, 30, 40]
list2 = [10, 20, 30, 0]

# checking with condition
if all(list1)==True:
    print("list1 has all true elements")
else:
    print("list1 does not has all true elements")

if all(list2)==True:
    print("list2 has all true elements")
else:
    print("list2 does not has all true elements")    

Output

输出量

list1 has all true elements
list2 does not has all true elements


翻译自: https://www.includehelp.com/python/all-function-with-example.aspx

python函数示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值