Practice: Positional Arguments

« Read the chapter

Q1. Subtract in order

BeginnerCoding

Write a function subtract(a, b) returning a minus b. Print subtract(9, 4).

Goal: Pass positional arguments.

Q2. Power function

BeginnerCoding

Write power(base, exponent) that returns base to the power exponent. Print power(2, 10).

Goal: Use two positional parameters.

Q3. Unpack a tuple

IntermediateCoding

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

IntermediateCoding

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

BeginnerCoding

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

IntermediateCoding

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

AdvancedCoding

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

BeginnerOutput

What does this print?

Code
def ratio(a, b):
    return a / b

print(ratio(1, 4))

Goal: Predict a positional call.

Q9. Reverse the call

BeginnerOutput

What does this print?

Code
def ratio(a, b):
    return a / b

print(ratio(4, 1))

Goal: See the effect of swapping.

Q10. Names are ignored

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
def f(a, b, c):
    return a * b * c

items = [2, 3, 4]
print(f(*items))

Goal: Predict unpacking.

Q12. Star on a string

AdvancedOutput

What does this print?

Code
def f(a, b, c):
    return c + b + a

print(f(*"xyz"))

Goal: Unpack a string.

Q13. Catch the error

IntermediateOutput

What does this print?

Code
def f(a, b):
    return a + b

try:
    f(1, 2, 3)
except TypeError:
    print("too many")

Goal: Recognise a TypeError.

Q14. Positional-only

AdvancedOutput

What does this print?

Code
def f(a, /, b):
    return a * 10 + b

print(f(3, b=4))

Goal: Read positional-only syntax.

Q15. Forgotten star

IntermediateDebugging

This program should print 15 but crashes with a TypeError. Fix it.

Code
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

BeginnerDebugging

This should print 'Hello, Sam' but prints 'Sam, Hello'. Fix the call without changing the function.

Code
def greet(greeting, name):
    print(greeting + ", " + name)

greet("Sam", "Hello")

Goal: Correct argument order.

Q17. Too many values

BeginnerDebugging

This crashes with a TypeError. Fix it so it prints 3.

Code
def smaller(a, b):
    return min(a, b)

print(smaller(3, 8, 5))

Goal: Fix an extra argument.

Q18. Position or name?

BeginnerConceptual

Does Python use the variable names in a call to match parameters? Explain.

Goal: Explain positional matching.

Q19. Purpose of the star

IntermediateConceptual

Explain what the * in f(*items) does and when you would use it.

Goal: Explain argument unpacking.

Q20. Choosing a call style

AdvancedApplication

A function takes eight settings. Should callers pass them positionally? Give a reason.

Goal: Choose argument style wisely.