A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-object-comparison-is-vs/ below:

Python Object Comparison : "is" vs "=="

Python Object Comparison : "is" vs "=="

Last Updated : 26 Feb, 2025

In Python, both is and == are used for comparison, but they serve different purposes:

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

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

Explanation: a and b are separate list objects with identical values [1, 2, 3]. == checks value equality and returns True, while is checks memory identity and returns False.

'is' operator

The is operator checks if two variables refer to the same object in memory, rather than just having equal values. It returns True only if both variables point to the exact same object in memory.

Example:

Python
x = [10, 20, 30]
y = x    # y points to the same memory location as x

print(x is y)

Explanation:

== operator

The == operator checks if two objects contain the same values, regardless of whether they are stored in the same memory location.

Example:

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

print(a == b) # same values

Explanation:

is vs == Summary Table

Parameter

== operator

is operator

Name

Equality operator

Identity operator

Functionality

Checks if values of two objects are equal.

Checks if memory addresses of two objects are the same.

Use Case

Used when we want to compare data stored in objects.

Used when we want to check whether two variables point to the same object in memory.

Mutable Objects (lists, dicts, sets, etc.)

Returns True if contents are the same, even if they are different objects.

Returns False unless both variables point to the same memory location.

Immutable Objects (ints, strings, tuples, etc.)

Returns True if values are equal.

May return True due to Python's internal object caching (interning).

Example 1

[1,2,3] == [1,2,3] → True

[1,2,3] is [1,2,3] → False

Example 2

"hello" == "hello" → True

"hello" is "hello" → True



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4