%config InteractiveShell.ast_node_interactivity="none"
def f(globals, locals):
import base64
code="ZGVmIG1ha2VfcHJpbnRfbG9jYWxzKCk6IAogICMgSW4gYSBmdW5jdGlvbiB0byBwcmV2ZW50IGxvY2FscyBhbmQgaW1wb3J0cyBmcm9tIGxlYWtpbmcuCiAgICBnbG9iYWwgbWFrZV9wcmludF9sb2NhbHMKICAgIGRlbCBtYWtlX3ByaW50X2xvY2FscyAgIyBPbmx5IHJ1biB0aGlzIGZ1bmN0aW9uIG9uY2UuCgogICAgaW1wb3J0IElQeXRob24KICAgIGltcG9ydCBhc3QKICAgIGltcG9ydCBpbnNwZWN0CgogICAgY2xhc3MgVmlzaXRvcihhc3QuTm9kZVZpc2l0b3IpOgogICAgICAgIGRlZiBfX2luaXRfXyhzZWxmKToKICAgICAgICAgICAgc2VsZi52YXJpYWJsZXMgPSBzZXQoKQogICAgICAgIGRlZiB2aXNpdF9OYW1lKHNlbGYsIG5hbWUpOgogICAgICAgICAgICBzZWxmLnZhcmlhYmxlcy5hZGQobmFtZS5pZCkKICAgICAgICAjIFRPRE86IFBvc3NpYmx5IGRldGVjdCB3aGV0aGVyIHZhcmlhYmxlcyBhcmUgYXNzaWduZWQgdG8uCgogICAgQUxMT1dfVFlQRVMgPSBbaW50LCBmbG9hdCwgc3RyLCBib29sLCBsaXN0LCBkaWN0LCB0dXBsZSwgcmFuZ2VdCiAgICBkZWYgZmlsdGVyX3ZhcmlhYmxlcyh2YXJpYWJsZXMsIGxvY2Fscyk6CiAgICAgICAgZm9yIHYgaW4gdmFyaWFibGVzOgogICAgICAgICAgICBpZiB2IGluIGxvY2FscyBhbmQgdHlwZShsb2NhbHNbdl0pIGluIEFMTE9XX1RZUEVTOgogICAgICAgICAgICAgICAgeWllbGQgdgogIAogICAgIyBVbmZvcnR1bmF0ZWx5LCB0aGVyZSBkb2Vzbid0IHNlZW0gdG8gYmUgYSBzdXBwb3J0ZWQgd2F5IG9mIGdldHRpbmcKICAgICMgdGhlIGN1cnJlb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mlhYmxlcyBpbiB0aGUgY2FsbGVyJ3MgZnJhbWUuIiIiCiAgICAgICAgaW1wb3J0IGluc3BlY3QKICAgICAgICAjIGN1cnJlbnRmcmFtZSgpIGZyYW1lIGlzIHByaW50X2xvY2Fscy4gV2Ugd2FudCB0aGUgY2FsbGVyJ3MgZnJhbWUKICAgICAgICBmcmFtZSA9IGluc3BlY3QuY3VycmVudGZyYW1lKCkuZl9iYWNrCiAgICAgICAgbG9jYWxzID0gZmluZF9sb2NhbHMoZnJhbWUpCiAgICAgICAgd2hpY2ggPSBzZXQod2hpY2gpIGlmIHdoaWNoIGVsc2UgZmluZF93aGljaChmcmFtZSkKICAgICAgICBsbCA9IHtrOiB2IGZvciBrLCB2IGluIGxvY2Fscy5pdGVtcygpIGlmIGsgaW4gd2hpY2h9CiAgICAgICAgaWYgdHlwZXM6CiAgICAgICAgICAgIHByaW50KCJcbiIuam9pbihmIntrfToge3R5cGUodikuX19uYW1lX199IOKGkCB7dn0iIGZvciBrLCB2IGluIGxsLml0ZW1zKCkpKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCI7ICIuam9pbihmIntrfSDihpAge3Z9IiBmb3IgaywgdiBpbiBsbC5pdGVtcygpKSkKCiAgICByZXR1cm4gcHJpbnRfbG9jYWxzCgpwcmludF9sb2NhbHMgPSBtYWtlX3ByaW50X2xvY2Fscygp"
exec(base64.b64decode(code), globals, locals)
f(globals(), locals())
del f
Consider the following code. What do you think will be printed?
L = [1, 2, 3]
L[2] = 4
# Change False to True to see the output.
if False:
print(L)
The above line
L[2] = 4
changes the element at index 2
of L
to be 4
. This is called "item assignment."
Use list item assignment to change lst
to contain [6, 5, 5, 7, 6]
.
lst = [6, 3, 5, 6, 6]
original = id(lst)
# YOUR CODE HERE
print(lst)
print("Test (want True):", id(lst) == original and lst == [6, 5, 5, 7, 6])
Again, consider the following code. What do you think will be printed?
A = [5, 10, 15, 4]
B = A
B[1] = 11
# Change False to True to see the output.
if False:
print(A)
print(B)
We "see" the change in A
, even though it was B
that was assigned to. Why?
Write your answer in a comment below.
is
¶Lists are mutable. This means that when a list item is changed within a list, all variables that contain the "same" list also see the changes.
The is
operator tells you whether its two arguments are the same thing.
What do you think the following would print?
C = [12, 3, 4]
D = C
E = [12, 3, 4]
# Change False to True to see the output.
if False:
print_locals()
print()
print("C is D:", C is D)
print("C is E:", C is E)
print("D is E:", D is E)
print()
print("C == D:", C == D)
print("C == E:", C == E)
print("D == E:", D == E)
The above code shows that C
and D
both contain the same list, while E
contains a different list. But notice that they all are equivalent (==
), since they all have the same items.
Predict the following output.
A = [1, 2, 3]
B = A
A = [4, 5, 6]
# Change False to True to see the output.
if False:
print(B)
What can you say about reassigning variables that contain lists? Is the behavior the same or different from other variables?
Write your answer in a comment below.
is
and other types¶How do you think is
works for other types? What do you think this code outputs?
for a in [1, 1.0, False, "hello, world"]:
b = a
# Change False to True to see the output.
if True:
print(f"{repr(a)}: {a is b}")
Just like for lists, a
and b
contain the same values even for other types. But since these types are immutable (cannot be changed), we can't use changes to the value to observe that they are indeed the same. However, we can test that they are the same using is
.
Using item assignment, mutate the list of lists in O
below so that it contains [[1, 1, 2], [3, 5, 8]]
.
F = [1, 2, 3]
S = [3, 4, 5]
O = [F, S]
originalF = id(F)
originalS = id(S)
originalO = id(O)
def change_O(O)
"""Mutates O so that it contains [[1, 1, 2], [3, 5, 8]]."""
# YOUR CODE HERE
change_O(O)
print()
print("Tests (want all True):")
print(O == [[1, 1, 2], [3, 5, 8]])
print(id(O) == originalO)
print(id(O[0]) == originalF)
print(id(O[1]) == originalS)