2026fa_cse160/lecture20261110/listmethods/list03MethodShallowCopy.py

31 lines
1 KiB
Python

# https://www.w3schools.com/python/python_ref_list.asp
# https://www.quackit.com/python/reference/python_3_list_methods.cfm
programMessage = '''
A shallow copy is a new name for an existing object. This copy demonstrates
this principle.
1.) Create a list.
2.) Create a new name and set it equal to the original list.
3.) Show the id's of the objects are the same.
4.) Append a value to one then show the other name shows the same list.
'''
print(programMessage)
basicList = [1, 34, 99, 87, 14]
print(f"Before copy {basicList}")
myList = basicList
print(f"After copy {basicList} ==> {myList}")
print(f"After copy {id(basicList)} <= Same ID => {id(myList)}")
print("\n")
print("Append a value to basicList. Examine the change to myList\n")
#basicList = [1, 34, 99, 87, 14]
myList = basicList
print(f"Before append to basicList {basicList}")
basicList.append(777)
print(f"basicList - After append {basicList}")
print(f"myList - After append {myList}")
print(f"After append {id(basicList)} <= Same ID => {id(myList)}")