Menu
Coddy logo textTech

Swapping Variables

Part of the Logic & Flow section of Coddy's Python journey — lesson 3 of 78.

Swapping variables is a common operation where the values of two variables are exchanged. Python offers a simple and elegant way to swap variables without needing a temporary variable, unlike many other programming languages.

Traditional Method (Using a Temporary Variable):

a = 10
b = 20
temp = a
a = b
b = temp
print(a)  # Output: 20
print(b)  # Output: 10

Pythonic Way (Simultaneous Assignment):

a = 10
b = 20
a, b = b, a
print(a)  # Output: 20
print(b)  # Output: 10

In the Pythonic way, the values of b and a are simultaneously assigned to a and b, respectively. This approach is more readable and doesn't require any additional variables.

challenge icon

Challenge

Easy

Write a Python program to swap the values of two variables without using a temporary variable. Initialize two variables, x and y, with the values 5 and 10 respectively. Swap their values and then print them.

Cheat sheet

Python allows swapping variables using simultaneous assignment without needing a temporary variable.

Traditional method using a temporary variable:

a = 10
b = 20
temp = a
a = b
b = temp

Pythonic way using simultaneous assignment:

a = 10
b = 20
a, b = b, a

The simultaneous assignment approach is more readable and doesn't require additional variables.

Try it yourself

# Initialize variables x and y



# Swap the values of x and y


# Print the swapped values
print(f"x: {x}")
print(f"y: {y}")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow