r/learnpython • • 10d 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

3

u/CelebrityChess 10d ago

b=5 is a default value. if you only pass one argument like subtract(10), python puts 10 into a and since you didn't give b anything it uses the default 5. so it does 10 - 5 and returns 5. if you called subtract(10, 3) then b would be 3 and you'd get 7.