forked from WebJournal/journaldev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_any_examples.py
More file actions
62 lines (41 loc) · 1.17 KB
/
python_any_examples.py
File metadata and controls
62 lines (41 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# iterable has at least one True
list_bools = [True, True, True]
print(any(list_bools))
# iterable none of the elements are True
list_bools = [False, False]
print(any(list_bools))
# iterable is empty
list_bools = []
print(any(list_bools))
# iterable elements are True string (at least one)
list_strs = ['True', 'false']
print(any(list_strs))
# iterable any elements is true string with different case
list_strs = ['fff', 'true']
print(any(list_strs))
# iterable any elements are not true string
list_strs = ['abc', 'def']
print(any(list_strs))
# iterable all elements are empty string
list_strs = ['', '']
print(any(list_strs))
# iterable objects example
class Employee:
name = ""
def __init__(self, n):
self.name = n
# comment and check output
def __bool__(self):
print('bool function called')
if len(self.name) > 3:
return True
else:
return False
# comment and check output
def __len__(self):
print('len function called')
return len(self.name)
list_objs = [Employee("Pankaj"), Employee("Lisa")]
print(any(list_objs))
list_objs = [Employee("A"), Employee("D")]
print(any(list_objs))