List mutability

  1. Which is the value of list a after 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]
    
  2. List a has 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]
    
  3. Which is the value of list b after 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
    
  4. 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.txt

  5. After executing these assignment statements:

    a = ['a', 5, [], 3.2, [4, 'bc']]
    b = ['a', 5, [], 3.2, [4, 'bc']]
    

    have a and b the same value? Are they the same object ? You can check it by using the operators == and is, respectively.

  6. After executing these assignment statements:

    a = ['a', 5, [], 3.2, [4, 'bc']]
    b = a
    

    have a and b the same value? Are they the same object ? You can check it by using the operators == and is, respectively.

  7. After executing these assignment statements:

    a = ['a', 5, [], 3.2, [4, 'bc']]
    b = a[:]
    

    have a and b the same value? Are they the same object ? You can check it by using the operators == and is, respectively.

  8. After executing these assignment statements:

    a = ['a', 5, [], 3.2, [4, 'bc']]
    b = a[-1]
    

    have a[-1] and b the same value? Are they the same object ? You can check it by using the operators == and is, respectively.

  9. After executing these assignment statements:

    a = ['a', 5, [], 3.2, [4, 'bc']]
    b = [4, 'bc']
    

    have a[-1] and b the same value? Are they the same object ? You can check it by using the operators == and is, respectively.

  10. 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 == and is, 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])
    
  11. which is the result of the following print expressions ?. You can check it by using the operators == and is, respectively.

    >>> b = [0, 1]
    >>> a = ['a', b, 'c']
    >>> print(a)
    >>> b[0] = 2
    >>> print(b)
    >>> print(a)