Vinay Garg Assistant Professor

Lesson 22: Positional, keyword, default and variable-length parameters

A function can be called by position or by name, can have optional arguments, and can accept any number of them. Each style exists to make the call site readable.

A dark code window showing a function with a default parameter being called by keyword

In short

  • Positional arguments are matched by order; keyword arguments are matched by name and can be in any order.
  • A default value makes a parameter optional: `def f(a, b=0)` can be called as `f(5)`.
  • `*args` collects extra positional arguments into a tuple; `**kwargs` collects extra keywords into a dictionary.
  • Never use a list or dictionary as a default value — it is created once and shared between calls.

2

ways to pass a value: position and keyword

Python docs

1

default list shared by every call, which is why to avoid it

Python docs

Four ways to pass a value

Python gives you more than one way to call a function, and each one exists to make the calling line clearer.

StyleExampleMeaning
Positionalstudent("Asha", "MCA")First argument to first parameter
Keywordstudent(name="Asha")Matched by name, order free
Defaultdef f(a, b=0)b may be left out
Variable lengthdef f(*args, **kwargs)Any number of arguments

The program

# params.py — positional, keyword and default arguments

def student(name, course="MCA", marks=0):
    return f"{name} | {course} | {marks}"

print(student("Asha"))                 # defaults used
print(student("Ravi", "MCA"))          # two positional
print(student("Neha", marks=91))       # keyword, course defaulted
print(student(marks=74, name="Amit"))  # keyword, any order

def total(*numbers):
    return sum(numbers)

print(total(1, 2, 3, 4))               # 10

def show(**info):
    for key, value in info.items():
        print(key, "=", value)

show(name="Asha", roll=21)

def add_item(item, bucket=[]):         # wrong: shared default
    bucket.append(item)
    return bucket

print(add_item("a"))
print(add_item("b"))                   # the list from the earlier call is still there

How it works, step by step

  1. student("Asha") fills name; course and marks take their defaults.
  2. student("Neha", marks=91) skips course by naming the parameter it wants. Combined calls like this are common in real code.
  3. *numbers collects every positional argument into a tuple, so sum() can add them all.
  4. **info collects keywords into a dictionary, which is then looped with items().
  5. The last function shows the trap: a default list is created once, when the function is defined, so both calls append to the same list.

What it prints

Asha | MCA | 0
Ravi | MCA | 0
Neha | MCA | 91
Amit | MCA | 74
10
name = Asha
roll = 21
['a']
['a', 'b']

The fix for the mutable default

def add_item(item, bucket=None):
    if bucket is None:
        bucket = []
    bucket.append(item)
    return bucket

print(add_item("a"))    # ['a']
print(add_item("b"))    # ['b'], a fresh list each time

None is safe because it is never modified. The list is then created fresh inside each call.

Ordering rules, in one line each

  • Parameters without defaults come before parameters with defaults.
  • Positional arguments come before keyword arguments in a call.
  • *args comes before **kwargs.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Write `student(name, course="MCA", marks=0)` and call it three ways: one argument, two, and one by keyword.
  2. Write a function `total(*numbers)` that returns the sum of any number of arguments.
  3. Write a function `describe(**info)` that prints every key and value it receives.
  4. Fix this bug: `def add(item, bucket=[])` — rewrite it so each call starts with an empty list.

Sources

Frequently asked questions

What is a keyword argument?

An argument passed by parameter name, such as student(name="Asha"). It can appear in any order and makes the call readable when there are several parameters.

What is a default parameter value?

A value written in the definition, used when the caller leaves that argument out. def greet(name="friend") can be called as greet() and still works.

What do *args and **kwargs do?

args collects extra positional arguments into a tuple; kwargs collects extra keyword arguments into a dictionary. They let a function accept any number of arguments.

Why is using [] as a default parameter a bug?

Because the default is created once, at definition time, and reused. Every call appends to the same list, so results leak between calls. Use None and create the list inside the function.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.