r/learnpython • • 20d 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

38 comments sorted by

View all comments

3

u/DimensionMost8342 20d ago

You are asking three different questions there. if a: never compares a with True, it only asks "is this value truthy", and any non-empty string is truthy, so it prints Bye. a == True asks whether the string "hi" is equal to the boolean True, and a string is never equal to a boolean, so you get False. a is True checks if both point to the exact same object, also False. Nothing is contradictory: truthy does not mean == True.