Function Arguments · Exercises
Core Python

Function Arguments Exercises

4 exercises that build on the Python Function Arguments 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: A function argument is the value you pass in a call, and Python matches each argument to a parameter either by position or by name, following the order rules in the def line. By the end you can call a function with positional and keyword arguments, design parameters with defaults, keyword-only and positional-only rules, avoid the mutable default trap, and predict what a function can change in the objects you pass. You will practise: Return the lowest and highest; Predict: change or rebind?; Fix the shared cart; Price an order.

0 / 4 completed

Function Arguments practice exercises

1. Return the lowest and highest

Write code Easy

Write a function min_max(numbers) that returns both the smallest and the largest number in the list as one tuple (smallest, largest).

Then call it on temperatures and unpack the result into two variables called low and high, and print them.

Your code
temperatures = [18, 25, 11, 30, 22]

def min_max(numbers):
    # return the smallest and the largest number
    pass

# unpack the result into low and high, then print both

2. Predict: change or rebind?

Predict the output Medium

The function changes its parameter in two different ways. Type exactly what the program prints, then press Check.

Read this code
def update(team, count):
    team.append("Zed")
    count = count + 1
    team = ["Reset"]
    return team, count

players = ["Asha", "Ben"]
number = 2
new_team, new_number = update(players, number)
print(players, number)
print(new_team, new_number)

3. Fix the shared cart

Fix the bug Medium

add_to_cart(item, cart=...) should add item to the given cart, or to a new empty cart when none is given. At the moment every call without a cart reuses the same list, so the second line prints ['milk', 'bread'] instead of ['bread']. Fix the function.

Your code
def add_to_cart(item, cart=[]):
    cart.append(item)
    return cart

print(add_to_cart("milk"))
print(add_to_cart("bread"))

4. Price an order

Write code Hard

Write price_order(prices, /, *, tax_rate=0.0, coupon=None).

  • prices is a list of item prices and is positional-only.
  • tax_rate and coupon are keyword-only. coupon is None or a fraction such as 0.25 meaning 25 % off the subtotal.
  • Return a tuple (subtotal, total). The subtotal is the sum of prices. The total is the subtotal minus the coupon discount (if any), plus tax on the discounted amount, rounded to 2 decimals.
  • Do not change the prices list.

The starter already prints one order, so you can check your work.

Your code
def price_order(prices, /, *, tax_rate=0.0, coupon=None):
    # return (subtotal, total)
    pass

print(price_order([40, 60], coupon=0.25, tax_rate=0.2))

Next lessonPython *args and **kwargs

More Python Function Arguments examples · All Python exercises