Assessment:
Given
a = [1,2]<br />
b = [1,2]<br />
Which of the following expressions evaluates to False?
1. a == b
a[0] is 1
a is [1,2]
b == [1,2]
Exercise:
Read the following interpreter session and fill in the missing sections of code in the remove_exact
function.
>>> class named_list(list): def __init__(self,name,*args): super(named_list,self).__init__(*args) self.name = name >>> fred = named_list('fred') >>> fred.append(2) >>> fred [2] >>> nameless = [2] >>> nameless == fred True >>> list_of_lists =[fred,nameless] >>> list_of_lists.remove(nameless) >>> list_of_lists[0].name Traceback (most recent call last): File "<pyshell#84>", line 1, in list_of_lists[0].name AttributeError: 'list' object has no attribute 'name' >>>def remove_exact(iterable, item): for index,element in enumerate(iterable): if item __ element: _______.pop(_____) break >>> list_of_lists =[fred,nameless] >>> remove_exact(list_of_lists,nameless) >>> list_of_lists[0].name 'fred'
Evaluation:
Which of the following code snippets always behaves the same as the expression a is not b
?
-
a != b
-
[o for o in vars(a)] != [o for o in vars(b)]
-
not a == b
-
id(a) != id(b)
Bonus Question! What does this print in the standard intepreter? (Feel free to check this out yourself.)
>>> for x in range(300): if x is 290: print x if x is 5: print x