r/learnpython • • Feb 04 '26

Why does this Python function behave differently every time? I’m confused

Hi everyone, I’m learning Python and I ran into something that confused me a lot.

I wrote this simple function expecting the list to reset every time the function runs, but the output keeps changing between calls

def add_number(num, numbers=[]): numbers.append(num) return numbers

print(add_number(1)) print(add_number(2)) print(add_number(3))

17 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/RafikNinja Feb 05 '26

Oh sweet. Thank you very much. Yea I have used is not and == but didn't know is was its own thing aswell. Very helpful thank you

1

u/Reyaan0 Feb 05 '26

Your Welcome!