+ 3

Explain this Python code?

def func(i ,x=[ ]): x.append(i) return x print(func(1)) print(func(2)) print(func(3)) output : #[1] #[1,2] #[1,2,3] https://code.sololearn.com/cDdML9IN8I7S/?ref=app

7th Oct 2020, 6:00 AM
Ratnapal Shende
Ratnapal Shende - avatar
1 Answer
+ 1
x=[ ] means an array. It is left empty here. Then a function 'func(i,x[ ])' is defined, in which 'x.append(I)' means you are adding 'i' to that array 'x[ ]'. And finally return x which is 'x=[i]', where 'i' represents number of elements of that array. If you can explain it better, let us know. Thanks, that's really great!
7th Oct 2020, 6:09 AM
Not_in_Use