Basic Python Commands and Functions: difficulty 3

Mark Yourself Question

  1. Grab a piece of paper and try this problem yourself.
  2. When you're done, check the "I have answered this question" box below.
  3. View the solution and report whether you got it right or wrong.

Consider the following program.

a = [1, 2, 3]
b = a
b.append(4)

c = a[:]
c.append(5)

def bump(lst):
    lst = lst + [9]
    return lst

d = bump(a)

Answer all four parts.

  1. State the value of a immediately after the call to b.append(4), and explain why.
  2. State the values of a and c after the call to c.append(5), and explain the difference from part 1.
  3. State the values of a and d after the final line, and explain why.
  4. State what would be different if the body of bump were replaced by lst.append(9) followed by return lst.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Basic Python Commands and Functions Questions: