I am currently using python 3.6, and I was playing around with the id() function. When I run the following code in IDLE,
x = 1
print(id(x), id(1))
The two memory addresses are the same. (1499456272 for me) My understanding is the integer 1, which is an object, has a memory address, and when the object is assigned to x, the variable gains the same memory address of the object. (not sure if this is correct)
When I replicate the above code using a string, for instance
s = "a"
print(id(s), id("a"))
I also get two memory addresses which are the same. Again, my current reasoning for why this occurs is the same as above.
However, when I try this using lists, I don't get the same memory address. For example,
l = [1]
print(id(l), id([1]))
gives me 1499456272 and 67146456.
Can anyone explain to me why this occurs? Perhaps my current reasoning for why ints and strings have the same memory address is flawed. Thanks :D