I know that python has an automatic garbage collector and so it should automatically delete variables when there are no more reference to them.
My impression is tha
It's important to keep two concepts separate: names and values. A variable in Python is a name referring to a value. Names have scope: when you define a local variable (by assigning a value to a name), the variable's scope is the current function. When the function returns, the variable goes away. But that doesn't mean the value goes away.
Values have no scope: they stick around until there are no more names referring to them. You can create a value in a function, and return it from that function, making a name outside the function refer to the value, and the value won't be reclaimed until some future point when all the references to it have gone away.
More detail (including pictures!) is here: Facts and Myths about Python Names and Values.