1 == True but 1 is not True: The Truth About True

Today I was playing around with Python and stumbled on some truths (pun intended π).
I realized I was a bit fuzzy on when to use ==
vs is
. So let's explore this a bit!
You've probably seen this before:
x = True
print(x is True) # β
True
x = 1 == 1
print(x is True) # β
Still True!
Why? Because 1 == 1
evaluates to the boolean True
, so x is actually the True
object.
Integers are truthy too:
print(1 == True) # β
True!
So far so good!
But now try this:
x = int(1)
print(x is True) # β False!
Wait β what?! Didnβt we just show that 1 == True
?
And now x = int(1)
, but this is False
?
That's because, even though 1 is truthy, itβs not the same object as True
.
But it does have the same value as True
.
To recap :
1 == True
β (same value)1 is True β
(not the same object)type(1) != type(True)
βint
vsbool
Thus:
Use
==
for value checksUse is for identity checks (like
None
,True
, orFalse
)
Subscribe to my newsletter
Read articles from Asha Asvathaman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
