[Tutor] How memory works in Python

David L Neil PyTutor at DancesWithMice.info
Tue Jan 7 14:04:45 EST 2020


On 8/01/20 3:44 AM, Deepak Dixit wrote:
> Hi Team,
> 
> I was thinking on the memory management in Python but little confused here
> because when I assign same value of type number and string to different
> variable, it points to same memory but when I do it for list or tuple it
> points to different memory locations.
> 
> Here is the code and the output:
> 
> # test.py
> x1 = 10
> y1 = 10
> 
> x2 = "Hello"
> y2 = "Hello"
> 
> x3 = [10]
> y3 = [10]
> 
> x4 = (10,)
> y4 = (10,)
> 
> print id(x1) == id(y1)
> print id(x2) == id(y2)
> print id(x3) == id(y3)
> print id(x4) == id(y4)
> 
> deepak at Deepak-PC:~$ python test.py
> True
> True
> False
> False
> 
> 
> Can you help me to understand how it works?


You may find http://pythontutor.com/visualize.html#mode=edit helpful.
NB no formal relationship with this list (to my knowledge)

It is designed as a learning aid by illustrating storage allocations and 
usage.
-- 
Regards =dn


More information about the Tutor mailing list