Professional Documents
Culture Documents
Search forums
The cut-off date for posting to this forum is reached so you can no longer post to it.
Discussion Unit 6
by Leonidas Papoulakis (Instructor) - Wednesday, 10 November 2021, 7:11 AM
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.
92 words
Permalink
Answer
Description:-
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 1/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Illustration:-
Object identity
Object Equivalency
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 2/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Answer
Description
As I
described in question #1 an Object in the list is a variable that is also a memory location. The Object
references a value in
memory location so that the interpreter uses it to do
some computation. This process is called object reference. When two
variable references a single value it
is called aliasing. It happens when a
variable "a" references to a value in a memory
location, i.e., a=[2,4,6,8,10], and b
is assigned to an i.e., b=a. then both variables are referencing a single memory
location
(Downey A, 2015, p. 96).
Illustration
Aliasing
Therefore,
let’s test that using The "is" operator if it evaluates to True.
3.
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.
Answer
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 3/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
out
it as it is. The third one is it will sort the list using the built-in function sort()
and is accessed using a variable name and
dot operator. Then prints out the
copied list which is passed as an argument to the function. The fourth
one is sorting using
the sorted() built-in function. This function is referenced by another variable called srtd_lst
and the original list is passed as an
argument to the built-in function sorted().
Then the value is printed out. Therefore, the script looks as follows.
def only_upper_case(t):
res = []
for s in t:
if s.isupper():
res.append(s)
print('[output 5]', 'Copy of sorted Upper Case Only List from function call=', res)
return res
xx = ['S', 'A', 'M', 'R', 'A', 'W', 'I', 'l', 'O', 'O', 'k', 's', 'H', 'a', 'P', 'p', 'Y'] # List is created here
print('[output 1]', 'Original List=', xx) # This prints the original List
print('[output 2]', 'Copy & unsorted List=', xx) # Prints the copied list as it is.
cp.sort() # Sort builtin functions is accessed here using var name with dot operator and sorts the copied list
srtd_lst = sorted(xx) # A new variable srtd_lst is assigned (referenced) a sorted builtin functions
# and the sorted function is then passed the original list xx argument is passed to sort the list
print('[output 3]', 'Sorted List from sorted(function)=', srtd_lst) # prints out the sorted original list
print('[output 4]', 'Copy of List & from sort(function)=', cp) # prints out the sorted copied list
only_upper_case(cp) # The function that prints the upper case is called and passed a parameter that contains
Reference
Downey,
A. (2015, p. 96 ). Think Python: How to Think Like a Computer Scientist
(2nd ed., Version 2.4.0). Needham: Green
Tea
Press.
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 4/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
906 words
Tags:
Discussion Forum Unit 6
Great in depth and concise post from you as per usual. I like your use of stack diagram pictures to explain the concepts.
Great description of how an object is allocated into memory and great use of a handful of the built in python functions we
have used in recent assignments. Keep up the good work
56 words
Hello Samrawi, the expanation was clear and informative. I liked that you used pictures and diagrams in your explanation
which make it more simple to read and understand.
28 words
Hi Samrawi,
Great Job on the details and illustration of your discussion. I always understand things I did not get on my own from your
work. You have a great career ahead in this field! Keep it up....
38 words
Nicely done, I can tell you spent a lot of time on this. It's a lot longer than mine and more in-depth. I like how you bolded
the important words. Great work.
32 words
Great answer it helped me to fully understand the topic, this week was very confused for me but your answer helped me to
make the topic clear.
Nicely done!
29 words
This is an in-depth explain of the terms “equivalent” and “identical.” Keep of the great job.
16 words
7 words
Hello Samrawi, I really learn a lot from your unique perspective and the presentation.
14 words
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.
Python programming language as we learnt earlierin unie one, is a formal language meaning that it has a well defined
structure that must be adhered strictly to. And this structure like other programming langages uses objects to represent data.
Which is why it is saşd to be object oriented. (Downey A., 2015)
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 6/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Now an objectfg is a collection of data (variables) and methods (functions defined within a class) that act on those data.
According to pythons docymentary. All data in a python program is represented by objrcts or by relations between objects.
. an identity
. a type
. a value
An objects identity is it address in memory. lt is uniqe and allocated at the point of instantiating object. The object type
determines the operations that an object supports and the possible values the object will accept and work with.
Now, the 'is' operator compares the identity of two objects; the id()function returns an integer representing its identity. While
the '=='operator checks for equivalence between two objects. Both operators returnvalue type is bool which means, they
either return a True or False value.
The following is a sample code to illustrate the difference between these operators;
Code:
==============================
c = a
def checks():
print("ID of a is",id(a))
print("ID of b is",id(b))
print("ID of c is",id(c))
print()
#returns True | False because both are equivalent but different identities.
print(a == b,"|",a is b)
#returns both True | True because both are equivalent and have the same identity.
print(a == c,"|",a is c)
#returns True | False because both are equivalent but different identities
print(b == c, ''|",b is c)
checks()
Output:
==============================
ID of a is 489093885704
ID of b is 489086264456
ID of c is 489093885704
True | False
True | True
True | False
[Program finished]
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 7/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
The program above comprises of three lists and a function. The function is doing tree things;
First, it prints out the IDs of the three lists which are defined globally.
Secondly, it checks for equivalence between the given objects and outputs the result.
And thirdly it also checks the objects (in this case, lists) if they are identical using the 'is' operator.
As you can see, the ID of 'c' is same as that of 'a' which makes them identical and that is because we assigned 'a' to 'c' using
the assignment operator (=). On the contrary, 'b' is not identical thus the ID check returned False.
However, all three lists are equivalent because they all have the same value in their current state. But when the value of any of
the lists changes, they seize to be equal. This property of lists is called mutability (i. ability to change value).
Describe the relationship between objects , references , and aliasing. Again, create your own examples with Python lists.
"Like I mentioned earlier, when objects are created, they are created with atype and a value assigned to them. Another
important thing that happens isthat they are given a name or an identifier.
Now, the name of a variable for example, points to address in memory wherethe value of that variable is stored. For instance,
in the variable x = 5,'x' is the reference to address in memory where the value of the object(which is 5) is stored with type 'int'.
In a situation where the same memory location can be accessed using morethan one name is referred to as aliasing. For
instance, we have a variable,x = 5 if we then assign x to y;"
x=5
#assign variable x to y
y=x
This means that variables x and y are all pointing to the same object. And we can check this using the is operator
x is y
#returns
True
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.
Code:
==============================
def modify(x, y):
a.append(45)
print(a)
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 8/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
print()
b.append(a)
print(b)
print()
b[2]=11
print(b)
print()
d=a+b
print(d)
print()
for i in a or c:
b.append(i)
print(b)
modify(a, b)
output:
==============================
[34, 23, 11 ]
[34,23,11,34]
[34,23,11,34,23]
[34,23,11,34,23,45]
[program finished]
The above program is a function defined with two parameters x and y. Thefunction is then called using lists a and b as
arguments.
The function is defined to test some operations that can be done on a list. As you see in the first statement, 'a.append(45)'
adds 45 to our list a.And 'b.append(a)' adds our list a to list b. List a becomes nested in list b. T he third operation, b[2]=11
changes the item with index 2, (which isour nested list a) to 11. The fourth d = a + b concatenates list a and b toform list d.
And in the fifth operation, the for loop does the same thingonly it adds the items of list b one at a time and prints the result
eachtime. Now earlier on we talked about references using variables as examples, inthe above function, the parameters
defined in the function and the names ofthose lists which we used as arguments to call the function are both alias because
they are both references to the same list objects and the are said to have been aliased
references
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 9/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
value. The other thing is you have typo errors and check your writing before you post.
56 words
Well researched and descriptive post. You covered all the requested criteria in the post. However please take care to run
your work through an advanced spelling and grammar checker such as grammarly, as although the spelling for some of
the mistaken words are correct it has changed the meaning of the sentence for example when explaining your first
program you wrote "when the value of any of the lists changes, they seize to be equal." which should be cease to be equal.
82 words
Hello Ahmet, everthing was great in your explanation you explained what is an object very well and the way of using the
identity function to expose the difference between the variable was very helpful to make it more clear.
39 words
Thanks for your input in this discussion. I appreciate the way you explained the concept clearly. Thanks . Keep up the good
work.
23 words
Good work, you explained it all well. It would be good to proof read your work though, there are a lot of spelling errors
and stuff. Also to fix the spacing on the code, it's hard to read and didn't copy paste right. I have to always adjust the
spacing on mine after I copy paste from my word document. Keep it up.
63 words
Hi Ahmet,
Excellent explanation even though you missed small portions of the discussion. I can see that you have a great
understanding of this weeks unit. Keep it up.
29 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 10/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Good job. You did answered the questions and touched on the relevant points
13 words
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.
An object refers to unique data belonging to a specific class, for example string or integers. Values refers to specific
assignment to a valuable. For instance x = 6 where x is the object and 6 is the value assigned to the valuable. Equivalent is an
operator that shows identity or equal value referring to an object . Equivalence may be used as a Boolean relation to indicate if
an object is equal to another . The result is always True or False. While is also an operator it does not show equivalence of
value rather it shows if two objects are the same. For example x is y.
Example 1
https://my.uopeople.edu/pluginfile.php/1521284/mod_forum/post/15093950/script%20example%201.py
Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.
https://my.uopeople.edu/pluginfile.php/1521284/mod_forum/post/15093950/example%202%20discussion.py
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.
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 11/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
https://my.uopeople.edu/pluginfile.php/1521284/mod_forum/post/15093950/question%202.py
237 words
Hello Simon, your works are great. Your explanation and illustrations are very clear and informative. The way you present it
is also great. Keep it up!
26 words
Great explanation of object, value, alias, reference and so on. Nice use of ide images to keep the program nicely formatted
and easily readable. I like how you showed that we can use == operator as well as "is"
39 words
Hello Simon, your explanation was informative and clear, I liked how the code and the output was organized in the picture,
you did a great job!
26 words
Permalink Show parent
Your explanation and illustrations are very clear and informative great job
11 words
I like the formatting you did with this, it's very clean and easy to read. It's also nice and short but gets all the info needed
out. Good work.
29 words
Hi Simon,
As always your illustrations and comment are very helpful and instrumental in helping to gain more knowledge and
practice. Thank you very much for your contribution.
28 words
9 words
The difference between objects and values is that objects store or holds values in them while values are of any type that can be
stored in an object.
The "is" operator is used to compare two objects which returns True if and only if the objects are identical
The objects are identical if and only if they contain the same values and are defined by the same variable name.
Example:
code:
x = ['hello',1,'world']
x = ['hello',1,'world']
print(x is x)
output: True
but suppose we have 2 objects which have the same values but a different variable name like:
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 13/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
code:
x = ['hello',1,'world']
y = ['hello',1,'world']
print(y is x)
output: False
In this case, these two objects are called "Equivalent" and in lists the “is” operator returns false if the objects are equivalent
(Downey, 2015).
------------------------------------------------------------------------------------------------------------------------------------------
The relationship between objects, references, and aliasing is that when we assign an object to a variable, this is called
referencing an object
Example:
x = [“Cars”,”Python”]
In the above statement here we are assigning x to the list [“Cars”,”Python”] and we say x refers to that list.
Example:
x = [“Cars”,”Python”]
y=x
------------------------------------------------------------------------------------------------------------------------------------------
def Example(L):
L1 = [1,1,1]
L3 = []
for i in L:
for j in L1:
L3 += [i+j]
break
return L3
print(Example([4,5,6]))
Explanation of my example:
In the above code, we have a function that takes an argument L which is a list of 3 integers as a parameter. Inside the function,
we assign the list object of the values [1,1,1] to a variable named L1 by reference so L1 refers to that object similarly for L3 we
assign it to an empty list. In the two loops we worked with the values in L and L1 add each one and store it in L3, then after the
loop finishes the function returns the list L3.
Reference:
Downey A. (2015). Think Python: How to think like a computer scientist, 2nd Edition. (p.96).
342 words
great work, the description and code are clean and easy to understand, but the last example took some time because it
was missing the input.
25 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 14/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
great work
2 words
Your input in unit six discussion is well appreciated. I have learned a number of things in this discussion. Thanks for your
input.
23 words
Dear Fouad, thanks for the concise and clear definition of the terms required as well as the detailed explanation of your
coding examples.
23 words
1. 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.
Answer: Objects are data variables defined by class and can be compared to determine if they are "equivalent" or "identical". A
sting value would be and 'Hello, World!' and a floating number would be 42.0.
When two operations are equal or not, the term equivalent is used, and the value "true" and is returned if they are equal.
Example:
z = x
def checks():
print("ID of x is",id(x))
print("ID of y is",id(y))
print("ID of z is",id(z))
print()
print(x == y,"|",x is y)
print(x == z,"|",y is z)
print(x == z,"|",y is x)
checks-()
Interpreter output
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 15/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
ID of x is 2316551449728
ID of y is 2316542558144
ID of z is 2316551449728
False | False
True | True
True | False
2. Describe the relationship between objects, references, and aliasing. Again, create your own
Answer:
The reference is use to point to the location where the object's value of type 'int' is stored. Aliasing is a term used to describe a
situation in which the same memory region can be accessed using multiple names.
Example
y=x
x is y
return True
The values of variable x are 19, 21, 24, and 28. As a result, we assign the same variable to y, implying that y refers to the same
value as x and that the values are not copied.
3. 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.
Example
z = [x,1,2,3,4,5]
y = x+z
y[2]=-8
def checks():
print("ID of x is",id(x))
print("ID of y is",id(y))
print("ID of z is",id(z))
print()
print(x == y,"|",x is y)
checks()
Interpreter Output:
ID of x is 1957386971968
ID of y is 1957418620544
ID of z is 1957378015168
False | False
False | False
False | True
This function uses reference values due that “y=x+z”, meaning that “y” was using the same value as “x” and “z” to output its
value.
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 16/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
References
Downey, A. (2015). Think Python| How to Think Like a Computer Scientist: Vol. Version 2.2.23 (2nd Edition). Green Tea Press.
https://my.uopeople.edu/pluginfile.php/1404469/mod_page/content/3/TEXT%20-%20Think%20Python%202e.pdf
449 words
Crystal,
Great work and i must say that the first example was flawless, I enjoyed reading your answer.
keep it !
21 words
1. 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.
Objects are considered to be an instance of a Class, the procedure used to confirm whether two objects are identical is by the
use of the ‘is’ operator.
On the other hand, a value is considered to be an object on which a variable can be assigned to it, a method to state that two
values/operands are equivalent or not, is by the usage of the ‘==’ operator and the code returns ‘true’.
The ‘is’ operator is used to compare and tell whether two objects are identical or not.
Example #1:
Input:
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 17/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Output:
Explanation:
In this case, the ‘is’ operator checked if the two variables that were assigned were directing to the same objects, as can be
observed list3 is directing to another object although is has the same data and considered ‘equivalent’ but it does no mean
that it is identical.
2. Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.
Aliasing refers to when there’s more than one reference for the same object that has an association to more than one variable.
In other words, if an element of the object is modified, both the variable that has the reference to the object will also be
modified.
Example#2:
Input:
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 18/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Output:
Explanation: as can be seen in the comments in the input section, list1 and list2 refers to the same object, when the elements
was modified with index 2, the object was changed and hence the variables were modified, in other words aliasing occurred.
3. 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.
Example:
Input:
Output:
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 19/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Explanation: In the example above the function receives a list and elements(string) were added, the function got the reference
to the list.
References:
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 (CC BY-NC 3.0).
404 words
Your explanation and examples are well appreciated for this discussion. I have learned quite a lot from your input. Thanks
alot.
21 words
Dear Wilmer,
Your answer is very well explained with concise examples, the definitions are simple and easy to understand.
19 words
Re: Discussion Unit 6
by Luke Henderson - Monday, 20 December 2021, 10:27 PM
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 20/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Since Python is known as an object oriented language, therefore in Python everything is an object. So when we can create a
variable, for example:
x = 10
We created an object(x) which has the value of 10. As we have seen in previous units it is possible to have multiple variables
with the same name, sometimes they may only be available in the scope of a function depending on how we declare them.
Even if their name is the same and their value is the same, that does not mean they are the same object. They may have the
same value which makes them equivalent but they are not the same object which means they are not identical(Downey, 2015).
We can check the truth behind these statements by using the Python functions “is” and “id()”. We use “is” to check if the values
are the same. When Python creates an object or reference it stores it in a specific address in memory. It is possible to use the
Python function id() to check that the specific address assigned to an object is the same as that of another object(Lenka, 2017).
Let's have a look at this in action.
Now here is a function I created to demonstrate the above concepts and its output placed side by side.
The function has the argument x which is just a placeholder for the list required. If any other input is given it will cause an error
since I use the append function which is exclusive to lists. When you call this function and pass a list to it as a parameter it will
assign that list to the object x and then append and sort the list. (Note it only works with a list of strings)
References
Lenka, C. (2017 November 25). id() function in Python. Geeks for Geeks. https://www.geeksforgeeks.org/id-function-python/
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 21/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Downey, A. (2015). Think Python: How to think like a computer scientist. This book is licensed under Creative Commons
Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0)
333 words
Hi, Luke, You have done great overall. You have explained in a clear and precise what an object and value together with
their relationship. Your illustration is also great. But it would be good if you were added some examples to show the
equivalence of those you explained.
48 words
Thank you for the post, i see you have used Geeks for Geeks, I'll have to check it out, the code is clean and easy to
understand, and the definition was helpful.
32 words
Hi, Luke, You have done a good job but I would suggest a better white space in-between lines of code for a clearer
screenshot.
24 words
objects hold data such as values, a value is the basic building blocks of any function such as numbers int or characters string,
or a boolean... etc.
Example1
>>>False
>>>mylist1 = mylist3
>>>true
A reference is the identifier used to access an object's value , aliasing is assigning that same value identifier to another object
variable,
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 22/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Example2
>>>my_list = [0, 1, 2] #my_list is the object referencing the list [0, 1, 2]
>>>id(my_list)
>>>4388520064
>>>his_list = my_list #using aliasing here both my_list and his_list are referencing the list [0, 1, 2]
>>>id(his_list)
>>>4388520064
Example 3
#Function takes 2 arguments, parameter a and parameter b adds them, then prints the object a with the same reference ID
>>> print(id(a))
>>> print(id(b))
>>> a.append(b)
>>> print(a)
>>> print(id(a))
>>>addto([0, 1, 2], 3)
>>>4452048768
>>>4384031024
>>>[0, 1, 2, 3]
>>>New reference ID
>>>4452048768
#Function takes 2 arguments, parameter a and parameter b adds them, then prints the new object nw with a new reference
ID
>>> print(id(a))
>>> print(id(b))
>>> print(nw)
>>> print(id(nw))
>>>old reference ID
>>>the reference id of a
>>>4452043392
>>>the reference id of b
>>>4452056768
>>>new object nw is a + b
>>>[0, 1, 2, 5, 4]
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 23/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
>>>4452124480
322 words
Tags:
Discussion Forum Unit 6
instead of
>>>mylist1 = mylist3
>>>mylist1 is mylist3 #mylist1 is equivalent to mylist3
>>>true
>>>mylist1 = mylist3
>>>mylist1 is mylist3 #mylist1 is equivalent to mylist3
43 words
You did a great job. well done in making you corrections. Your explanation was very clear and understandable.
18 words
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.
- Equivalent means that the two objects are the same object.
- Identical means that the two objects are the same object and have the same value.
The difference between objects and values is that objects are not the same object, but have the same value.:
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 24/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
This prints True because name1 and name2 are the same objects
Output:
name1 is name2: True
Equivalent:
This prints False because name3 and name4 are NOT the same objects
Output:
name3 is name4: False
The 'is' operator is used to check if two objects are the same object returning a boolean value.
Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.
Aliasing is when two variables point to the same object, but the variables are different.
The following line creates a new object and name6 points to it. And a reference is created.
name6 = name5
The line below changes the value of name5 first item to "Solorzano" also changes the value of name6 creating an alias
name5[0] = "Solorzano"
print("name5:", name5)
Output:
name5: ['Solorzano', 'Carlos', 'Duran']
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.
list_to_modify = [1, 2, 3, 4, 5]
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 25/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
print(list_to_modify) # Print the list without modifications
newList = list_to_modify # this will create a new list with the same elements as the list passed as an
argument, using the same memory address
# newList gets the same value as list_to_modify because they are pointing to the same object in memory
return newList # this will return the new list with the new values
Output:
[1, 2, 3, 4, 5]
References
Downey, Allen. (2015). Think Python: How to Think Like a Computer Scientist. Needham, MA: Green Tea Press
480 words
great post, the last example works perfectly, and the post is overall clean and easy to understand, but for the example
"name 5" and "name6" the last comment is untrue, for it to be true it has to go something like this
name5 = 0
name6 = name 5
name5 =1
print (name6)
>>> 0
name6 = name5
print (name6)
>>>>1
that's why in a function you should update both values once one has changed, or else a function wont work properly.
82 words
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 26/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
>>>James is Jack
False
>>>Jack is James
False
>>>#they are not the same person even if they are twins
>>>Jamie = James
>>>Jamie is James
True
>>>Jackie = Jack
>>>Jackie is Jack
True
t += [skill]
return t
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 27/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
>>>James
>>>Jack
References
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 (CC BY-NC 3.0).
Dear Janelle,
I really like your examples as are very easy to follow, something I would like to add to your very detailed explanation, is the
+= operand what does it do? perhaps you could have added a comment saying this line will add an item at the end of the
list, and maybe you could have added another line to modify the list using square brackets to compare different ways of
modifying a list.
Python variables and an object are almost an equivalent concepts. The identifier of a variables changes when giving the
variable a new value. That new values results in a new object being created.
The 'is' operator is used to check if two variables are equal or not
mylists=[10,20,30,50]
newlists=[30,50,40]
newlists=mylists
newlists is mylists
print(newlists)
Refereces have an aliance if they refer to the same object in memory during program execution.
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 28/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
Question 1
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.
Answer
Equivalent is a statement that shows that objects have the same value.
Identical is use to show that two objects are equivalent. Note that if two objects are equivalent, they are not necessarily
identical. Variables that have the same objects are not identical but they are equivalent because they refer to different values,
and variables that have the same values means they are identical because they refer to the same values.
The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity. While the
‘==’ operator checks for equivalence between two objects. Both operators return value type is bool, which means, they either
return a True or False value.
>>> z = x == y
>>> print(x)
>>> print(y)
>>> print(z)
True
>>> c = x is y
>>> print(c)
False
>>>
Variables x and y points to the same list [11, 22, 33]. In variable z, x is equivalent to y. The return gives True. In the third line of
code, “x” and “y” have the same values. That means they are equivalent, and that is why when we use the “==”, we get a True
output. In a real sense, being a string, they refer to two different objects that have the same value and that is why when we use
the “is” operator, we receive a False output, which means they are not identical.
Question 2
Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.
Answer
Aliasing is a circumstance where two or more variables refer to the same object. Variables store references to values.
True
>>> print(list1)
>>> print(list1)
>>> print(list2)
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 29/31
1/5/22, 1:49 AM CS 1101 - AY2022-T2: Discussion Unit 6
>>>
The output is “True” in forth line of code because list1 and list2 now refer to the same object. Therefore, list2 is an alias of list1
Question 3
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.
Answer
Create your own unique examples for this assignment. Do not copy them from the textbook or any other source.
... c.append(44)
...
...
>>> b = [22,33]
>>>
>>> add_value(b)
>>>
>>> print(b)
>>> When c.append(44) is called, a list of object is passed into the function as an argument. Parameter ‘b’ contains the
references to the object. c.append(44) calls the method append to the object.
Downey, A. (2015). Think Python, How to think like a computer scientist. This book is licensed under Creative Commons
Attribution – Noncommercial 3.0 Unported (CC BY-NC 3.0)
557 words
All activities close on Wednesdays at 11:55 PM, except for Learning Journals/Portfolios which close on Thursdays at 11:55 PM always
following the clock at the top of the page.
Due dates/times displayed in activities will vary with your chosen time zone, however you are still bound to the 11:55 PM GMT-5
deadline.
Jump to...
Resources
UoPeople Library
Orientation Videos
LRC
Syllabus Repository
Honors Lists
Links
About Us
Policies
University Catalog
Support
Student Portal
Faculty
Faculty Portal
CTEL Learning Community
Office 365
Tipalti
Contact us
English (en)
English (en)
العربية(ar)
https://my.uopeople.edu/mod/forum/discuss.php?d=640899 31/31