Practice: Positional Arguments
Q1. Subtract in order
Write a function subtract(a, b) returning a minus b. Print subtract(9, 4).
Goal: Pass positional arguments.
Q2. Power function
Write power(base, exponent) that returns base to the power exponent. Print power(2, 10).
Goal: Use two positional parameters.
Q3. Unpack a tuple
Write area(w, h) returning w * h. Store (6, 7) in a tuple called size and print area(*size).
Goal: Unpack a sequence into arguments.
Q4. Distance on a line
Write distance(x1, x2) returning the absolute difference. Print distance(3, 11) and distance(11, 3), each on its own line.
Goal: Design a function that is order independent.
Q5. Full name
Write full_name(first, last) returning last, a comma, a space and first. Print full_name("Ada", "Lovelace").
Goal: Match string arguments by position.
Q6. Spread a range
Write total3(a, b, c) returning the sum. Call it with *range(1, 4) and print the result.
Goal: Unpack any iterable.
Q7. Mixed unpacking
Write line(x, y, z) returning the string x-y-z using format with the three values. Call it as line(0, *[5, 9]) and print.
Goal: Mix normal and unpacked arguments.
Q8. Order matters
What does this print?
def ratio(a, b):
return a / b
print(ratio(1, 4))Goal: Predict a positional call.
Q9. Reverse the call
What does this print?
def ratio(a, b):
return a / b
print(ratio(4, 1))Goal: See the effect of swapping.
Q10. Names are ignored
What does this print?
def f(x, y):
return x - y
x = 10
y = 3
print(f(y, x))Goal: Know that variable names do not matter.
Q11. Star on a list
What does this print?
def f(a, b, c):
return a * b * c
items = [2, 3, 4]
print(f(*items))Goal: Predict unpacking.
Q12. Star on a string
What does this print?
def f(a, b, c):
return c + b + a
print(f(*"xyz"))Goal: Unpack a string.
Q13. Catch the error
What does this print?
def f(a, b):
return a + b
try:
f(1, 2, 3)
except TypeError:
print("too many")Goal: Recognise a TypeError.
Q14. Positional-only
What does this print?
def f(a, /, b):
return a * 10 + b
print(f(3, b=4))Goal: Read positional-only syntax.
Q15. Forgotten star
This program should print 15 but crashes with a TypeError. Fix it.
def total(a, b, c):
return a + b + c
nums = [4, 5, 6]
print(total(nums))Goal: Fix a missing unpacking star.
Q16. Wrong order
This should print 'Hello, Sam' but prints 'Sam, Hello'. Fix the call without changing the function.
def greet(greeting, name):
print(greeting + ", " + name)
greet("Sam", "Hello")Goal: Correct argument order.
Q17. Too many values
This crashes with a TypeError. Fix it so it prints 3.
def smaller(a, b):
return min(a, b)
print(smaller(3, 8, 5))Goal: Fix an extra argument.
Q18. Position or name?
Does Python use the variable names in a call to match parameters? Explain.
Goal: Explain positional matching.
Q19. Purpose of the star
Explain what the * in f(*items) does and when you would use it.
Goal: Explain argument unpacking.
Q20. Choosing a call style
A function takes eight settings. Should callers pass them positionally? Give a reason.
Goal: Choose argument style wisely.