[go: up one dir, main page]

0% found this document useful (0 votes)
5 views17 pages

Df Program 6,

In Python, equivalent objects have the same value, while identical objects refer to the same memory location. The document provides examples demonstrating these concepts using lists and aliasing, highlighting how modifications to aliases affect the original object. It also explains in-place modifications through a custom function that reverses a list and appends its mirrored version.

Uploaded by

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

Df Program 6,

In Python, equivalent objects have the same value, while identical objects refer to the same memory location. The document provides examples demonstrating these concepts using lists and aliasing, highlighting how modifications to aliases affect the original object. It also explains in-place modifications through a custom function that reverses a list and appends its mirrored version.

Uploaded by

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

Equivalent vs.

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

list_a = [1, 2, 3] list_b =


[1, 2, 3]
list_c = list_a
print(list_a == list_b) #
Equivalent? True
(same values)
print(list_a is list_b) #
Identical? False
(different objects)
print(list_a is list_c) #
Identical? True (same
object)

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.reverse() alters the


original list object.

Ÿ lst.extend() appends
mirrored data to the
same object.

Downey, A. (2015). Think


Python: How to think like a
computer scientist. Green Tea
Press. This book is licensed
under Creative Commons
Attribution-NonCommercial 3.0
Unported

You might also like