python tutorial

Python Is Keyword

Learn more about Python, one of the world’s most versatile and popular programming languages.

Oftentimes while writing conditional logic, developers have to compare for equality of two variables or values. Equality operator == is always used for such comparisons. While == is used for testing whether two variables are the same or not, is keyword in Python will compare whether two values refer to the same object or not.

One key difference to note while deciding on whether to use == operator or is keyword is:

  • Use == sign for comparing whether the values are the same.
  • Use is keyword for comparing objects equality. It is very useful when comparing whether non-primitive values like objects and arrays are equal or not.
  • Values like strings, numbers, and booleans are stored in the same type of memory called stack and hence using == sign is a very appropriate choice.
  • Values like objects and arrays are actually stored in a different memory called heap and hence a direct comparison of objects and arrays that look identical will still end up being unequal.

Let’s explore the difference between == operator and is keyword in the code snippet below.

Code Example
>>> value_one = True
>>> value_two = False
>>> value_three = True

>>> greeting_one = 'hello'
>>> greeting_two = 'world'
>>> greeting_three = 'hello'


# comparing for equality using == operator compares if the values in variables are same
>>> print('value_one == value_two: ', value_one == value_two)
value_one == value_two:  False
>>> print('value_one == value_three: ', value_one == value_three)
value_one == value_three:  True
>>> print('greeting_one == greeting_two: ', greeting_one == greeting_two)
greeting_one == greeting_two:  False
>>> print('greeting one == greeting three: ', greeting_one == greeting_three)
greeting one == greeting three:  True

# values that are not objects and arrays are stored in same type or memory called stack
# and hence compares equal even when is keyword is used
>>> print('value_ one is value_two: ', value_one is value_two)
value_ one is value_two:  False
>>> print('value_one is value_three: ', value_one is value_three)
value_one is value_three:  True
>>> print('greeting_one is greeting_two: ', greeting_one is greeting_two)
greeting_one is greeting_two:  False
>>> print('greeting_one is greeting_three: ', greeting_one is greeting_three)
greeting_one is greeting_three:  True

As seen above, comparing values that are not objects and arrays using == operator and is keyword results in the same outcome as values like strings and booleans are stored in the same memory type called stack.

Code Example
>>> array_one = [1, 3, 5]
>>> array_two = [1, 3, 5]
>>> object_one = { 'firstName': 'John', 'lastName': 'Doe' }
>>> object_two = { 'firstName': 'John', 'lastName': 'Doe' }

# comparing for equality using == operator compares if the values in variables are same
>>> print('array_one == array_two: ', array_one == array_two)
array_one == array_two:  True
>>> print('object_one == object_two: ', object_one == object_two)
object_one == object_two:  True

# values that are objects and arrays are stored in different type of memory called heap
# and hence compares not equal even when 'is' keyword is used
>>> print('array_one is array_two: ', array_one is array_two)
array_one is array_two:  False
>>> print('object_one is object_two: ', object_one is object_two)
object_one is object_two:  False

As seen above, comparing values that are objects and arrays and have the same values using the == operator returns true as it only compares the values, and the values are indeed equal. However, when compared using the is keyword, the values are compared to be referring to the same object or array and because the objects and arrays are stored separately in heap, they are compared as unequal.

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required