def split_full_name(full):
first, last = full.split(" ", 1)
return first, last
f, l = split_full_name("Alice Smith")
print(f) # Alice
print(l) # Smith
Behind the scenes, returning first, last is the same as returning the tuple (first, last). The parentheses are optional.
name, age, role = ("Alice", 32, "analyst")
def summary(numbers):
return {
"count": len(numbers),
"total": sum(numbers),
"mean": sum(numbers) / len(numbers),
"max": max(numbers),
"min": min(numbers),
}
stats = summary([10, 20, 30, 40, 50])
print(stats["mean"]) # 30.0
Better than returning a 5-tuple: at the call site you read stats["mean"], not stats[2].
return a, b.x, y = func().Write a function that takes a list of numbers and returns min, max, and average. Try a tuple return first; then rewrite as a dict.