Python is a highly object-oriented programing language, In fact, every item in python is considered an object of a specific class in python.
Python variables are references or pointers to an object.
This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.
Now, if we consider,
Here python will not create another object but just point to the same object by creating a new pointer alone.
Now, if we change the value of m,
Now, python creates an integer object 400, and m is made as a reference to it.
Suppose we are changing,
Now, a string object is created with the value foo, and m is made as a reference to that object.
Here, if you notice there is no reference for the integer object with the value 300 and no way we can access it.
When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated memory. This process is garbage collection.
Still, not clear about the above topic? You can watch this tutorial for a clear idea of Memory Allocation in python | Episode-3 | Pythonista_Geek | Shravanthi | Basics of python
For an example implementation, Memory Allocation with example in python | Episode-4 |Pythonista_Geek | Shravanthi |Basics of python
I hope, this article gave a clear idea about the internal process of variables in python.