Python Academy · Lesson

args and kwargs

Accept variable-length positional and keyword arguments.

Lesson 3 of 414 steps

args and kwargs is a free Python Academy lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

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.
Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

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. 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. The Python Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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