*args and **kwargs · Exercises
Core Python

*args and **kwargs Exercises

4 exercises that build on the Python *args and **kwargs lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: *args collects any extra positional arguments into a tuple and **kwargs collects any extra keyword arguments into a dictionary, so one function can accept a flexible number of inputs; the same stars in a call unpack a list or dictionary into arguments. By the end you can write functions that accept any number of arguments, order normal, default, *args, keyword-only and **kwargs parameters correctly, unpack lists and dictionaries in a call, and forward arguments to another function. You will practise: Average of any number of scores; Predict what gets collected; Fix the wrapper that drops keywords; Build a URL with a query string.

0 / 4 completed

*args and **kwargs practice exercises

1. Average of any number of scores

Write code Easy

Write a function average(*scores) that accepts any number of scores and returns their average. When it is called with no scores it should return 0.

Then call it as average(70, 80, 90) and print the result.

Your code
def average(*scores):
    # return the average, or 0 when there are no scores
    pass

# call average(70, 80, 90) and print the result

2. Predict what gets collected

Predict the output Medium

Read how show collects its arguments and type exactly what the program prints, then press Check. Pay attention to how a tuple with one item is printed.

Read this code
def show(first, *rest, **extras):
    print(first, rest, extras)

show(1)
show(1, 2, 3)
show(1, 2, key="x")
nums = [7, 8]
opts = {"mode": "fast"}
show(*nums, **opts)

3. Fix the wrapper that drops keywords

Fix the bug Medium

with_label should call format_price with whatever extra arguments it receives and put a label in front. It works for positional arguments but crashes when you pass currency="EUR". Fix with_label so every positional and keyword argument is forwarded. The expected output is Total: EUR 49.50.

Your code
def format_price(amount, currency="USD", decimals=2):
    return f"{currency} {amount:.{decimals}f}"

def with_label(label, *args):
    return label + ": " + format_price(*args)

print(with_label("Total", 49.5, currency="EUR"))

4. Build a URL with a query string

Write code Hard

Write build_url(base, *parts, **query).

  • Start with base (for example "https://shop.example.com").
  • Add each item of parts after it, separated by /.
  • If query is not empty, add ? and the pairs as key=value, joined with &, in the order they were given.
  • Skip any query value that is None.

For example, build_url("https://shop.example.com", "products", "shoes", page=2, sort="price") returns "https://shop.example.com/products/shoes?page=2&sort=price". Print that example.

Your code
def build_url(base, *parts, **query):
    # base + "/" + parts, then ?key=value pairs joined by &
    pass

print(build_url("https://shop.example.com", "products", "shoes", page=2, sort="price"))

Next lessonPython Lambda Functions

More Python *args and **kwargs examples · All Python exercises