HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonTip

What is the difference between Python's equality operators?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
equalitywhatoperatorsbetweendifferencethepython

Problem

Python provides two very similar equality operators used for comparisons:
  • The double equals (==), also known as the equality operator
  • The is keyword, also known as the identity operator


Although similar to one another, the double equals (==) and the is keyword are used for different comparison purposes and yield different results.
The main difference between the two is that the is keyword checks for reference equality while the double equals (==) operator checks for value equality. In other words, is will return True if two variables both refer to the same object in memory (aka. identity), whereas the double equals operator will evaluate to True if the two objects have the same value.

Solution

a = [1, 2, 3]
b = a
c = [x for x in a]

print([
  a == b, # True
  a is b, # True
  a == c, # True
  a is c  # False
])

x = 'hi'
y = x
z = 'HI'.lower()

print([
  x == y, # True
  x is y, # True
  x == z, # True
  x is z  # False
])


  • The is keyword, also known as the identity operator


Although similar to one another, the double equals (==) and the is keyword are used for different comparison purposes and yield different results.
The main difference between the two is that the is keyword checks for reference equality while the double equals (==) operator checks for value equality. In other words, is will return True if two variables both refer to the same object in memory (aka. identity), whereas the double equals operator will evaluate to True if the two objects have the same value.
Here are some examples to clear up any confusion:

Code Snippets

a = [1, 2, 3]
b = a
c = [x for x in a]

print([
  a == b, # True
  a is b, # True
  a == c, # True
  a is c  # False
])

x = 'hi'
y = x
z = 'HI'.lower()

print([
  x == y, # True
  x is y, # True
  x == z, # True
  x is z  # False
])

Context

From 30-seconds-of-code: identity-equality

Revisions (0)

No revisions yet.