Added code for lecture 2026-11-10
This commit is contained in:
parent
5b630435ae
commit
07b9e54bf0
52 changed files with 1248 additions and 1 deletions
31
lecture20261110/listmethods/list03MethodShallowCopy.py
Normal file
31
lecture20261110/listmethods/list03MethodShallowCopy.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
# 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)}")
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue