Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 11

Swap Variable Values

Easy

8 minute session

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.

Code editor
Loading editor…

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.

03:39 AM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.