+ 5
[Solved] Why print(all([(2, 2)])) return True?
I tried below statements, both return False print((2, 2) == True) # False print([(2, 2)] == True) # False Why print(all([(2, 2)])) return True?
27 odpowiedzi
+ 8
Try this:
t = 2, 2
print(t) # (2, 2)
print(type(t)) # <class 'tuple'>
print(bool(t)) # True
The all function takes an iterable and evaluates to True when all elements have a True boolean value. The truthiness of a non-empty container, such as this tuple, is generally True in Python.
As a cross-check, you can create an empty tuple like this:
t = tuple()
And in this case bool(t) is False.
+ 5
My experiments:
print(()) #()
print([]) #[]
print(type(()))
print(type([]))
print(type((())))
print(type(([])))
print(type((None,)))
print(type([None]))
print(type((None,None)))
print(type([None,None]))
print(all((None,None))) #False
print(all([None,None])) #False
print(all(())) #True this is surprising
print(all([])) #True this is surprising
print(all(([]))) #True this is surprising
print(all((()))) #True this is surprising
print(all((2,2))) #True
print(all([2,2])) #True
+ 3
Bob_Li
Yes, I was somewhat mixed up because it is confusing.
From this code, it is like each element get pass to bool() first. So even the element contains 0 value, since the element is not empty it is evaluated to True.
https://sololearn.com/compiler-playground/c21Q4x2HSsJV/?ref=app
+ 2
a tuple,list,set,and dictionary is False only when they are empty.
print(bool(()))
print(bool([]))
print(bool({}))
print(bool(set()))
if the len>0, then it becomes True.
so it is a good rule to remember that the truthiness of a collection is tied to it's len
+ 2
Wong Hei Ming, Mirielle . Practical application..😁
def is_broke(w):
if(w):
print("You're not broke")
else:
print("Yes, you're broke. Merry Christmas.")
my_wallet = (None,None)
is_broke(my_wallet)
my_wallet = (0, 0)
is_broke(my_wallet)
my_wallet = (0,)
is_broke(my_wallet)
#The only thing that keeps you from going broke is that ',' 😅
my_wallet = (0)
is_broke(my_wallet)
my_wallet = ()
is_broke(my_wallet)
+ 2
Mirielle
Thanks and I know that. it also in the code I posted a few hours earlier.
+ 1
#Falsy values are interesting...
print(bool([])) #False...ok, it's falsey
print(len([])) #0
print(all([])) #True why?
print(bool(None)) #False...ok
print(all([None])) #False...ok
print(all([0])) #False...ok
print(all([False])) #False...ok
print(all([list()])) #False...ok
print(all([tuple()])) #False...ok
print(all([dict()])) #False...ok
print(all([str()])) #False...ok
print(all([set()])) #False...ok
print(all([bool()])) #False...ok
print(all([print()])) #print() still prints, but evaluates to None
#But these feels inconsistent Why are these falsy values not False?
print('Directly comparing falsy values to True and False:')
print(None==False) #False...?
print(''==False) #False...?
print([]==False) #False...?
print('---------------')
#so == only compares value, not falseyness.
print(0==False) #True...ok
print((1-1)==False) #True...ok
#But any() and all() can detect falsy values. To use == on falsy values, you have to convert to bool
print(bool(None)==False) #True...ok
print(bool('')==False) #True...ok
+ 1
Okay, I dig into the documentation
all()
https://docs.python.org/3/library/functions.html#all
Truth Value Testing
https://docs.python.org/3/library/stdtypes.html#truth
print(all((None,None))) #False
print(all([None,None])) #False
According to Truth Value Testing, None, False and 0 are considered to be False, thus prints False.
print(all(())) #True this is surprising
print(all([])) #True this is surprising
According to all(), an empty iterable is considered as True, thus prints True.
print(all(([]))) #True this is surprising
print(all((()))) #True this is surprising
Since the tuple contains another tuple / list, the tuple is not empty and don't have 0, None or False values, thus prints True.
Put it into test:
print(all(((1,), (2,), (3,)))) # True, a tuple of tuples
print(all((1, 2, 0))) # False, tuple contains 0
And this surprise me
print(all(((1), (2), (0)))) # False, 0 inside the tuple, nothing special
print(all(((1,), (2,), (0,)))) # True
How come a tuple (0,) is considered True?
y = (0) #<class 'int'>
print(bool(y)) # False, as expected
x = (0,) # <class 'tuple'>
print(bool(x)) # True ???
+ 1
Bob_Li
Your application was fun.
However I'm talking how it works combine with all() method.
print(len((0,))) returns 1, one element inside a tuple, and the element is 0. And we know print(0==False) returns True.
Now with the following
print(all((0, ))) returns True
The tuple is not empty, so it can return True or False base on the elements.
In this case 0 is the only element and 0 is same as False. So the tuple contains a False value. Why it returns True?
Edited
We all are teached tuple is similar to list.
print(all([0])) returns False.
Similar to above tuple, the list has one element and it is 0.
With list it returns False, but tuple returns True.
+ 1
Mirielle, you miss my point.
all([]) returns True because the itreable is empty, as document indicated.
all([False, False, False]) returns False because the itreable contains at least one False value.
And we all agree with that.
The interesting part is all([[False, False], [False, False], [False, False]]) return True.
It passes a list of lists to the all() function, and each individual list has False only.
If we take individual list to the all() function it returns False, but if it is inside a list, all() evaluate the most outer list and returns True.
In my original post, I was asking why all([(2, 2)]) return True.
At first I was thinking tuple (2, 2) is not a conditional expression so it cannot be True.
Of course it turns out my assumption was wrong because bool((2, 2)) returns True.
+ 1
Bob_Li ,
Well, give it some consideration anyway, for readability and friendliness. Discussions have a range of readers. Copying and pasting is not trivial. Discussions have many posts. There are many discussions. If we all adopt the convention of documenting our own code posts, we save each other and every non-posting reader from having to duplicate that effort.
+ 1
Rain
The interesting part is for all([]) or all(()) returning True.
"if the iterable is empty, return True"
It feels counter to an empty list being Falsey... there is nothing in an empty list or tuple, so why should all([]) be True? isn't an len==0 falsey?
There is an SO discussion here:
https://stackoverflow.com/questions/11979683/why-JUMP_LINK__&&__python__&&__JUMP_LINK-built-in-all-function-returns-true-for-empty-iterables
+ 1
Bob_Li ,
I think of all() as meaning no_members_are_false() rather than all_members_are_true(). The behavior makes sense but the name is perhaps poorly matched to that behavior.
[Edit] It's kind of like how str.upper() means ensure_no_copied_members_are_lower() rather than ensure_all_copied_members_are_upper().
+ 1
Rain
I always think all() is a collection of and conditions, while any() is for or conditions.
+ 1
Wong Hei Ming ,
Yeah, and the name any() is well matched to its behavior, meaning any_member_is_true().
0
print(all((0,))) returns False.
also
print(any((0,))) returns False.
you are getting mixed up 😅
0
Flase
0
Bob_Li ,
I wish you would comment the output of every print statement.
0
"""
Prove all() examines members of iterable, not members of members of iterable, etc.
"""
#help(all) # uncomment to reproduce
"""
Help on built-in function all in module builtins:
all(iterable, /)
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
"""
# The documentation-only use of
# / above means parameters
# to its left are positional.
#help(bool) # Too long; uncomment to read.
print("example one") # example one
print(bool(False)) # False
print(
all(
[False, False]
)
) # False
print("example two") # example two
print(bool([False])) # True
print(
all(
[
[False],
[False],
]
)
) # True
print("example three") # example three
print(bool([False, False])) # True
print(
all(
[
[False, False],
[False, False],
]
)
) # True