List mutability¶
Which is the value of list
aafter each of these assignment statements ?a = [['a', 1], 3, [4, 5, 'b'], [1, [2, 'c'], 9]] a[0] = 5.7 a[-1] = 'hello' a[1] = ['X', 1, 2.5] a[0:2] = [0, 1] a[2:4] = [] a[0:1] = [2, 3, 4, 5] a[1:1] = ['x', 'y'] a[-2:] = ['a', 'b', 'c'] a[:-5] = [0, 1, 2] a[15:20] = [5, 6]
List
ahas been defined as follows:a = ['a', 'b', 'c', 'd', 'e', 'f']
write assignment statements to match the following expressions:
a == ['a', 'b', 1, 'd', 'e', 'f'] a == ['a', 'b', 'c', 'd', 2, 3] a == ['a', 'e', 'f'] a == ['a', 'b', 'c', 'd', 4, 5, 'e', 'f'] a == [6, 'c', 'd', 'e', 'f'] a == ['a', 'b', 'c', 7] a == ['a', 8, 9, 'f'] a == ['a', 'b', 'c', 'd', 'e', 'f', 10, 11, 12]
Which is the value of list
bafter the execution of each of the following assignment statements ?:b = ['garlic', 'onion', 'parsley'] del b[0] b[0] = 'thyme' b[0] = ['garlic', 'onion', 'rosemary'] b[0:1] =['garlic', 'onion'] b = b + b
We have written the two following Python functions
def list_test1(w): w = [1,1,1,1] w[1] = 5 def list_test2(w): w[1] = 5 w = [1,1,1,1]
What will the value of variables k1 and k2 be after executing the following instructions ?
>>> k1 = [0,0,0] >>> k2 = [0,0,0] >>> list_test1(k1) >>> list_test2(k2)
Give a reasoned answer. You can check the result using the following test file:
mutab.txtAfter executing these assignment statements:
a = ['a', 5, [], 3.2, [4, 'bc']] b = ['a', 5, [], 3.2, [4, 'bc']]
have
aandbthe same value? Are they the same object ? You can check it by using the operators==andis, respectively.After executing these assignment statements:
a = ['a', 5, [], 3.2, [4, 'bc']] b = a
have
aandbthe same value? Are they the same object ? You can check it by using the operators==andis, respectively.After executing these assignment statements:
a = ['a', 5, [], 3.2, [4, 'bc']] b = a[:]
have
aandbthe same value? Are they the same object ? You can check it by using the operators==andis, respectively.After executing these assignment statements:
a = ['a', 5, [], 3.2, [4, 'bc']] b = a[-1]
have
a[-1]andbthe same value? Are they the same object ? You can check it by using the operators==andis, respectively.After executing these assignment statements:
a = ['a', 5, [], 3.2, [4, 'bc']] b = [4, 'bc']
have
a[-1]andbthe same value? Are they the same object ? You can check it by using the operators==andis, respectively.Given the following list:
a = ['a', 5, [], 3.2, [4, 'bc']]
which is the result of the following print expressions ?. You can check it by using the operators
==andis, respectively.>>> b = a >>> print(a, b) >>> b[3] = 'x' >>> print(a, b) >>> a[0] = 10 >>> print(a, b) >>> b = a[-1] >>> b[0] = 'y' >>> print(a, b, a[-1])
which is the result of the following print expressions ?. You can check it by using the operators
==andis, respectively.>>> b = [0, 1] >>> a = ['a', b, 'c'] >>> print(a) >>> b[0] = 2 >>> print(b) >>> print(a)