Concepts / Positional Arguments

Positional Arguments

When a function call uses positional arguments, each value is assigned according to where it appears.

  • Programming
Interactive lab

Try it: Binding Arguments to Parameters

How Python matches a call's arguments to a function's parameters — positionals left to right, extras into *args, keywords by name, defaults for the rest, unknown names into **kwargs — and the exact SyntaxError or TypeError when a call doesn't fit, including the shared mutable default.

How it works

  1. The parser checks the call first: a positional after a keyword, or a repeated keyword, is a SyntaxError before anything runs.
  2. At the call site, *list spreads into separate positional arguments and **dict into keyword arguments.
  3. Positional arguments fill the parameters left to right; leftovers go into the *args tuple.
  4. Each keyword matches a parameter by exact name (a second value is 'got multiple values'); unknown names go into **kwargs or raise 'unexpected keyword argument'.
  5. Then: too many positionals, or a required parameter still empty, is a TypeError; otherwise defaults fill the rest.
  6. Defaults are evaluated once, at def time: a list default is the same object in every call.

Default run (13 steps): def greet(…) runs once: it creates the function and evaluates its default values right now. Then the call greet('Ann', 'Hi', *[7, 8], sep='-', mood='happy') starts. … Done. Printed: {'name': 'Ann', 'greeting': 'Hi', 'sep': '-', 'args': (7, 8), 'kwargs': {'mood': 'happy'}}.

Simplified: Five hand-written signatures with no positional-only (/) parameters; argument values are small literals (ints, short strs, True/False/None, flat lists). Error messages are CPython 3.12's (3.13+ may add a 'Did you mean' hint).

Educational simulation

Loading the simulation…

Why Argument Position Matters

When a function call uses positional arguments, each value is assigned according to where it appears. The first value goes to the first parameter, the second value goes to the second parameter, and so on. This means you must remember the function's parameter order every time you call it. The more parameters a function has, the easier it becomes to count incorrectly or assign a value to the wrong parameter.

In the real world

Generated example: imagine a function described as schedule_event(title, day, location). A positional call supplies the title first, the day second, and the location third. Supplying those values in a different order does not tell the interpreter what you intended; it changes which parameter receives each value.

Left-to-Right Assignment

Positional argument matching is an order-based process. For a function with parameters first, second, and third, the first supplied value is matched with first, the second supplied value with second, and the third supplied value with third. The interpreter uses the argument's position rather than examining what the value means.

Parisfirst valuedestinationParisMondaysecond valuedayMonday10third valueguests10position 1position 2position 3
Which parameter receives each positional value when arguments are assigned strictly from left to right?
Worked example

Tracing Three Positional Values

A generated function has parameters destination, day, and guests. A call supplies Paris, Monday, and 10 in that order. Determine which value reaches each parameter.

  1. 1
    First value

    Paris appears first, so it is assigned to destination.

  2. 2
    Second value

    Monday appears second, so it is assigned to day.

  3. 3
    Third value

    10 appears third, so it is assigned to guests.

Result

destination receives Paris, day receives Monday, and guests receives 10.

Naming the Destination

A keyword argument assigns a value by naming the parameter that should receive it. The interpreter matches the supplied name directly to a parameter name, so the keyword arguments can appear in any order. This removes the need to remember the parameter order and makes the purpose of each value visible in the function call.

guests10destinationParisdestinationParisdayMondaydayMondayguests10name matchname matchname match
How can named arguments reach their matching parameters even when the arguments appear in a different order?
Key point

The keyword must exactly match a parameter name. If the name is misspelled or does not exist in the function definition, Python raises a TypeError. Python does not guess that a similar name was intended.

In the real world

Generated example: a call can identify guests by name before identifying destination. The order of those named arguments does not change the matching, because guests is matched to the parameter named guests and destination is matched to the parameter named destination.

Combining Both Styles

A single function call can contain both positional and keyword arguments. The required order is strict: every positional argument must come before every keyword argument. Once the call has started using keyword arguments, a later positional argument is not allowed. Python raises a syntax error because the call would no longer follow the required distinction between position-based and name-based matching.

Parispositional valuedestinationParisguests10dayMondaydayMondayguests10positionnamename
How do positional and keyword arguments map to the function's parameters when they are used together, and why must positional arguments come first?
Worked example

One Positional Value and Two Named Values

A generated function has parameters destination, day, and guests. A call supplies Paris positionally, then supplies guests and day by name.

  1. 1
    Match the positional value

    Paris is the first supplied value, so it is matched with the first parameter, destination.

  2. 2
    Match the named values

    The names guests and day directly identify their matching parameters, regardless of their order after the positional value.

  3. 3
    Check the call order

    The positional value appears before both keyword arguments, so the combination follows the required rule.

Result

The call correctly assigns Paris to destination, 10 to guests, and Monday to day.

Selective Default Overrides

Keyword arguments are especially useful when a function has parameters with default values. You can name only the parameter you want to change while allowing other parameters to keep their defaults. This avoids supplying every value solely to reach one parameter later in the parameter list.

destinationParisdestinationParisdays3days5guideFalseguideFalsekeeps valuekeyword overridekeeps default
Which parameters change when a keyword argument is provided, and which parameters continue using their default values?
In the real world

Generated example: suppose a function has destination, days with a default of 3, and guide with a default of False. Providing destination and the keyword days with a value of 5 changes the trip length while leaving guide at its default. The named argument identifies exactly which parameter should be overridden.

Best practice

Prefer keyword arguments when a value's purpose is not obvious from its position, when a function has many parameters, or when you are changing only selected parameters with defaults. The names make the call more self-documenting and can reduce position-related mistakes.

Mistakes to Avoid

  • Common mistake

    Relying on the wrong parameter order

    What it looks like

    Supplying a value intended for guests in the position used by day.

    Why it fails

    Positional matching uses location in the argument list, not the intended meaning of the value.

    Do this instead

    Check the function's parameter order or use a keyword argument to name the intended parameter.

  • Common mistake

    Putting a positional argument after a keyword argument

    What it looks like

    Starting a call with a named argument and then adding an unnamed value.

    Why it fails

    Python requires all positional arguments to appear before all keyword arguments.

    Do this instead

    Move the positional argument before the first keyword argument, or name that value as well.

  • Common mistake

    Misspelling a keyword parameter

    What it looks like

    Using destinatoin instead of destination.

    Why it fails

    Keyword names must exactly match parameter names. Python does not match similar names.

    Do this instead

    Compare the keyword with the function definition and correct the spelling.

  • Common mistake

    Supplying every defaulted parameter when changing only one

    What it looks like

    Providing values for all parameters just to override one parameter near the end.

    Why it fails

    This adds unnecessary positional counting and makes the call harder to read.

    Do this instead

    Use a keyword argument for the parameter that needs a non-default value and let the other defaults remain in use.

Practice the Mapping

MEDIUM

Generated practice: A function has parameters name, priority, and archived, where priority and archived have default values. Describe how you would call the function when you want to provide name positionally, set archived by keyword, and leave priority at its default. Then explain why the positional value must appear first.

Hints
  • The positional value must match the first parameter.
  • Use the exact parameter name for the keyword argument.
  • Do not provide a value for the parameter whose default should remain active.
  1. 1
    To reason about a function call, first separate position-based matching from name-based matching. Positional values are assigned from left to right. Keyword values go directly to parameters with matching names. In a mixed call, positional arguments come first. When defaults are available, keyword arguments let you override selected parameters while leaving the remaining defaults unchanged.

Key Takeaways

  • Positional arguments are matched from left to right according to parameter order.
  • Keyword arguments identify their target parameters by exact name, so their order does not matter.
  • When combining styles, place every positional argument before every keyword argument.
  • Keyword arguments are useful for overriding selected parameters while allowing other default values to remain active.
  • Using parameter names can make function calls clearer and less error-prone.

Related concepts

More concepts starting with P