Immutability of Floats, Ints and Strings in Python

Nikunj nikunjbadjatya at gmail.com
Fri Nov 25 06:24:37 EST 2016


Hi All,

Out of curiosity, I wanted to understand the reason behind having different memory location for two identical floats . This is unlike ints or strings. Tried googling but couldn't find anything concrete. Any links or references would be appreciated!

Example:

For FLOATS:
==========

>>> l = 1.3
>>> id(l)
140421602788216

>>> k = 1.3
>>> id(k)
140421602788240

>>> k == l
True

>>> k is l
False

For INTS and STRINGS:
=================
>>> i = 2
>>> o = 2
>>> id(i), id(o)
(140421602779712, 140421602779712)

>>> i is o
True

>>> a1 = 'hi'
>>> a2 = 'hi'
>>> a1 is a2
True



Thanks.



More information about the Python-list mailing list