This guide explains return statement and returning multiple values through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.
What You Will Learn
return immediately ends a function call and optionally provides a value. Returning several comma-separated values actually returns one tuple, which callers can unpack into separate names.
When and Why to Use It
The following example focuses on a practical use of return Statement and Returning Multiple Values.
Step-by-Step Example
def divide_with_remainder(total, portions):
quotient, remainder = divmod(total, portions)
return quotient, remainder
whole_portions, leftover = divide_with_remainder(17, 5)
print(whole_portions, leftover)
Understanding the Example
divmod gives the quotient 3 and remainder 2. The function returns a two-item tuple, unpacked into whole_portions and leftover.
Expected Output
3 2
Important Behavior and Edge Cases
Statements after an executed return do not run; reaching the end without a return gives None.
Try It Yourself
Return a minimum and maximum value from a function and unpack the result.
Common Beginner Mistakes
- Statements after an executed return do not run; reaching the end without a return gives None.
- Confusing a returned value with printed output, or failing to verify a function’s behavior with different input values.
- Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.
Key Takeaways
- return immediately ends a function call and optionally provides a value.
- divmod gives the quotient 3 and remainder 2.
- Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.