Arguments can have default values, which are used when the caller leaves them out:
def add_tax(price, rate=0.08):
return round(price * (1 + rate), 2)
add_tax(20) uses the 8% rate, and add_tax(20, 0.2) overrides it. Arguments with defaults go after the ones without.
Callers can name arguments: add_tax(20, rate=0.2). Named arguments can come in any order and make calls easier to read.
A function can return several values at once as a tuple, return low, high, which the caller unpacks: low, high = price_range(prices).
Watch out for one trap: a default value is created once, when Python reads the def line, not each time the function runs. A default list like tags=[] is shared by every call, so items pile up from one call to the next. Use None as the default and make the list inside the function.
def split_cost(total, people, tip=0.15):
return round(total * (1 + tip) / people, 2)
split_cost(240, 4), split_cost(240, people=4, tip=0.2)
Out: (69.0, 72.0). The first call uses the default tip; the second names its arguments.