Question discussion

What is the output of the following Python code?def f(x, l=[]): l.append(x) return l print(f(1), f(2), f(3))

915Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

def f(x, l=[]):
    l.append(x)
    return l
print(f(1), f(2), f(3))
  • A[1] [2] [3]
  • B[1] [1, 2] [1, 2, 3]
  • C[3] [3] [3]
  • D[1, 2, 3] [1, 2, 3] [1, 2, 3]
Correct answer: D

Explanation

The default list is created once and shared; all three calls return that same list, which holds [1, 2, 3] when print displays it.

Discussion 0

Nobody has replied yet. Start the discussion!