+ 1

please explain this Python code

why does it execute the first if statement, if x is not true? https://code.sololearn.com/cvfSqJLMaqYI/?ref=app

23rd Apr 2019, 8:16 PM
Kerry Layguev
Kerry Layguev - avatar
4 Respuestas
+ 4
In Python if x: You are alreading checking if x condition is True. Like Dragos explained x is true. Continuing You shouldn't do if x == True: In this situation you are asking if x and True have the same reference and off course it doesn't.
23rd Apr 2019, 9:06 PM
Anya
Anya - avatar
+ 3
x is not an empty list. It contains an empty tuple. So x is True.
23rd Apr 2019, 8:27 PM
Dragos
Dragos - avatar
+ 3
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.  In this case 1 equals False and 2 equals True
23rd Apr 2019, 11:43 PM
BroFar
BroFar - avatar
+ 1
Thanks for the answers. Finally I've got it. I should ask if bool(x) == True and not if x == True.
24th Apr 2019, 2:39 AM
Kerry Layguev
Kerry Layguev - avatar