# In python, variables refer to objects...
# Everything is an object, but only some objects
# can be modified.
a=9 # a refers to the "9" object, but nine is always nine
b=a # b now refers to the same object as a, which is "9"
b=7 # now b refers to the "7" object and no longer to "9"
# lists can be modified:
a=[9] # a refers to the list currently containing the "9" object
b=a # b now refers to the same list as a
b[0]=7 # change the 0th item (which is the only item) in the list from 9 to 7
print(a) # note we are printing a, not b
print(b)
# Why did this print 7? Because a and b referred to the same
# list object, so when it was modified through the b reference
# the same list referenced by a was also the modified list.
# What if we want b to be a copy of a?
import copy
a=[9]
b=copy.deepcopy(a) # now b is a copy of a rather than referring to a's object too
b[0]=7
print('using copy:')
print(a)
print(b)
To embed this project on your website, copy the following code and paste it into your website's HTML: