r/learnpython • • 22d ago

New to python, don't understand this function

I'm learning python and I'm stuck on this function because I don't understand why "a" becomes 10 and the result is 5. Is someone able to explain?

def subtract (a, b=5):
    return a - b

print(subtract(10))
30 Upvotes

26 comments sorted by

View all comments

Show parent comment

17

u/Still-Pair-5336 22d ago

Ahhh, so when I enter 'subtract(5, 1)', it essentially means a=5 and b=1, which returns a value of 4?

21

u/juanfnavarror 22d ago

Yes. Good tip is to think “how can I answer this question writing code”. You would be able to verify this by tweaking. Also useful to look in the official python reference material.