+ 1

What is the output of this code? And explain how?

my_list=[([]), [()]] for i in my_list: print(bool(i), end=" ")

14th Jun 2020, 12:37 PM
Vikas Mishra
Vikas Mishra - avatar
1 Antwort
+ 7
First some basics. Round braces on its own, ie. empty round braces, creates a tuple. Round braces around something groups it together and indicates precedence, It says, first evaluate this, then the braces dissapear. (3+2)*2 5*2 10 Side note: Round braces with a seperating comma also creates a tuple, as in ([ ] ,) we don't have this in your example The bool of an empty list is False The bool of a list containing anything is True. my_list=[([]), [()]] for i in my_list: print(i, type(i), bool(i)) #I changed it a bit output: [ ] list False <- Empty list [( )] list True <- list containing a tuple
14th Jun 2020, 1:06 PM
Louis
Louis - avatar