Lesson guide
What this Python exercise practices
Swap Variable Values is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
Summary
Swap the values of two variables using Python's tuple unpacking without a temporary variable.
Problem statement
Write a function swap_values(a, b) that swaps the values of a and b using a single assignment (tuple unpacking) and returns the swapped pair as a tuple (a, b). Do not use a temporary variable.
Task
Practice swapping two variables in a single statement and returning the swapped values.
Examples
Swap two numbers
Input
swap_values(1, 2)
Output
(2, 1)
Explanation
Values 1 and 2 are swapped and returned.
Input format
A function call swap_values(a, b) with two values.
Output format
Return a tuple (a, b) containing the swapped values.
Constraints
Inputs can be any Python values. Use tuple unpacking (a, b = b, a) to swap.
Samples
Sample input 0
swap_values('x', 'y')
Sample output 0
('y', 'x')
Explanation 0
String values are swapped and returned.
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.