Python Program to Swap Two Variables
Last Updated :
12 Jul, 2025
The task of swapping two variables in Python involves exchanging their values without losing any data . For example, if x = 10 and y = 50, after swapping, x becomes 50 and y becomes 10.
Using Tuple Unpacking
Tuple unpacking is the most efficient method as it eliminates the need for extra memory or temporary variables. It also enhances code readability and performance, making it the preferred choice in Python.
Python
x, y = 10, 50
x, y = y, x # swapping
print("x:", x)
print("y:", y)
Explanation: x, y = y, x
uses tuple unpacking to swap the values of x
and y
in a single step. It first creates a temporary tuple (y, x)
, then unpacks it back into x
and y
, eliminating the need for an extra variable .

Using Arithmetic Operations
By using basic arithmetic operations, we can swap two variables without a temporary variable. This method is efficient and works well for integers. However, in some languages, it may cause overflow with very large numbers.
Python
x, y = 10, 50
x = x + y
y = x - y
x = x - y
print("x:", x)
print("y:", y)
Explanation: First, x is updated to the sum of x and y. Then, y is reassigned to x - y, which effectively gives it the original value of x. Finally, x is updated to x - y, restoring y’s original value to x.
Using XOR Operator
XOR (Exclusive OR) operator can be used to swap two variables at the bit level without requiring additional memory. This method is commonly used in low-level programming but can be harder to read and understand compared to other approaches.
Python
x, y = 10, 50
x = x ^ y
y = x ^ y
x = x ^ y
print("x:", x)
print("y:", y)
Explanation: First, x = x ^ y stores the XOR result of x and y in x. Next, y = x ^ y applies XOR again, effectively retrieving the original value of x and storing it in y. Finally, x = x ^ y retrieves the original value of y and assigns it to x, completing the swap .
Using Temporary Variable
The traditional method of swapping two variables uses an additional temporary variable. While it is straightforward and easy to understand, it is not the most optimized approach as it requires extra memory allocation. This method is mainly used when clarity is more important than efficiency.
Python
x, y = 10, 50
temp = x
x = y
y = temp
print("x:", x)
print("y:", y)
Explanation: First, temp = x stores the value of x, then x = y assigns y’s value to x, and finally, y = temp restores x’s original value into y.
Similar Reads
Python Program to swap two numbers without using third variable The task of swapping two numbers without using a third variable in Python involves taking two input values and exchanging their values using various techniques without the help of a temporary variable. For example, if x = 5 and y = 7 then after swapping, x = 7 and y = 5.Using tuple unpackingTuple un
4 min read
Python Program to Swap Two Elements in a List In this article, we will explore various methods to swap two elements in a list in Python. The simplest way to do is by using multiple assignment.Example:Pythona = [10, 20, 30, 40, 50] # Swapping elements at index 0 and 4 # using multiple assignment a[0], a[4] = a[4], a[0] print(a)Output[50, 20, 30,
1 min read
Python program to Swap Keys and Values in Dictionary Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value so that they can be retrieved efficiently. Letâs discuss various ways of swapping the keys and values in Python Dictionary. Method#1 (Does not work when there are multiple same values
4 min read
Python Program to Split a List into Two Halves Splitting a list into two halves is a common operation that can be useful in many cases, such as when dealing with large datasets or when performing specific algorithms (e.g., merge sort). In this article, we will discuss some of the most common methods for splitting a list into two halves.Using Lis
4 min read
Python program to Swap i'th with j'th elements in List Given a list, the task is to write a Python program to the given list of elements, toggle every i and j elements in the list. Examples: Input : test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4], i, j = 4, 8 Output : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8] Explanation : 4 is swapped by 8 at each occurrence. In
4 min read
Variable Operations Dictionary Update - Python The task of performing variable operations in a dictionary in Python involves updating the values associated with specific keys based on certain operations. For example, consider a scenario where x = 6 and y = 7. The goal could be to store the sum of x and y under the key 'Gfg' and the product of x
3 min read