Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Swap Variable Values

Swap the values of two variables using Python's tuple unpacking without a temporary variable.

Python practice8 minVariables & Data TypesBeginnerLast updated December 29, 2025

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)

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 1

Input

swap_values('x', 'y')

Output

('y', 'x')

String values are swapped and returned.