any()和and、or一樣是Short-Circuiting Operator,也就是說碰到第一個item為True any()就會直接return True不繼續往下執行。
以下是兩個使用any()的例子:
1.判斷一個list中是否有負數
if any(x < 0 for x in numbers):
print "some of the numbers are negative"
else:
print "none of the numbers are negative"
2.檢查密碼是否含有一個大寫字母、一個數字、一個小寫字母
def checkPassword(data):
'Return True if password strong and False if not'
result = any(c.isupper() for c in data) and
any(c.isdigit() for c in data) and
any(c.islower() for c in data)
return result
