Last modified: Oct 28, 2026
Python Swap Two Variables
Swapping two variables is a common task in programming. In Python, it can be done easily and efficiently. This article explains multiple ways to perform this operation. We'll keep things simple and beginner-friendly.
Why Swap Variables?
Swapping variables is useful in many scenarios. For example, sorting algorithms often require swapping values. It helps in reorganizing data dynamically during runtime.
Method 1: Using Tuple Unpacking
This is the most Pythonic way to swap variables. Python allows assigning multiple variables in one line using tuples.
# Swap two variables using tuple unpacking
x = 5
y = 10
print("Before swapping:")
print("x =", x)
print("y =", y)
# Swapping happens here
x, y = y, x
print("After swapping:")
print("x =", x)
print("y =", y)
Before swapping:
x = 5
y = 10
After swapping:
x = 10
y = 5
This method uses tuple unpacking under the hood. It's clean, readable, and efficient. No extra memory is needed for a temporary variable.
Method 2: Using a Temporary Variable
In other programming languages, you typically use a temporary variable to hold one value during the swap. Python supports this approach too.
# Swap two variables using a temporary variable
a = 20
b = 30
print("Before swapping:")
print("a =", a)
print("b =", b)
# Store value of 'a' in temp
temp = a
# Assign value of 'b' to 'a'
a = b
# Assign original value of 'a' to 'b'
b = temp
print("After swapping:")
print("a =", a)
print("b =", b)
Before swapping:
a = 20
b = 30
After swapping:
a = 30
b = 20
This method is more verbose but works in almost every programming language. It's good for beginners transitioning from other languages.
Method 3: Using Arithmetic Operators
You can also swap variables using addition and subtraction. This avoids using a temporary variable.
# Swap two variables using arithmetic operations
num1 = 15
num2 = 25
print("Before swapping:")
print("num1 =", num1)
print("num2 =", num2)
# Perform arithmetic swap
num1 = num1 + num2
num2 = num1 - num2
num1 = num1 - num2
print("After swapping:")
print("num1 =", num1)
print("num2 =", num2)
Before swapping:
num1 = 15
num2 = 25
After swapping:
num1 = 25
num2 = 15
This method works well with integers. However, it may cause overflow in some languages. Python handles large numbers automatically, so it's safe here.
Important Notes
When swapping variables, always consider readability. The tuple unpacking method is recommended for most cases. It's concise and follows Python best practices.
If you're working with mutable objects like lists, swapping can behave differently. Always test your code to ensure correct behavior.
Common Mistakes
A common mistake is trying to swap without proper assignment. For example, writing x = y; y = x does not work as expected. Both variables end up with the same value.
Another mistake is using arithmetic methods with non-numeric types. These methods only work with numbers.
When to Use Each Method
Use tuple unpacking for general-purpose swaps. It's the standard Python way. Use temporary variables when clarity is more important than brevity. Arithmetic methods are useful in constrained environments but rarely needed in Python.
Understanding variable scope can help avoid confusion. For instance, if you're swapping inside a function, check whether variables are local or global. Learn more about this in our guide on Python Global and Local Variables.
Practical Applications
Swapping variables is essential in sorting algorithms. It's also used in graph algorithms and backtracking problems. Knowing how to swap efficiently improves code performance.
In loops, swapping can help iterate in reverse or alternate patterns. For example, you might swap loop counters conditionally. To understand loop behavior better, read our article on Python For Loop and While Loop Explained.
Sometimes, conditional logic determines whether to swap. Using Python If Else Statements: A Beginner Guide can help you implement such checks effectively.
Conclusion
Swapping two variables in Python is straightforward. The tuple unpacking method is the most elegant solution. Temporary and arithmetic methods offer alternatives depending on your needs. Choose the method that best fits your use case and coding style. With practice, swapping becomes second nature.