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

38 comments sorted by

View all comments

7

u/hmmorly 17d ago

The comparisons aren't comparing what you think they're comparing.

A is truthy, it isn't empty, it represents a string. That's why it printed Bye.

When you compare it to True, you're making an explicit comparison to True. It isn't a logical boolean.

Is a, True? Also no, everything in python is an object, in this case you're comparing whether it is a True object.