Master Mathematics.

Programming · Hard · Python, C & Pseudocode · Problem 1

A mutable default argument

What does this Python program print?

def append_to_lst(val, lst=[]):
    lst.append(val)
    return lst
print(append_to_lst(1))
print(append_to_lst(2))
print(append_to_lst(3, []))
  1. (A) [1] / [2] / [3]
  2. (B) [1] / [1, 2] / [3]
  3. (C) [1] / [2] / [1, 2, 3]
  4. (D) [1] / [1, 2] / [1, 3]

Source: GATE DA