What is the output of the following? def f(a, b=[]): b.append(a) return b print(f(1)) print(f(2))

Python Developer Medium

Python Developer — Medium

What is the output of the following? def f(a, b=[]): b.append(a) return b print(f(1)) print(f(2))

Key points

  • The function f appends the value of a to the list b.
  • The default argument b is shared between function calls.
  • The list b retains its value from the previous function call.

Ready to go further?

Related questions