I'm new in Python (new as in it's been 2 days since I first began learning about it) but I'm somewhat already experienced in C and I was learning about mutability and immutability when my professor told me that integers and other data types in Python are immutable. He showed "proof" of this by assigning an int to a variable, showing the memory address, modifying the variable and showing the memory address in it again which was modified.
I began experimenting with this, and found the following:
>>> a = 5
>>> id(a)
140708610844592
>>> a = 6
>>> id(a)
140708610844624
>>> # jumps 32 bits
>>> a = 5
>>> id(a)
140708610844592
>>> #Same address 'a' declared the first time
>>> b = 5
>>> id(b)
140708610844592
>>> #Same address as a
>>> c = [3,4,5,6]
>>> id(c)
2168086594816
>>> id(c[2]) #index of 5
140708610844592
>>> #same address for 5 but memory not contiguous data type 'list' is a pointer?
>>> c = [3,4,5,6]
>>> id(c)
2168086406272
>>> #Even though list is the same, memory changes which is not happening for int
>>> x = 7
>>> id(x)
140708610844656
>>> 140708610844656 - 140708610844624 #address of 'x' minus address of when 'a' was 6
32
>>> # mem in 6 and 7 are contiguous
>>> id(8) #Value not declared before
140708610844688
>>> 140708610844688 - 140708610844656 #address of 8 minus address of x
32
>>> # x and 8 are contiguous, ints pre-declared?
>>> z = (3,4,5,6) #tuple
>>> id(z)
2168086643872
>>> id(z[2])
140708610844592 #Always same location for int 5
>>> z = (3,4,5,6)
>>> id(z)
2168086750288 #same tuple sequence, different address, same happened with list
>>> id(100)
140708610847632
>>> 140708610847632 - 140708610844592 #Address in 100 minus address in 5
3040
>>> 3040/32 #number of bits between size of int
95.0 # between 5 and 100 exist 95 consecutive ordered ints
>>> st = "hello"
>>> id(st)
2168086495280
>>> id(st[0]) #address of character 'h'
2168051747440
>>> st = "hell"
>>> id(st)
2168086498416
>>> id(st[0])
2168051747440
>>> st = "hello"
>>> id(st)
2168086498352
>>> id(st[0])
2168051747440 #address of 'h' remains the same but str 'hello' address different
So, based on this my questions are obvious.
Do integers and other characters already come predefined and variables are just assigned their memory address depending on these values?
Memory address for tuples, str, lists keep changing even when the same secuences are declared.
So from my point of view if we consider as immutable every data type that changes address when redefined, then practically every data type variable is immutable except for predefined integers and chars that basically do not change their location in memory.
Therefore a list containing int elements is as immutable as a tuple.
Or does mutability/immutability only refers to if the elements in a secuence can be modified or not, which in case an int it's neither mutable or not because it's an object itself, not a secuence.
And also, data types list, tuple, etc are just pointers pointing at other addresses? unlike arrays it does not seem that their elements are together but in different locations, like a linked list.
I hope you guys can help me clarify this.