Function Arguments
When calling a function, you can pass arguments by position (positional arguments) or by name (keyword arguments). def describe(name, age): ... can be call...
Positional and Keyword Arguments
When calling a function, you can pass arguments by position (positional arguments) or by name (keyword arguments). def describe(name, age): ... can be called as describe("Alice", 30) — positional, or describe(age=30, name="Alice") — keyword. Keyword arguments can appear in any order. You can mix them: describe("Alice", age=30), but positional arguments must always come before keyword arguments. Keyword arguments greatly improve readability when a function has many parameters.
Default Arguments
Default arguments are parameter values specified in the function definition that are used when the caller does not supply that argument. def power(base, exponent=2): return base ** exponent. Calling power(5) returns 25; power(5, 3) returns 125. Default arguments allow you to create flexible APIs where common cases require fewer arguments. Remember the mutable default trap: always use None as default for mutable types like lists and dicts, then initialise them inside the function.
*args — Variable Positional Arguments
Adding *args to a parameter list allows a function to accept any number of positional arguments. Inside the function, args is a tuple. def total(*args): return sum(args). Call: total(1, 2, 3, 4) returns 10. The name args is a convention; the asterisk (*) is what matters. You can place *args after required positional parameters: def log(message, *tags). The *args must come after all positional and default parameters in the signature.
**kwargs — Variable Keyword Arguments
Adding **kwargs allows a function to accept any number of keyword arguments. Inside the function, kwargs is a dictionary. def display(**kwargs): for k, v in kwargs.items(): print(f"{k}: {v}"). Call: display(name="Alice", score=95). This is useful for functions that forward configuration options or build data structures dynamically. The full parameter order in a signature is: positional, *args, keyword-only, **kwargs.
Keyword-Only Parameters
Parameters that appear after * or *args in a signature are keyword-only — callers must supply them by name. def create_user(name, *, role="user", active=True). The * signals that all following parameters are keyword-only even though they are not *args. This is useful for clarity in functions with many optional settings, preventing accidental positional misuse. Python 3 introduced keyword-only parameters; they do not exist in Python 2.
Exam Tip
Know the correct order of parameter types in a function signature: (positional, *args, keyword-only, **kwargs). Violating this order raises SyntaxError. Common exam question: def f(a, b=1, *args, **kwargs) — is this valid? Yes. What about def f(a, *args, b=1, **kwargs)? Also valid — b is a keyword-only parameter here. Another question: what type is args inside the function? A tuple. What type is kwargs? A dictionary. These distinctions appear in Python certification exams.