0Pricing
Python Academy · Lesson

args and kwargs

Accept variable-length positional and keyword arguments.

args and kwargs is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

*args and **kwargs let functions accept variable numbers of positional and keyword arguments.

What is *args?

def f(*args) collects all extra positional arguments into a tuple named args.
def f(*args):
    print(args)
f(1, 2, 3)  # (1, 2, 3)

Iterating *args

for x in args: ... works because args is a tuple. You can also use sum(args), max(args), etc.
def total(*args):
    return sum(args)
print(total(1, 2, 3, 4, 5))

What is **kwargs?

def f(**kwargs) collects all extra keyword arguments into a dict named kwargs.
def f(**kwargs):
    print(kwargs)
f(name='Alice', age=30)

Using **kwargs

Access kwargs like any dict: for key, val in kwargs.items():
def display(**kwargs):
    for k, v in kwargs.items():
        print(f'{k}: {v}')
display(x=1, y=2)

Combining Regular, *args, **kwargs

def f(a, b, *args, **kwargs): normal params come first, then *args, then keyword-only, then **kwargs.
def f(a, *args, **kwargs):
    print(a, args, kwargs)
f(1, 2, 3, x=4)

Forwarding Arguments

def wrapper(*args, **kwargs): return real_func(*args, **kwargs) perfectly forwards all args to the wrapped function.
def real(a, b, c=0): return a+b+c
def wrapper(*args, **kwargs): return real(*args, **kwargs)
print(wrapper(1, 2, c=3))

Unpacking with * in Calls

*iterable unpacks any iterable as positional args. **mapping unpacks a dict as keyword args.
def add(a, b, c): return a+b+c
args = [1, 2, 3]
print(add(*args))

Extended Iterable Unpacking

first, *rest = [1,2,3,4] gives first=1, rest=[2,3,4]. *head, last = lst gives the last element.
first, *rest = [1, 2, 3, 4]
print(first, rest)
*head, last = [1, 2, 3]
print(head, last)

Type Hints for *args/**kwargs

def f(*args: int, **kwargs: str) annotates the type of each individual item (not the container).
def f(*args: int, **kwargs: str) -> None:
    print(args, kwargs)
f(1, 2, name='Alice')

Common Patterns

print() itself uses *args — print(*my_list) prints each element space-separated. Very Pythonic.
lst = [1, 2, 3]
print(*lst)  # 1 2 3

Quick Check

What type does *args produce inside the function?

Recap

*args is a tuple of extra positional args. **kwargs is a dict of extra keyword args. Use them for flexible APIs and forwarding arguments.

Keep Going

Great work! Move on to the next lesson to keep progressing.

Frequently asked questions

Is the “args and kwargs” lesson free?

Yes — the full text of “args and kwargs” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “args and kwargs”?

Accept variable-length positional and keyword arguments. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “args and kwargs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Calling Functions
  2. Default and Keyword Arguments
  3. args and kwargs
  4. Return Values and Scope
← Back to Python Academy