How is the output False, True? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

How is the output False, True?

print(bool(([]))) print(bool([()])) output: False True

29th Oct 2022, 3:07 PM
Shantanu
Shantanu - avatar
8 Respostas
+ 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
29th Oct 2022, 4:16 PM
Tibor Santa
Tibor Santa - avatar
+ 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
29th Oct 2022, 6:46 PM
Lothar
Lothar - avatar
+ 3
29th Oct 2022, 4:36 PM
Tibor Santa
Tibor Santa - avatar
+ 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.šŸ˜²
29th Oct 2022, 10:15 PM
Bob_Li
Bob_Li - avatar
+ 2
Tibor Santa why does empty list give False as output?
29th Oct 2022, 4:31 PM
Shantanu
Shantanu - avatar
+ 1
Tibor Santa Thank you šŸ˜ƒ
29th Oct 2022, 4:38 PM
Shantanu
Shantanu - avatar
+ 1
Lothar Thank you šŸ˜ƒ
30th Oct 2022, 6:22 AM
Shantanu
Shantanu - avatar
0
Bob_Li Thank you šŸ˜ƒ
30th Oct 2022, 6:23 AM
Shantanu
Shantanu - avatar