+ 1

How do you use the any() function?

Can someone show my how to use the any() function for python3?

9th Nov 2019, 7:31 PM
Tony Soodsamai
Tony Soodsamai - avatar
6 ответов
+ 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. ##################################
9th Nov 2019, 8:21 PM
rodwynnejones
rodwynnejones - avatar
+ 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)
9th Nov 2019, 7:49 PM
HonFu
HonFu - avatar
+ 1
In what language
9th Nov 2019, 7:48 PM
Isaac Duah [Active!]
Isaac Duah [Active!] - avatar
+ 1
You can use it nicely with a generator expression or map (together with a function that returns a boolean).
9th Nov 2019, 9:08 PM
Thoq!
Thoq! - avatar
- 1
Simple ex def Fun(): print("hi") Fun()
11th Nov 2019, 3:58 AM
Saurabh Arya
Saurabh Arya - avatar