Understanding Variable Assignment and Deep vs. Shallow Copy in Python 🌟

Understanding Variable Assignment and Deep vs. Shallow Copy in Python 🌟


πŸ–‹ Variable Assignment in Python

In Python, variables act as references to objects in memory. When you assign x = 3, Python:

  1. Creates the object 3 in memory.

  2. Associates the variable x with this memory address.

Example:

x = 3
y = "hello"
num = y  # Assigns the value of y ("hello") to num
y = 5    # Overwrites y with 5

Behavior:

  • num holds "hello" even after y is reassigned, as num references the original value of y.

  • Reassigning y does not affect num.

Quiz Recap:

x = 3
y = "hello"
num = y
y = 5
print(num)  # Output: hello
print(y)    # Output: 5

Key Takeaway: Variables only update their value when explicitly reassigned.


πŸ” Shallow Copy vs. Deep Copy

Python provides two main ways to copy objects: shallow copy and deep copy. These are critical when working with mutable data types like lists, dictionaries, and other nested objects.


1️⃣ Shallow Copy

A shallow copy creates a new object but copies references to the elements within the original object. Changes to the elements in the copied object affect the original if those elements are mutable.

Example:

original_list = [1, 2, 3]
shallow_copy = original_list[:]
shallow_copy[0] = 10

print(original_list)   # Output: [1, 2, 3]
print(shallow_copy)    # Output: [10, 2, 3]
  • Changing shallow_copy does not affect original_list because the top-level objects are independent.

  • However, if the list contains nested mutable objects, those shared references will reflect changes.

Example with Nested Objects:

original_list = [[1, 2], [3, 4]]
shallow_copy = original_list[:]
shallow_copy[0][0] = 10

print(original_list)   # Output: [[10, 2], [3, 4]]
print(shallow_copy)    # Output: [[10, 2], [3, 4]]

Why? The top-level list is copied, but the nested lists inside are shared references.


2️⃣ Deep Copy

A deep copy creates a completely independent copy of the object and recursively copies all nested objects. Changes to the deep copy do not affect the original object.

Example:

import copy

original_list = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(original_list)
deep_copy[0][0] = 10

print(original_list)   # Output: [[1, 2], [3, 4]]
print(deep_copy)       # Output: [[10, 2], [3, 4]]

Why? Each nested list is duplicated, so modifications deep_copy do not propagate to original_list.


πŸ”§ Understanding Memory Allocation

  1. Immutable Objects (e.g., integers, strings, tuples):

    • Copied by value.

    • Each variable assignment creates a new reference.

    x = 10
    y = x
    y = 20
    print(x)  # Output: 10
  2. Mutable Objects (e.g., lists, dictionaries, sets):

    • Copied by reference in a shallow copy.

    • Deep copies break this reference by duplicating nested objects.


πŸ› οΈ Best Practices for Copying Objects

  1. Use Shallow Copy:

    • When the object contains no nested mutable objects.

    • For performance efficiency when you don’t plan to modify the copy’s contents.

  2. Use Deep Copy:

    • When working with complex nested data structures.

    • When you need a fully independent duplicate.


πŸ“Œ Key Takeaways

  1. Variable Assignments:

    • Assignments create references to objects.

    • Reassigning a variable does not affect other variables referencing the original value.

  2. Shallow Copy:

    • Creates a new object but shares references for nested objects.

    • Changes to mutable nested objects affect the original.

  3. Deep Copy:

    • Recursively duplicates all objects, ensuring complete independence.

  4. Memory Allocation:

    • Immutable objects are copied by value.

    • Mutable objects are copied by reference unless explicitly deep copied.


πŸŽ“ Quiz Time

Question:

What will the following code output?

import copy

original = [[1, 2], [3, 4]]
shallow = original[:]
deep = copy.deepcopy(original)

shallow[0][0] = 99
deep[1][0] = 88

print(original)

Answer:

[[99, 2], [3, 4]]
  • shallow[0][0] = 99: Modifies original because the nested list is shared.

  • deep[1][0] = 88: Does not modify original because it’s a deep copy.


πŸš€ Next Steps

In the next section, we’ll delve into control structures like if-else statements and loops, allowing you to write dynamic, interactive programs. Stay tuned! 😊

Last updated