Problem No 9
Implement a function that returns multiple values
Easy≈ 10 minute session
Lesson guide
What this Python exercise practices
Implement a function that returns multiple values is a beginner practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Practice path
Summary
Return multiple values from a function using tuples so callers can unpack results.
Problem statement
Write sum_and_product(a, b) which takes two numeric arguments and returns two values: their sum and their product. The return should be a tuple so callers can unpack it like s, p = sum_and_product(2, 3).
Task
Implement sum_and_product(a, b) that returns both the sum and the product of two numbers as a tuple (sum, product).
Examples
Sum and product of 2 and 3
Input
sum_and_product(2, 3)
Output
(5, 6)
Explanation
2 + 3 = 5, 2 * 3 = 6, so return the tuple (5, 6).
Input format
Two numeric arguments a and b.
Output format
A tuple with two elements: (a + b, a * b).
Constraints
Do not use external libraries. Handle ints and floats. Return a tuple so Python's default repr matches expected outputs.
Samples
Sample input 0
sum_and_product(0, 5)
Sample output 0
(5, 0)
Explanation 0
0 + 5 = 5, 0 * 5 = 0.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.