Skip to main content

Shallow Copy and Deep Copy in Python पायथन में गहरी प्रतिलिपि और उथली प्रतिलिपि

In Python, the concepts of deep copy and shallow copy refer to how data structures, like lists, are duplicated.

पायथन में, गहरी प्रतिलिपि और उथली प्रतिलिपि की अवधारणाएं संदर्भित करती हैं कि कैसे डेटा संरचनाएं, जैसे सूचियां डुप्लिकेट की जाती हैं।

1.) Shallow Copy उथली प्रतिलिपि :-  A shallow copy in Python creates a new object, but instead of creating copies of nested objects, it copies references to those objects. This means that if the original object is modified, changes to nested objects will reflect in both the original and the shallow copy.
पायथन में एक उथली प्रतिलिपि एक नई वस्तु बनाती है, लेकिन नेस्टेड वस्तुओं की प्रतियां बनाने के बजाय, यह उन वस्तुओं के संदर्भों की प्रतिलिपि बनाती है। इसका मतलब यह है कि यदि मूल वस्तु को संशोधित किया जाता है, तो नेस्टेड वस्तुओं में परिवर्तन मूल और उथली प्रतिलिपि दोनों में दिखाई देंगे।

You can create a shallow copy of a list using Slicing, copy method and copy module:-
आप स्लाइसिंग, कॉपी विधि और कॉपी मॉड्यूल का उपयोग करके किसी सूची की एक उथली प्रतिलिपि बना सकते हैं:-

original_list = [1, 2, [3, 4]]
shallow_copied_list = original_list[:]

original_list = [1, 2, [3, 4]]
shallow_copied_list = original_list.copy()

import copy
shallow_copied_list = copy.copy(original_list)

Example उदाहरण:-

import copy

# Original list with a nested list
original_list = [1, 2, [3, 4]]

# Creating a shallow copy
shallow_copied_list = copy.copy(original_list)

# Modifying a nested object in the original list
original_list[2][0] = 'changed'

print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Shallow Copied List:", shallow_copied_list)  # Output: [1, 2, ['changed', 4]]

# Modifying the outer object in the shallow copy
shallow_copied_list[1] = 'new_value'

print("After modifying shallow copy:")
print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Shallow Copied List:", shallow_copied_list)  # Output: [1, 'new_value', ['changed', 4]]


2.) Deep Copy गहरी प्रतिलिपि:- A deep copy in python creates a new object and recursively copies all objects found within the original. This means that changes to nested objects in the original will not affect the deep copy. 
पायथन में एक गहरी प्रतिलिपि एक नया ऑब्जेक्ट बनाती है और मूल के भीतर पाए गए सभी ऑब्जेक्ट को पुनरावर्ती रूप से कॉपी करती है। इसका मतलब यह है कि मूल में नेस्टेड ऑब्जेक्ट में परिवर्तन से डीप कॉपी पर कोई प्रभाव नहीं पड़ेगा। 

We can create a deep copy using the `copy` module:-
हम 'कॉपी' मॉड्यूल का उपयोग करके एक गहरी प्रतिलिपि बना सकते हैं:-

import copy
deep_copied_list = copy.deepcopy(original_list)

Example उदाहरण:-

import copy

# Original list with a nested list
original_list = [1, 2, [3, 4]]

# Creating a deep copy
deep_copied_list = copy.deepcopy(original_list)

# Modifying a nested object in the original list
original_list[2][0] = 'changed'

print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Deep Copied List:", deep_copied_list)  # Output: [1, 2, [3, 4]]

# Modifying the outer object in the deep copy
deep_copied_list[1] = 'new_value'

print("After modifying deep copy:")
print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Deep Copied List:", deep_copied_list)  # Output: [1, 'new_value', [3, 4]]

Comments

Popular posts from this blog

Python IDLE पायथन आईडीएलई (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट)

IDLE (Integrated Development and Learning Environment) is a graphical user interface (GUI) for the Python programming language. It includes the Python standard library and provides a convenient environment for writing, running, and debugging Python code. IDLE (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट) पायथन प्रोग्रामिंग भाषा के लिए एक ग्राफिकल यूजर इंटरफेस (GUI) है। इसमें पायथन मानक लाइब्रेरी शामिल है और पायथन कोड लिखने, चलाने और डीबग करने के लिए एक सुविधाजनक वातावरण प्रदान करता है।  Key features of IDLE are:- आईडीएलई की मुख्य विशेषताएं हैं:-  1. Interactive Shell:- IDLE provides an interactive Python shell, allowing you to execute Python commands and see the immediate results. 1. इंटरएक्टिव शेल:- आईडीएलई एक इंटरैक्टिव पायथन शेल प्रदान करता है, जो आपको पायथन कमांड निष्पादित करने और तत्काल परिणाम देखने की अनुमति देता है।  2. Script Editor:- It includes a text editor for writing Python scripts. You can create, edit, and save Python files with a ".py" extension. 2. स्क्...

Operator's in Python पायथन में ऑपरेटर्स

Python supports various types of operators for performing operations on variables and values. Here are some common types of operators in Python: पायथन वेरिएबल्स और मानों पर संचालन करने के लिए विभिन्न प्रकार के ऑपरेटरों का समर्थन करता है। यहां पायथन में कुछ सामान्य प्रकार के ऑपरेटर हैं: 1. Arithmetic Operators:    + # Addition     - # Subtraction    * # Multiplication    / # Division    % # Modulus (remainder)    ** # Exponentiation    // # Floor Division 2. Comparison Operators:        == # Equal to    != # Not equal to    < # Less than    > # Greater than    <= # Less than or equal to    >= # Greater than or equal to     3. Logical Operators:       and # Logical AND    or # Logical OR    not # Logical NOT    4. Assignment Operators:    ...

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...