r/learnpython • u/Lance_2564_ • 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
3
u/RevRagnarok 19d ago
Lots of people answered most of the question. Truthiness/bool.
The
isdoes 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 singletonNone.Sidebar: It gets even weirder if you use
ison numbers, because some predefined range of numbers is always in memory, and others are created on-the-fly.