return — Intermediate Playground
Exits a function and optionally sends a value back to the caller
Python Playground
def min_max(numbers):
return min(numbers), max(numbers)
lo, hi = min_max([3, 1, 4, 1, 5, 9])
print(f"Min: {lo}, Max: {hi}")
def stats(data):
n = len(data)
mean = sum(data) / n
variance = sum((x - mean) ** 2 for x in data) / n
return {"mean": mean, "variance": variance, "count": n}
result = stats([10, 20, 30, 40, 50])
print(result)
Output
Click "Run" to execute your code
Python can return multiple values as a tuple (with implicit packing). You can also return dicts or named tuples for clarity.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?