def f(x, pi=3.14159)a, p = rectangle(3, 4)Real functions often need several inputs and hand back several answers. A rectangle has a width and a height — and you usually want both its area and its perimeter.
def rectangle(width, height):
return width * height, 2 * (width + height)
area, perim = rectangle(3, 4)
print(area, perim) # 12 14
Separate parameters with commas in the def line. When you call the function, you supply one argument per parameter, in order.
def rectangle(width, height):
area = width * height
perimeter = 2 * (width + height)
return area, perimeter
rectangle(3, 4) # width=3, height=4
rectangle(3, 4) is not the same as rectangle(4, 3) for perimeter math elsewhere.
Give a parameter a default with = in the def line. The caller can leave it out — or pass their own to override it.
def circle(radius, pi=3.14159):
area = pi * radius * radius
circ = 2 * pi * radius
return area, circ
circle(2) # uses pi = 3.14159
circle(2, pi=3.14) # overrides → pi = 3.14
def circle(radius, pi=3.14159) is fine; the reverse is a syntax error.
Put commas after return and Python packs the values into a tuple for you — the same tuples you met on Day 3.
def rectangle(width, height):
return width * height, 2 * (width + height)
result = rectangle(3, 4)
print(result) # (12, 14) ← a tuple
print(type(result)) # <class 'tuple'>
Put the matching number of names on the left of = and Python hands each tuple slot to a name. Cleaner than digging out result[0].
# Unpack into two names:
a, p = rectangle(3, 4)
print(a) # 12
print(p) # 14
# Without unpacking — works, but ugly:
result = rectangle(3, 4)
print(result[0]) # 12
print(result[1]) # 14
Positional: matched by order. Keyword: matched by name with name=value — order-free and self-documenting.
circle(2) # positional: radius=2, pi=default
circle(2, 3.14) # positional: radius=2, pi=3.14
circle(2, pi=3.14) # keyword for pi — reads clearly
circle(radius=2, pi=3.14) # both keyword — order no longer matters
circle(pi=3.14, radius=2) # same call, swapped order ✓
Multiple parameters, a default, a tuple return, and unpacking — working together.
def circle(radius, pi=3.14159): # default value
area = pi * radius * radius
circ = 2 * pi * radius
return area, circ # return a tuple
a, c = circle(2) # unpack two names
print(f"area={a:.2f}, circ={c:.2f}") # area=12.57, circ=12.57
a, c = circle(2, pi=3.14) # keyword overrides default
print(f"area={a:.2f}, circ={c:.2f}") # area=12.56, circ=12.56
# A positional argument after a
# keyword argument is illegal:
circle(radius=2, 3.14)
# SyntaxError: positional argument
# follows keyword argument
# Fix — keyword for both, or
# positional for both:
circle(2, 3.14) # ✓
circle(radius=2, pi=3.14) # ✓
# Forgetting to unpack the tuple:
area = rectangle(3, 4)
print(area) # (12, 14) — a whole
# tuple, not just the area!
# Fix — give it both names:
area, perim = rectangle(3, 4)
print(area) # 12 ✓
Return a tuple (area, perimeter). Unpack it in the caller: a, p = rectangle(3, 4).
Return (area, circumference) using a default pi. Try circle(2) and circle(2, pi=3.14).
students/<your-username>/D12_geometry_helpers.py. Full brief in lab/module_1_foundations/D12_geometry_helpers/problem.md. PR before 12:00.
Scope: local vs global, and why globals are usually a bad idea.