How to swap two variables in Python

Key takeaways:

  • Python offers four primary ways to swap two variables:

    • Using a temporary variable

    • Using tuple unpacking

    • Using arithmetic operations (without a temporary variable)

    • Using the XOR operation

  • Using a temporary variable is a straightforward method that involves using an extra variable to temporarily hold one value during the swap.

  • Using tuple assignments or comma operators allows us to swap values cleanly without a temporary variable.

  • We can swap values using addition and subtraction or multiplication and division, eliminating the need for a third variable.

  • Using XOR operation to achieve a swap without additional storage.

Learning to swap variables is an essential building block, whether modifying numbers in a game score, rearranging things in a list, or just practicing your coding skills.

Four ways to swap two variables in Python

Swapping the values of two variables is a common operation in programming. There are multiple ways to swap the values between two variables. We’ll explore four methods to achieve Python swapping effectively, including:

  • Using a temporary variable

  • Using tuple unpacking

  • Using arithmetic operations (Without temporary variable)

  • Using XOR operation

“Controlling complexity is the essence of computer programming.” — Brian Kernighan

For example, swapping values is a small yet crucial step in sorting, making organizing data easier.

Method 1: Using a temporary variable

The most straightforward method of swapping two variables is using a temporary variable. This method involves creating an extra variable temporarily holding one of the values.

a = 10
b = 20
temp = a
a = b
b = temp
print("a =", a)
print("b =", b)

In the code above, we first assign the value of a to the temporary variable temp. Then, we assign the value of b to a. Finally, we assign the value of temp (which holds the original value of a) to b. As a result, the values of a and b are swapped. This is a classic way to use a swap function in Python.

Method 2: Using tuple unpacking or comma operator

Python allows multiple assignments using tuples, enabling us to swap variables without a temporary variable. This method is often referred to as tuple unpacking or comma operator.

a = 10
b = 20
a, b = b, a
print("a =", a)
print("b =", b)

In the code snippet above, we assign the values of b and a to a tuple (b, a). Then, using tuple unpacking, we assign the tuple values back to a and b, respectively. This way, the values of a and b are effectively swapped.

Method 3: Using arithmetic operations

The arithmetic operations erase the need to use the third temporary variable to swap the variables in Python. The following are two combinations of arithmetic operations to solve this problem:

  • Addition and subtraction operation

  • Multiplication and division operation

Using both Addition and subtraction operators

Another method to swap variables in Python is by using arithmetic operations. This method works by utilizing addition and subtraction operations to swap the values, as illustrated below:

1 of 6

Here’s the code:

a = 10
b = 20
a = a + b
b = a - b
a = a - b
print("a =", a)
print("b =", b)

In the code snippet above, we update the value of a by adding b. Then, we update the value of b by subtracting the original value of b from the updated value of a. Finally, we update the value of a by subtracting the original value of b from the updated value of a. As a result, the values of a and b are swapped.

Using both multiplication and division operators

This method swaps the values by utilizing multiplication and division operations. Here’s the code for this.

a = 10
b = 20
a = a * b
b = a / b
a = a / b
print("a =", a)
print("b =", b)

In the code snippet above, the first operation multiplies a by b, updating a to 200. Next, the code updates b to be the result of dividing the new value of a by the original value of b, which gives b a new value of 10. Finally, the code updates a again by dividing the current value of a by the new value of b, resulting in a being updated to 20. This demonstrates a creative way to perform Python swapping using math.

Method 4: Using the XOR operation

Swapping variables can also be achieved using the XOR (^) operation. XOR is a bitwise operator that returns 0 for the same input and 1 when the inputs differ. One important property of XOR is that XORing a value with another value twice will cancel out the effect of the XOR operation. We can use XOR to swap variables using this property: a ^ b ^ b = a.

Following is an illustration of using the XOR operation to swap variables.

1 of 6

Below is the implementation of this method in Python:

a = 10
b = 20
a = a ^ b
b = a ^ b
a = a ^ b
print("a =", a)
print("b =", b)

In the code snippet above, we perform XOR operations between a and b to swap their values. a ^ b is stored in a. Now a contains original a^b, so a^b again will hold original a^b^b, which equals a. Similarly, a^b again will be a^ba=a^b ^ab=a^b^b=a, equal to b.

Conclusion

Swapping the values of two variables is a common task in programming, and Python provides multiple ways to accomplish it. Whether you use a temporary variable, tuple unpacking, arithmetic operations, or the XOR operation, each method effectively demonstrates the concept of swap Python.

Quiz!

1

What arithmetic operations can be used to swap two variables?

A)

Addition and multiplication

B)

Subtraction and division

C)

Addition and subtraction

D)

None of them

Question 1 of 30 attempted

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do we swap two variables in one line?

We can swap two variables in one line using the comma operator:

a, b = b, a

How do we swap two variables without using the third variable?

We can swap two variables without using the third variable with the following methods:

  • Using tuple unpacking
  • Using arithmetic operations (without temporary variable)
  • Using the XOR operation

Can the swap techniques be used with different data types in Python?

Yes, we can swap variables of different types, but it’s essential to ensure they are compatible.


Can we swap more than two variables in Python?

Yes, we can swap more than two variables in Python, as follows:

a, b, c = c, a, b

What does the swapcase() method do in Python?

The swapcase() method creates a string where every uppercase letter is changed to lowercase, and vice versa.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved