Reputation: 4538
I searched for understanding about the all
function in Python, and I found this, according to here:
all
will returnTrue
only when all the elements are Truthy.
But when I work with this function it's acting differently:
'?' == True # False
'!' == True # False
all(['?','!']) # True
Why is it that when all elements in input are False
it returns True
? Did I misunderstand its functionality or is there an explanation?
Upvotes: 6
Views: 1726
Reputation: 1
all() function is used when we want to check that if all the items in a list are iterable or not.
For ex :
x=[1,2,3,4,5]
all(x)
It will return True.
Upvotes: -2
Reputation: 45806
'?' and '!' are both truthy since they are non-empty Strings.
There's a difference between True
and "truthy". Truthy means that when coerced, it can evaluate to True
. That's different from it being ==
to True
though.
Upvotes: 2
Reputation: 27333
only when all the elements are Truthy.
Truthy != True
.
all
essentially checks whether bool(something)
is True
(for all something
s in the iterable).
>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True
Upvotes: 10