0
How is the output False, True?
print(bool(([]))) print(bool([()])) output: False True
8 Antworten
+ 4
You can figure this out by running this code
x = ([])
y = [()]
print(x, type(x), len(x))
print(y, type(y), len(y))
x is an empty list
y is a list with one element which is an empty tuple
+ 9
Shantanu ,
i have added some more samples how to create tuples. see also my comments in the file.
https://code.sololearn.com/cTQEKS67wPIM/?ref=app
+ 3
It's the rule in python. Empty containers are falsey.
https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/
+ 3
Shantanu
what you should concentrate on is that while:
a = ()
creates an empty tuple,
a = ([])
creates an empty list.😁
if you want a tuple containing an empty list, you would have to write
a = ([],)
or
a = [],
tuple is the one with the strange syntax.
() for empty, but only comma "," matters if there are elements.
a = 1, 2, 3
is the same as
a = (1, 2, 3)
and
a = ( , ) is an error.😲
+ 2
Tibor Santa why does empty list give False as output?
+ 1
Tibor Santa Thank you 😃
+ 1
Lothar Thank you 😃
0
Bob_Li Thank you 😃