Python all() 函數
all() 函數用於檢查迭代的所有元素是否為真。它接受一個可迭代的容器並返回True
如果所有元素都為真,否則返回False
。
用法:
all(iterable)
參數: iterable
– 一個可迭代的容器,如列表、元組、字典。
返回值: bool
– 一個布爾值
例:
Input: val = [10, 20, 30, 40] print(all(val)) val = [10, 20, 0, 40] print(all(val)) Output: True False
用於檢查可迭代對象的所有元素是否為真的 Python 代碼(打印返回值)
# 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))
輸出
True False False True True True
用於檢查可迭代對象的所有元素是否為真的 Python 代碼(使用條件檢查)
# 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")
輸出
list1 has all true elements list2 does not has all true elements
相關用法
- Python unittest assertNotIsInstance()用法及代碼示例
- Python Tkinter askopenfile()用法及代碼示例
- Python unittest assertIsNotNone()用法及代碼示例
- Python unittest assertFalse()用法及代碼示例
- Python Tkinter asksaveasfile()用法及代碼示例
- Python unittest assertIs()用法及代碼示例
- Python unittest assertNotIn()用法及代碼示例
- Python unittest assertAlmostEqual()用法及代碼示例
- Python ascii()用法及代碼示例
- Python unittest assertGreater()用法及代碼示例
- Python unittest assertIsNot()用法及代碼示例
- Python abs()用法及代碼示例
- Python unittest assertLessEqual()用法及代碼示例
- Python unittest assertEqual()用法及代碼示例
- Python any()用法及代碼示例
- Python unittest assertIsNone()用法及代碼示例
- Python attr.asdict()用法及代碼示例
- Python unittest assertNotAlmostEqual()用法及代碼示例
- Python unittest assertNotEqual()用法及代碼示例
- Python unittest assertGreaterEqual()用法及代碼示例
注:本文由純淨天空篩選整理自 all() function with example in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。