Difference Between is and == in Python

While working on a Python project for US clients, I often come across two operators, is and ==, that seem similar but have distinct purposes. In this tutorial, I will explain the difference between is and == in Python. Let us understand more about the topic with examples and screenshots of executed example code.

‘==’ in Python

The == operator is used to compare the values of two objects. It checks whether the values are equivalent, regardless of whether they are the same object in memory.

Example

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b) 

Output:

True
False

I have executed the above example code and added the screenshot below.

Difference Between is and == in Python

In the example above, a and b are two different lists that contain the same values. The == operator returns True because the values are the same. However, the is operator returns False because a and b are different objects in memory.

ReadJavaScript vs Python for Web Development: Choosing the Right for Your Project

‘is’ in Python

The is operator checks for object identity. It returns True if two references point to the same object in memory.

Example

a = [1, 2, 3]
b = a
print(a == b) 
print(a is b)  

Output:

True
True

I have executed the above example code and added the screenshot below.

Difference Between is and == in Python example

In this example, a and b are references to the same list object. Both == and is return True because they refer to the same object in memory.

Check out Difference Between “is None” and “== None” in Python

Key Differences

  1. Purpose: == checks for value equality, while is checks for identity.
  2. Usage: Use == when you need to compare the values of objects. Use is when you need to check if two references point to the same object.
  3. Behavior with Immutable Types: Immutable types like integers and strings, is might seem to behave like == due to Python’s internal optimizations, but this is not guaranteed.

Examples

1. Compare Strings

a = "hello"
b = "hello"
print(a == b)  
print(a is b)  

Output:

True 
True

I have executed the above example code and added the screenshot below.

Difference Between is and == in Python Compare Integers

For small strings, Python may use a technique called interning, where it reuses objects for efficiency. This can make is return True for small strings with the same value. However, this behavior should not be relied upon.

Read Python 3 vs Python 2

2. Compare Lists

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)  
print(a is b)

Output:

True
False

Lists are mutable, so a and b are different objects even if they contain the same values. Thus, == returns True, but is returns False.

Check out Difference Between = and == in Python

3. Compare Integers

a = 1000
b = 1000
print(a == b) 
print(a is b)  

Output:

True
False

Here, a and b have the same value, so == returns True. However, they are different objects in memory, so is returns False.

Read Jax Vs PyTorch

3. Use is with None

A common use case for is is to check if a variable is None.

a = None

if a is None:
    print("a is None") 

Output:

a is None

Here, is is used to check if a is None, which is more efficient and idiomatic than using ==.

Read Is Python an Interpreted Language

Summary Table

Aspect==is
PurposeChecks value equalityChecks object identity
UsageCompare values of objectsCheck if references point to the same object
Common Use CasesComparing numbers, strings, listsChecking if a variable is None
Behavior with Immutable TypesReturns True if values are equalMay return True due to interning, but not guaranteed

Conclusion

In this tutorial, I explained the difference between is and == in Python. We discussed what are “==” and ‘is‘ in Python with examples some key differences. I gave examples by comparing strings, lists, and integers, by using 'is' with None and given a summary for reference.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.