The is operator for large integers in Python

In Python, we use the is operator for an object’s identity comparison. It checks whether two variables refer to the same object in memory. If the objects are identical, is returns True; otherwise, it returns False.

Here’s a code example:

x = 100
y = 100
print(x is y) # Output: True

In the code above, we use the is operator to compare x and y and we get the output True. We can check the ids of both variables using the id() function as shown in the following widget:

x = 100
y = 100
print(id(x))
print(id(y))

The ids (memory addresses) of x and y are the same because the id() function in Python returns the unique identifier of an object. It provides the memory address to where the object is stored in the computer’s memory.

Integer caching

Python caches and reuses the same objects for the commonly used integers ranging from -5 to 256 to optimize memory usage and improve performance. This process is known as integer caching. So, when we use the is operator to compare two integer literals or variables within this range, we get True if they have the same value.

Here’s a code example that illustrates this behavior:

x = 100
y = 100
print(x is y) # Output: True
print(id(x))
print(id(y))
x += 100
y += 100
print(x is y) # Output: True
print(id(x))
print(id(y))
x += 100
y += 100
print(x is y) # Output: True
print(id(x))
print(id(y))

Explanation

  • Lines 1–2: We assign the value 100 to the variables x and y.

  • Line 3: We check if x and y refer to the same object in memory using the is operator. Both variables contain the same value, which is an integer and since small integers are typically cached in memory, they refer to the same object. Therefore, the output will be True.

  • Lines 4–5: We print the memory addresses (identity) of the objects referred to by x and y. Since x and y both contain the same integer value, they refer to the same object and so, the memory address will also be the same.

  • Lines 6–7: We increment the values of x and y by 100. After these lines, both x and y will have the value 200.

  • Line 8: We again check if x and y refer to the same object in memory. Since both variables now contain the value 200, they still refer to the same object. Therefore, the output will be True.

  • Lines 9–10: We print the memory addresses (identities) of the objects referred to by x and y. Since x and y both contain the same integer value, they refer to the same object. Therefore, the memory address will be the same.

  • Lines 11–12: We increment the value of x and y again by 100. After these lines, x and y will have the value 300.

  • Line 13: We again check if x and y refer to the same object in memory. Since both variables now contain the value 300, which is out of the range -5 to 256, the output will be False.

  • Lines 14–15: We print the memory addresses (identities) of the objects referred to by x and y. In this case, the memory addresses are different due to the increment.

Identity comparison of variables and literal values

As we have already seen, assigning values exceeding the range of -5 to 256 will output False, when we use the is operator to compare them. It is the same for literal values. Here’s a code example:

x = 300
y = 300
print(x is y) # Output: True
print(300 is 300) # Output: True

Explanation

In the above example, the x and y values exceed the range -5 to 256, but the output is still True because Python keeps an array of integer objects for all integers between -5 and 256. When we create an int in that range, we actually get back a reference to the existing object. But when we increment a variable with a constant value, and if the value of the integer exceeds the range -5 to 256 after the increment, they become separate objects in the memory. So, the output will then be False.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved