mardi 16 janvier 2018

Why "if" statement is executed even when the condition is False?

Ok this might be trivial but for clarity, I am still asking it:

In [1]: s = "abc"

In [2]: type(s)
Out[2]: str

In [3]: if s:
   ...:     print("works")
works

In [4]: s == True
Out[4]: False

So a string is not equal to True and even then the if thinks that it is True. Explicitly checking whether s == True returns False.

In [6]: if not s:
   ...:     print("works")

So here for the if statement, not s is False! What is happening?

Aucun commentaire:

Enregistrer un commentaire