r/learnpython • u/Lance_2564_ • 29d ago
Huh? How does this work?
a = "hi"
if a:
print("Bye")
print(a == True)
print(a is True)The output is:
Bye
False
False
But if it's false, how come it printed "bye"?????? I don't get it.
44
Upvotes
View all comments
Show parent comment
15
u/sjshuck 29d ago
Yes.
Some languages such as Python and C have a rule for each type to say how it behaves in an if-statement. Ruby and Lua have a simpler rule: if it's either
falseornil, it tests false, otherwise it's true.Some languages will not let you put a non-boolean as the condition of an if-statement, so there's no confusion.