0% found this document useful (0 votes)
4 views

Copy Techniques in Python

The document explains two copy techniques in Python: Shallow Copy and Deep Copy. Shallow Copy creates a new object with the same content but different memory addresses, while Deep Copy creates a new object that shares the same memory address as the original. Examples illustrate how modifications to one object affect the other in each technique.

Uploaded by

bb7608758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Copy Techniques in Python

The document explains two copy techniques in Python: Shallow Copy and Deep Copy. Shallow Copy creates a new object with the same content but different memory addresses, while Deep Copy creates a new object that shares the same memory address as the original. Examples illustrate how modifications to one object affect the other in each technique.

Uploaded by

bb7608758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

================================================

Copy Techniques in Python


================================================
=>In Python Programming, we have Two Types of Copy Techniques. They are

1. Shallow Copy
2. Deep Copy
-----------------------------------------------------------------------------------
------------------------------------
1. Shallow Copy
-----------------------------------------------------------------------------------
------------------------------------
=>The Properties of Shallow Copy are
a) Initial Content of Both the Objects are SAME
b) The Memory Address of Both the Objects are DIFFERENT
c) The Modifications are Indepedent ( Whatever the changes we do on one
object,
Which are not reflected to another object )
=>To Implement the Shallow Copy Technique, we use copy().

Syntax: object2=object1.copy()
Examples:
-------------------------
>>> l1=[10,"Rossum",23.45]
>>> print(l1,id(l1))------------[10, 'Rossum', 23.45] 1996956062656
>>> l2=l1.copy() # Shallow Copy
>>> print(l2,id(l2))------------[10, 'Rossum', 23.45] 1996959406400
>>> l1.append("Python")
>>> l2.insert(1,"Nether")
>>> print(l1,id(l1))-----------[10, 'Rossum', 23.45, 'Python'] 1996956062656
>>> print(l2,id(l2))-----------[10, 'Nether', 'Rossum', 23.45] 1996959406400
-----------------------------------------------------------------------------------
------------------------------------
2. Deep Copy
-----------------------------------------------------------------------------------
------------------------------------
=>The Properties of Deep Copy are
a) Initial Content of Both the Objects are SAME
b) The Memory Address of Both the Objects are SAME
c) The Modifications are Depedent ( Whatever the changes we do on one
object,
Which are reflected to another object and both of them are
pointing same memory space)

=>To Implement the Deep Copy Technique, we use Assignment Operator ( = )

Syntax: object2=object1

Examples:
---------------------
>>> l1=[10,"Rossum",23.45]
>>> print(l1,id(l1))-----------------[10, 'Rossum', 23.45] 1996956038208
>>> l2=l1 # Deep Copy
>>> print(l2,id(l2))----------------[10, 'Rossum', 23.45] 1996956038208
>>> l1.append("Python")
>>> print(l1,id(l1))---------------[10, 'Rossum', 23.45, 'Python'] 1996956038208
>>> print(l2,id(l2))---------------[10, 'Rossum', 23.45, 'Python'] 1996956038208
>>> l2.insert(1,"Nether")
>>> print(l1,id(l1))--------------[10, 'Nether', 'Rossum', 23.45, 'Python']
1996956038208
>>> print(l2,id(l2))--------------[10, 'Nether', 'Rossum', 23.45, 'Python']
1996956038208
========================x==================================

You might also like