3

I'm learning how python stores variables and refers it with names in Python and I came across something that I couldn't make sense of. I assigned same value to var1 and var2 and saw that these variables refers same object. And I created another two variable named var3 and var4 and assigned same value (but different from before) and I saw that objects that these variables refers isn't the same. Can someone explain the difference in this?

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> var1 = 100
>>> var2 = 100
>>> var1 is var2
True
>>> var3 = 500
>>> var4 = 500
>>> var3 is var4
False
>>> id (var1)
491903584
>>> id (var2)
491903584
>>> id (var3)
56573264
>>> id (var4)
87458544
>>>
>>> vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'var1': 100, 'var2': 100, 'var3': 500, 'var4': 500}
bylds
  • 87
  • 1
  • 9

0 Answers0