0
>>> a = 123
>>> print(id(a))
94366784571712
>>> b = 123
>>> print(id(b))
94366784571712

But, when I change the value to 345 it gives different results

>>> a = 345
>>> print(id(a))
139794845105136
>>> b = 345
>>> print(id(b))
139794845103472

Please can anyone explain why was it giving the same value for 123?

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Talha Tayyab
  • 2,102
  • 9
  • 14
  • 25
  • 3
    This might help: [why does variable's address change in python when the variable is assigned a different value?](https://stackoverflow.com/questions/45911631/why-does-variables-address-change-in-python-when-the-variable-is-assigned-a-dif) – Bhavyadeep Yadav May 22 '21 at 15:52
  • 1
    TL;DR Python caches integers in the range [-5, 256] – Tomerikoo May 22 '21 at 18:18

0 Answers0