# Python Coding challenge - Day 76 | What is the output of the following Python code?

### Code :

def f1(a,b=[]):

b.append(a)

return b

print (f1(2,[3,4]))

### Solution and Explanation:

In the given code, you have a function f1 that takes two parameters a and b, with a default value of an empty list [] for b. The function appends the value of a to the list b and then returns the modified list.

When you call f1(2, [3, 4]), it means you are passing the value 2 for a and the list [3, 4] for b. The function then appends 2 to the provided list, and the modified list [3, 4, 2] is returned.

However, it's important to note that when you use a mutable default argument like a list (b=[]), it can lead to unexpected behavior. The default value is created only once when the function is defined, not each time the function is called. So, if you modify the default list (e.g., by appending elements to it), the changes persist across multiple function calls.

If you were to call the function again without providing a value for b, it would continue to use the modified list from the previous call. Here's an example:

print(f1(3))  # Output: [3]

The default value for b is now [3] because of the previous call.

To avoid this issue, it's generally recommended to use None as the default value and create a new list inside the function if needed. Here's an updated version of your function:

def f1(a, b=None):
if b is None:
b = []
b.append(a)
return b

print(f1(2, [3, 4]))
print(f1(3))
This way, you ensure that a new list is created for each call when a value for b is not provided.