Variables are storing references to the computer memory locations.
"""Variables as references
When you assign a value to a variable, you are creating that value
in computer memory, and store a reference to it
When you assign new value to a, reference to a doesn't affect b

a = 42; b = a; a = 100

assert a == 100
assert b == 42

print('Tests passed')


When you copy a list, you are copying the list (because lists are mutable).
""" References are different with lists
Lists are mutable
Even the code touches only A list ...
both A and B are changed!
Values stored in A and B both refer to the same list

A = [1, 2, 3]; B = A; A[1] = 'x'

assert A == [1, 'x', 3]
assert B == [1, 'x', 3]

print('Test passed')


Python makes automatic garbage collection.
"""Python's automatic garbage collection
Delete any values not being referred to by any variables
Manual memory management in other programming languages ...
is a common source of bugs.
The getrefcount function returns the number of references

import sys

a = 'somevalue'
assert sys.getrefcount(a) == 4
assert sys.getrefcount('somevalue') == 4

b = a; c = b
assert sys.getrefcount(a) == 6

del a; del b; del c
assert sys.getrefcount('somevalue') == 3 # Look Here

print('Tests passed')

  Last update: 233 days ago