Answer to Question #248587 in Python for Raymond Ekwubiri

Question #248587
Describe the difference between objects and values using the terms “equivalent” and “identical”. Illustrate the difference using your own examples with Python lists and the “is” operator.

Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.

Finally, create your own example of a function that modifies a list passed in as an argument. Describe what your function does in terms of arguments, parameters, objects, and references.

Create your own unique examples for this assignment. Do not copy them from the textbook or any other source.
1
Expert's answer
2021-10-08T23:51:58-0400


In python programming value objects are identical to variables in that they are used for testing identity of objects based on the value. Is operator is used to test and identity the object identity. Implementation of variables can reuse a single instance of an object.


In Equivalence, value of objects can change within the scope of a mutable list while variables represent the relations between the objects. An object contains both value and identity which is identical to a variable that contains data of the object.


The scope of an immutable object can not be changed. Is operator is used for objects that are identical while === is used for objects that have an equivalence. The operator is returns true if objects pointing to the same object are identical.


The following example returns false since the objects are not equivalent.


a1 = [1, 2, 3, 4]


a2 = [2, 3, 4, 5]


print(a1 == a2)


Example 2 returns true since they are identical.


s1 = "sun"

s2 = "sun"


print(s1 is s2)


An alias relationship is said to exist between references of objects if they refer to the same object allocated in memory during execution. A set is a pair of references that satisfy alias relationships between objects.



a1 = [1, 2, 3, 4]


a2 = [2, 3, 4, 5]


# alias


a2 = a1


a2.append("7")

a1.append("3")


print(a1)


A function that accepts list as argument and returns a modified list.


def modify_list(ls):

a = ls

b = a


print('b', b)

print('a', a)

# reverse

a.reverse()

# print

print('b', b)

print('a', a)

# new list

a = [1, 4, 6, 10, 8, 3]

print('b', b)

print('a', a)


modify_list([1, 2, 3, 4, 5])





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS