Python offers several flexible ways to pass arguments. Choosing well makes functions both easy to call and hard to call incorrectly.
Positional (required) arguments
Arguments are matched to parameters by position. The number must match exactly: too few or too many raises TypeError. Wrong order gives a wrong result without any error.
Default arguments
A parameter can have a default value, used when the call omits it: def simple_interest(p, r=2, t=5):. Parameters with defaults must come after those without.
Keyword arguments
The caller names the parameter: compound_interest(r=13, p=100000, t=5). Order no longer matters, and calls become self-explanatory. In a call, positional arguments must come before keyword arguments.
Arbitrary arguments
*args collects any number of extra positional arguments into a tuple.
**kwargs collects any number of extra keyword arguments into a dictionary.
The names args and kwargs are conventions; the * and ** are what matter. The order in a definition is: normal parameters, *args, then **kwargs.
How arguments are passed
Python passes a reference to the object (sometimes called “call by object reference” or “call by sharing”). If the function changes a mutable object (for example appends to a list), the caller sees the change; if it rebinds the parameter (lst = [...]), the caller does not.
The mutable default trap
A default value is created once, when the function is defined, not on every call. A mutable default such as [] is therefore shared between calls.
Key takeaways
Positional arguments match by order and count.
Defaults make parameters optional; keywords match by name.
*args is a tuple of extra positional values; **kwargs a dictionary of extra named values.
Defaults are evaluated once; use None instead of a mutable default.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a function unique(numbers) that returns a list of the distinct items of a list in their original order (Check Your Progress 2, Ex 2), and give its output for [1, 2, 1, 1, 2, 2, 3, 4, 6, 3].
Show worked answer
def unique(numbers): out = [], then for n in numbers: if n not in out: out.append(n), then return out. For the given list it returns [1, 2, 3, 4, 6]. Each item is added only the first time it is seen, which keeps the original order. (list(dict.fromkeys(numbers)) gives the same result in one line; list(set(numbers)) removes duplicates but does not guarantee the order.)
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/functions-and-file-handling-in-python/function-arguments/ to read this page.