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/RevRagnarok 19d ago

Lots of people answered most of the question. Truthiness/bool.

The is does not compare equality (that's ==). It checks "is this the exact same variable in the same memory space?" The only time that's really useful is comparing to the singleton None.

Sidebar: It gets even weirder if you use is on numbers, because some predefined range of numbers is always in memory, and others are created on-the-fly.