Df Program 6,
Df Program 6,
Identical in
Python
In
Python, equivalent obj
ects have the same
value/content,
while identical objects
are the same object in
memory.
Example 1:
Lists and
Comparisons
python
Output explanation:
list_a ==
list_b is True because
their values match.
list_a is
list_b is False because
they are separate
objects.
list_a is
list_c is True because li
st_c references the
same object as list_a.
Objects,
References,
and Aliasing
Objects: Data
structures in memory
(e.g., ``).
References: Variables
pointing to objects
(e.g., list_a).
Aliasing: Multiple
references to the same
object.
Example 2:
Aliasing and
Mutability
python
original = ["apple",
"banana"] alias =
original # Aliasing: both
reference the same
object copy =
original.copy() # New
object with same
values
alias.append("cherry")
print(original) # Output:
["apple", "banana",
"cherry"] print(copy) #
Output: ["apple",
"banana"]
Explanation:
Modifying alias affects
original because they
reference the same
object.
copy remains
unchanged because it’s
a separate object.
Function
Modifying a List
Argument
Example 3: In-
Place List
Reversal with
Custom Logic
python
def mirror_modify(lst):
"""Reverses the list and
appends its mirrored
version.""" # Modify the
original list in-place
lst.reverse() mirrored =
lst.copy()
mirrored.reverse()
lst.extend(mirrored)
numbers = [10, 20, 30]
mirror_modify(numbers
) print(numbers) #
Output: [30, 20, 10, 10,
20, 30]
Technical
Breakdown:
Arguments/
Parameters:
numbers is passed
to mirror_modify as an
argument.
lst becomes a
parameter referencing
the same object
as numbers.
In-Place Modification:
lst.extend() appends
mirrored data to the
same object.