+ 1
How do you use the any() function?
Can someone show my how to use the any() function for python3?
6 Antworten
+ 1
#################################
print(any((2 < 3, 3 > 4, 4 > 5))) # one expression is true so True is printed.
##################################
a = 2 < 3 # a is True
b = 3 > 4 # b is False
c = 4 > 5 # c is False
print(any([a, b, c])) # one element is true so True is printed.
##################################
+ 7
You pass an iterable to 'any' and it checks if at least one of the elements is True.
What's True and what's False then will depend on the type of the element.
any([0, 0, 0, 7]) -> True (7!=0)
any(('', '', '')) - > False (all strings zero length)
any(False for b in range(5)) -> False (all five
elements are (literally) False)
+ 1
In what language
+ 1
You can use it nicely with a generator expression or map (together with a function that returns a boolean).
- 1
Simple ex
def Fun():
print("hi")
Fun()