Member-only story

Understanding the Difference Between == and is in Python

Javascript Jeep🚙💨
3 min readJun 2, 2024
Photo by Michele Wales on Unsplash

In Python, == checks if two things have the same value, while is checks if they are the same object in memory.

The == Operator

The == operator is used to compare the values of two objects to check if they are equal. When you use ==, Python checks if the values held by the objects are the same, regardless of whether they are the same instance in memory.

Examples of == Operator

# Comparing integers
a = 10
b = 10
print(a == b) # Output: True

# Comparing strings
str1 = "hello"
str2 = "hello"
print(str1 == str2) # Output: True

# Comparing lists
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 == list2) # Output: True

In each of these cases, the == operator returns True because the values of a and b, str1 and str2, and list1 and list2 are the same, even though they might be different objects in memory.

The is Operator

The is operator, on the other hand, checks for identity. This means it checks if two references point to the same object in memory. When you use is, Python verifies whether the two operands refer to the same object, not just if their values are equal.

Examples of is Operator

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Javascript Jeep🚙💨
Javascript Jeep🚙💨

No responses yet

Write a response