0% found this document useful (0 votes)
36 views3 pages

Discussion Assignment Unit6

The document outlines a discussion assignment focused on distinguishing between 'equivalent' and 'identical' objects in Python, using lists and the 'is' operator for illustration. It explains concepts of objects, references, and aliasing, emphasizing how modifications to a list through one reference affect all aliases. Additionally, it includes a function example that modifies a list, highlighting the relationship between arguments, parameters, and references.

Uploaded by

SAYILE JAMES
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)
36 views3 pages

Discussion Assignment Unit6

The document outlines a discussion assignment focused on distinguishing between 'equivalent' and 'identical' objects in Python, using lists and the 'is' operator for illustration. It explains concepts of objects, references, and aliasing, emphasizing how modifications to a list through one reference affect all aliases. Additionally, it includes a function example that modifies a list, highlighting the relationship between arguments, parameters, and references.

Uploaded by

SAYILE JAMES
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/ 3

Discussion Assignment

Welcome to the Unit 6 Discussion Forum!

Use the terms "equivalent" and "identical" to distinguish between objects and values.
Illustrate the difference further using your own examples with Python lists and the “is” operator.

Using your own Python list examples, explain how objects, references, and aliasing relate to
one another.

Finally, create your own example of a function that modifies a list passed in as an argument.
Hence, 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.

The code and its output must be explained technically whenever asked. The explanation can be
provided before or after the code, or in the form of code comments within the code. For any
descriptive type question, Your answer must be at least 150 words.

End your discussion post with one question related to programming fundamentals learned in
this unit from which your colleagues can formulate a response or generate further discussion.
Remember to post your initial response as early as possible, preferably by Sunday evening, to
allow time for you and your classmates to have a discussion.

When you use information from a learning resource, such as a textbook, be sure to credit your
source and include the URL. Continue to practice using APA format for citations and references.

Solution

In Python, an object is a data structure that has a state and behavior. The state of an object is
represented by its attributes, and the behavior of an object is represented by its methods.
A value is a piece of data that can be assigned to a variable. Values can be numbers, strings, lists,
dictionaries, or other objects.
Two objects can be equivalent if they have the same value while Two objects can be identical if
they are the same object.
For example, the following two lists are equivalent, since they have the same element though not
identical, since they aren’t the same object. This can be proved by using the “is” statement, from
the below the output “false” will be printed. Since the two lists are not identical
list1 = [1,2,3]
list2 = [1,2,3]

print(list1 is list2)
Output

The following two lists are identical, since they have the same element and the same object. This
can be proved by using the “is” statement, from the below the output “true” will be printed.
Since the two lists are identical
list1 = [1,2,3]
list2 = list1

print(list1 is list2)
Output

When a variable is assigned to an object, a reference to the object is created. The reference is a
pointer to the object in memory. Two variables can refer to the same object. This is called
aliasing.
For example, the following code creates two variables that refer to the same list:
list1 = [1,2,3]
list2 = list1
The two variables, list1 and list2, are aliases of each other. They refer to the same object in
memory.
When you modify a list through one of its aliases, the change is reflected in all of the aliases.
For example, the following code modifies the list through the alias list2:
list1 = [1,2,3]
list2 = list1

list2.append(4)
The list is now [1, 2, 3, 4].The change is reflected in both list1 and list2.
list1 = [1,2,3]
list2 = list1

list2.append(4)

print("List2 is :", list2)


print("List1 is :", list1)
Output
When a function is called, the arguments are passed to the function as references. This means
that the function can modify the arguments.
For example, the following function modifies the list that is passed to it:
def modify_list(list):
list.append(4)#introduces the number 4 into the list [1,2,3]

list= [1, 2, 3]
modify_list(list)#Calls the modify function

print(list)

Output

The function modify_list modifies the list that is passed to it. The change is reflected in the
original list.

In conclusion, objects, references, and aliasing are important concepts in Python. It is important
to understand these concepts in order to write efficient and correct code.

References

Py4e-int - https://runestone.academy/ns/books/published/py4e-int/lists/objectsValues.html

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy