Understanding when to use is, ==, is not, and not
2024
1. is
vs ==
is
- Purpose: Checks identity.
- It determines whether two variables refer to the same object in memory.
- Used primarily for checking singleton objects (
None
,True
,False
), or ensuring two variables point to the exact same instance.
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, because `b` refers to the same object as `a`.
print(a is c) # False, because `c` is a different object, even if it has the same value.
==
- Purpose: Checks equality.
- It determines whether two variables have the same value, regardless of whether they refer to the same object.
Example:
pythonCopy codeprint(a == b) # True, values are the same.
print(a == c) # True, values are the same.
When to Use:
is
: Use when you need to ensure two variables reference the exact same object.- Example:
if a is None:
(checks for identity with theNone
singleton).
- Example:
==
: Use when you need to compare values.- Example:
if content_type == "JSON":
(checks for value equality).
- Example:
2. is not
vs not
is not
- Purpose: Checks identity negation.
- It determines whether two variables do not refer to the same object.
Example:
x = None
if x is not None: # Checks if `x` is not the `None` singleton.
print("x is not None")
not
- Purpose: Negates a boolean expression.
- Converts
True
toFalse
andFalse
toTrue
.
Example:
x = False
if not x: # Equivalent to `if x == False:`
print("x is False or evaluates to False")
y = [1, 2, 3]
if not y: # Evaluates to False because the list is not empty.
print("List is empty")
When to Use:
is not
: Use for identity negation, especially for checking if a variable is notNone
,True
, orFalse
.- Example:
if x is not None:
- Example:
not
: Use to negate the truthiness of an expression.- Example:
if not x:
(to check ifx
is falsy).
- Example:
Comparison Chart
Operator | Checks | When to Use |
---|---|---|
is | Identity: same object in memory | For checking singletons like None , or ensuring two variables reference the same object. |
== | Equality: same value | For comparing values of variables, like strings, numbers, or objects with the same content. |
is not | Identity negation: not the same object | For ensuring a variable does not reference a specific singleton or object. |
not | Negates the truthiness of an expression | For negating conditions or checking falsy values (None , False , empty containers, etc.). |
Examples of Usage
Checking for None
x = None
if x is None: # Correct, checks identity with `None`.
print("x is None")
if x == None: # Works, but less Pythonic. Avoid in practice.
print("x equals None")
Comparing Objects
a = [1, 2, 3]
b = a
c = [1, 2, 3]
if a is b: # True, `b` is the same object as `a`.
print("a and b are the same object")
if a == c: # True, `a` and `c` have the same value.
print("a and c have the same value")
if a is not c: # True, `a` and `c` are different objects.
print("a and c are not the same object")
Negating a Condition
x = False
if not x: # Equivalent to `if x == False:`
print("x is False")
y = []
if not y: # Checks for an empty list (falsy value).
print("y is an empty list")
Key Notes
- Identity vs Equality:
- Use
is
andis not
for identity checks (e.g.,None
,True
,False
). - Use
==
and!=
for comparing values.
- Use
- Negations:
- Use
not
to invert the truthiness of any expression. - Use
is not
for negating identity checks.
- Use
- Falsy Values:
- In Python,
None
,False
,0
,""
,[]
,{}
, andset()
are considered falsy. Usenot
to check these effectively.
- In Python,