Fundamentals 9 min read

Understanding the Difference Between == and is in Python

This article explains how the == operator compares object values while the is operator checks object identity in Python, provides detailed usage scenarios, and includes practical code examples for basic types, composite types, None, and API testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding the Difference Between == and is in Python

In Python, the == operator compares the values of two objects, while the is operator checks whether they are the same object in memory.

== operator

It returns True when the contents of two objects are equal, working for basic types (int, str) and composite types (list, dict).

# compare integers
print(3 == 3)  # True
# compare strings
print("hello" == "hello")  # True
# compare lists
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)  # True

is operator

It returns True only when both operands refer to the exact same object.

# compare integers (small integers may be cached)
a = 3
b = 3
print(a is b)  # True
# compare strings (short strings may be cached)
s1 = "hello"
s2 = "hello"
print(s1 is s2)  # True
# compare lists
a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)  # False
# same reference
c = a
print(a is c)  # True

Why can is sometimes return True?

Python caches small integers and short strings, so distinct literals may share the same memory address.

# large integer
a = 257
b = 257
print(a is b)  # False
# short string
s1 = "hello"
s2 = "hello"
print(s1 is s2)  # True

Usage scenarios

Basic type comparison : use == to compare values, is to check identity.

Composite type comparison : use == for content equality, is for identity.

Comparing None : both == and is work because None is a singleton.

Comprehensive example

a = 3
b = 3
print(a == b)  # True
print(a is b)  # True

a = 257
b = 257
print(a == b)  # True
print(a is b)  # False

s1 = "hello"
s2 = "hello"
print(s1 == s2)  # True
print(s1 is s2)  # True

l1 = [1, 2, 3]
l2 = [1, 2, 3]
print(l1 == l2)  # True
print(l1 is l2)  # False

l3 = l1
print(l1 is l3)  # True

n1 = None
n2 = None
print(n1 == n2)  # True
print(n1 is n2)  # True

Test examples for API automation

Example 1: compare JSON response data using == for content and is for object identity.

import requests
import json
def test_json_response():
    response = requests.get("https://api.example.com/data")
    expected_data = {"status": "success", "message": "Data retrieved."}
    assert response.json() == expected_data, "JSON content does not match."
    same_data = expected_data
    assert expected_data is same_data, "Objects are not the same."

test_json_response()

Example 2: compare HTTP status code values.

from enum import Enum
class HttpStatusCode(Enum):
    OK = 200
    CREATED = 201
    BAD_REQUEST = 400
    UNAUTHORIZED = 401
    NOT_FOUND = 404
    INTERNAL_SERVER_ERROR = 500

def test_http_status_code():
    response = requests.get("https://api.example.com/data")
    expected_code = HttpStatusCode.OK.value
    assert response.status_code == expected_code, "Status code does not match."
    assert response.status_code is HttpStatusCode.OK.value, "Status code is not OK."

test_http_status_code()

Example 3: verify object reference.

import requests
def test_object_reference():
    response = requests.get("https://api.example.com/data")
    data = response.json()
    new_data = data
    assert data == new_data, "Data dictionaries do not match."
    assert data is new_data, "Data objects are not the same reference."

test_object_reference()

Example 4: compare None values.

import requests
def test_none_values():
    response = requests.get("https://api.example.com/data")
    data = response.json()
    key = "missing_key"
    assert key not in data, f"Key '{key}' should not exist."
    assert data.get(key) == None, f"Key '{key}' should be None."
    assert data.get(key) is None, f"Key '{key}' should be None."

test_none_values()

Example 5: compare dictionary key‑value pairs.

import requests
def test_dict_key_value():
    response = requests.get("https://api.example.com/data")
    data = response.json()
    expected_value = "some_value"
    assert data["key"] == expected_value, "Key value does not match."
    same_value = expected_value
    assert expected_value is same_value, "Values are not the same object."

test_dict_key_value()

In summary, == checks for value equality, while is checks for identity; using them correctly in automated tests improves verification of API responses.

TestingcomparisonEqualityidentity
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.