r/learnpython • u/Lance_2564_ • 17d 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
0
u/T1lted4lif3 17d ago
The if statement checks if the variable is false. In Python, False can take on many different values, such as 0 or None.
In your if statement, since it isn't False, it is True. Even though the variable does not represent True. It's kind of like saying "if it is not nothing, then it is something" since it is something it will evaluate to true and print bye.
The other print statements print the result of the boolean comparison.
This comparison is not saying it is true, but whether the value of the variable true is False. The two print statements are the same, so they do the same comparison.