Lesson guide
What this Python exercise practices
Swap Values If First Greater 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
Summary
Return the pair (a, b), swapping them if the first is greater than the second.
Problem statement
Write a function swap_if_greater(a, b) that returns a tuple (x, y) where x and y are the input values in non-decreasing order. If a is greater than b, return (b, a); otherwise return (a, b).
Task
Use conditional logic to reorder two values when needed and return them as a tuple.
Examples
Swap when first is greater
Input
5, 3
Output
(3, 5)
Explanation
Since 5 > 3, the returned tuple swaps the values.
Input format
Two numeric arguments a and b (ints or floats).
Output format
Return a tuple (x, y) where x <= y.
Constraints
Do not print; just return the tuple. Maintain exact numeric values and types.
Samples
Sample input 0
2, 3
Sample output 0
(2, 3)
Explanation 0
2 <= 3 so no swap.
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.