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.
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.
An Object is an instance of a class while value is an object that assigns to a variable
To know if two objects are identical or are not, we use the "is" operator.
The term equivalent(==) is used when we want to know
if two operands are equal or not if equal conditions returns true.
#OWN EXAMPLES IN PYTHON
my_first_list = [10,20,30,40,50]
my_second_list = my_first_list
my_third_list = list(my_first_list)
#print my_first_list
print(my_first_list)
#output: [10, 20, 30, 40, 50]
#print my_second_list
print(my_second_list)
#output: [10, 20, 30, 40, 50]
#print my_third_list
print(my_third_list)
#output: [10, 20, 30, 40, 50]
#Lets check the equality of the variables using (==)
print(my_first_list==my_second_list)
#output: True
print(my_first_list==my_third_list)
#output: True
#Lets check if the list are same or not using "is" operator
print(my_first_list is my_second_list)
#output: True
print(my_first_list is my_third_list)
#output: False
2.Describe the relationship between objects, references, and aliasing. Again, create your own examples with Python lists.
ANSWER.
Reference is a symbolic name representation of a python variable that reference to an object.
once an object is created and assigned to a variable, then refer that object by its name.
There is a relationship between objects and references,
object structure is a set of objects that are connected through references.
Aliases act as pointers in some case and aliases are used in the case of dictionaries and lists.
An alias can be defined as when two identifiers refer to the same variable .i.e. An object Y is aliased if two or more variables hold the same reference to Y.
#OWN EXAMPLES IN PYTHON
#OBJCETCS
my_object_1 = [10,20,30,40]
print(my_object_1)
#output: [10, 20, 30, 40]
#REFERENCES
my_object_2 = my_object_1
print(my_object_2)
#output: [10, 20, 30, 40]
#ALIASING
my_object_2 = my_object_1
print(my_object_2)
#output: [10, 20, 30, 40]
print(id(my_object_1))
#output: 2415366640640
print(id(my_object_2))
#output: 2415366640640
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.
The following function receives a list as an argument and append the value 50
def extend_list(my_list_parameter: list):
my_list_parameter.append(50)
my_testIlist = [10, 20, 30,40]
extend_list(my_testIlist)
print(my_testIlist)
#output: [10, 20, 30, 40, 50]
Description of the function
When extend_list is called, a list object is passed into the function as argument. Parameter my_list_parameter contains the reference to the object. extend_list calls the method append of the object.
Comments
Leave a comment