22
loading...
This website collects cookies to deliver better user experience
all()
method returns True if the iterable object is empty.all()
method isall(iterable)
all()
function takes iterable as an argument the iterable can be of type list , set , tuple , dictionary , etc.all()
method returns a boolean value.True
if all of the elements in an iterable is true, In case of empty iterable object all()
returns True
.False
if any of elements in iterable are falseCondition | Return Value |
---|---|
All elements are true | True |
All elements are false | False |
One element is true and others are false) | False |
One element is false and others are true | False |
Empty Iterable | True |
any()
and all()
as series of logical “ OR ” and “ AND ” operators in simple terms.any()
will return True when at least one of the elements is True. all()
will return True only when all the elements are True.+-----------------------------------------+---------+---------+
| | any | all |
+-----------------------------------------+---------+---------+
| All Truthy values | True | True |
+-----------------------------------------+---------+---------+
| All Falsy values | False | False |
+-----------------------------------------+---------+---------+
| One Truthy value (all others are Falsy) | True | False |
+-----------------------------------------+---------+---------+
| One Falsy value (all others are Truthy) | True | False |
+-----------------------------------------+---------+---------+
| Empty Iterable | False | True |
+-----------------------------------------+---------+---------+
# All the elements in the list are true
list = [1,3,5,7]
print(all(list))
# All the elements in the list are false
list = [0,0,False]
print(all(list))
# Only one element is false
list = [1,5,7,False]
print(all(list))
# Only 1 element is true
list = [0, False, 5]
print(all(list))
# False since its Empty iterable
list = []
print(all(list))
True
False
False
False
True
# Non Empty string returns True
string = "Hello World"
print(all(string))
# 0 is False but the string character of 0 is True
string = '000'
print(all(string))
# True since empty string and not iterable
string = ''
print(all(string))
True
True
True
all()
method returns True. Otherwise, it returns False.# All elements in dictionary are true
dict = {1: 'Hello', 2: 'World'}
print(all(dict))
# All elements in dictionary are false
dict = {0: 'Hello', False: 'World'}
print(all(dict))
# Some of the elements in dictionary are true but one is false
dict = {1: 'Hello', 2: 'World', False: 'Welcome'}
print(all(dict))
# Empty Dictionary returns True
dict = {}
print(all(dict))
True
False
False
True
# All elements of tuple are true
t = (1, 2, 3, 4)
print(all(t))
# All elements of tuple are false
t = (0, False, False)
print(all(t))
# Some elements of tuple are true while others are false
t = (5, 0, 3, 1, False)
print(all(t))
# Empty tuple returns True
t = ()
print(all(t))
True
False
False
True
# All elements of set are true
s = {1, 2, 3, 4}
print(all(s))
# All elements of set are false
s = {0, 0, False}
print(all(s))
# Some elements of set are true while others are false
s = {1, 2, 3, 0, False}
print(all(s))
# Empty set returns True
s = {}
print(all(s))
True
False
False
True