Python 3.1 快速導覽 - 內建函數 all()

內建函數 (function) all() ,判斷參數 (parameter) iterable 中所有元素是否為迭代器 (iterator)

函數描述
all(iterable)判斷 iterable 中所有元素是否為迭代器


all() 與下面的函數定義是一樣的
def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True


舉例示範如下
print(all("123"))      # 字串
print(all([0, 1]))     # 串列
print(all((0, 1)))     # 序對
print(all({1:1, 2:2})) # 字典

# 《程式語言教學誌》的範例程式
# http://pydoing.blogspot.com/
# 檔名:all.py
# 功能:示範 Python 程式 
# 作者:張凱慶
# 時間:西元 2010 年 12 月 


執行結果如下



中英文術語對照
函數function
參數parameter
迭代器iterator
字串string
字典dictionary
串列list
序對tuple


內建函數




沒有留言: