Monday 31 July 2017

46 Shallow and Deep Copy In Python

aaa
'''
colours1 = ["red", "green","blue"]
colours2 = colours1
colours2 = ["rouge", "vert","viol"]
print (colours1)
print(colours2)

##########http://www.python-course.eu/deep_copy.php

colours1 = ["red", "green"]
colours2 = colours1
colours2[1] = "blue"
print(colours1)
'''
'''
list1 = ['a','b','c','d']
list2 = list1[:]
list2[1] = 'x'
print (list2)
#['a', 'x', 'c', 'd']
print (list1)
#['a', 'b', 'c', 'd']
'''


lst1 = ['a','b',['ab','ba']]
lst2 = lst1[:]
lst2[0] = 'c'
lst2[2][1] = 'd'
print(lst2)
print((lst1))
#['a', 'b', ['ab', 'd']]

No comments:

Post a Comment