-
-
Save IndhumathyChelliah/5d7f123c592c7daaa275e3f67dd801a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
d={'Name':'karthi', | |
'Rollno':12, | |
'Marks':[100,90] | |
} | |
d1=d.copy() | |
print (d1) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90]} | |
#modifying immutable data types | |
d['Rollno']=5 | |
print (d) #Output: {'Name': 'karthi', 'Rollno': 5, 'Marks': [100, 90]} | |
#changes are not reflected in copied dictionary. | |
print (d1) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90]} | |
# modifying mutable datatypes | |
d['Marks'].append(99) | |
print (d) #Output: {'Name': 'karthi', 'Rollno': 5, 'Marks': [100, 90, 99]} | |
#changes are reflected in copied dictionary also. | |
print (d1)#Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]} | |
d2=copy.deepcopy(d1) | |
print (d2) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]} | |
#modifying immutabe data types | |
d1['Rollno']=15 | |
print (d1) #Output:{'Name': 'karthi', 'Rollno': 15, 'Marks': [100, 90, 99]} | |
#changes are not reflected in copied dictionary. | |
print (d2) #Output: {'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]} | |
# modifying mutable datatypes | |
d1['Marks'].append(101) | |
print (d1) #Output: {'Name': 'karthi', 'Rollno': 15, 'Marks': [100, 90, 99, 101]} | |
#changes are not reflected in deep copied dictionary. | |
print (d2)#Output:{'Name': 'karthi', 'Rollno': 12, 'Marks': [100, 90, 99]} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment