Python Program to Swap Two Variables

Updated on November 26, 2024
Swap two variables header image

Introduction

Swapping two variables involves exchanging the values held by the variables with each other. This task is a fundamental concept in programming and is frequently explored when learning a new language or working on data manipulation projects. In Python, swapping variables can be elegantly handled in various ways, leveraging Python's syntactic features to make the operation more intuitive and readable.

In this article, you learn how to swap two variables in Python using different methods. Each approach is demonstrated through examples, highlighting the flexibility and simplicity Python offers for such basic operations. The techniques range from using a temporary variable to Python's tuple unpacking feature.

Using a Temporary Variable

Swapping the contents of two variables traditionally involves three steps: storing one value temporarily, replacing the first value with the second, and finally assigning the stored value to the second variable. Here’s how you perform these steps in Python.

  1. Initialize two variables.

  2. Use a temporary variable to hold one of the values during the swap.

    python
    a = 5
    b = 10
    temp = a
    a = b
    b = temp
    print("a:", a, "b:", b)
    

    In this code, temp is used to temporarily store the value of a. Then a is assigned the value of b, and b gets the value stored in temp. The result is that a and b have their values swapped.

Python Tuple Unpacking

Python provides a very concise way to swap variables by using tuple unpacking. This method does not require a temporary variable and is typically more "Pythonic."

  1. Define the variables to swap.

  2. Swap the values using tuple unpacking.

    python
    a = 5
    b = 10
    a, b = b, a
    print("a:", a, "b:", b)
    

    Here, a, b = b, a leverages tuple unpacking, where b, a on the right-hand side forms a tuple (10, 5) that is immediately unpacked into a and b. This swaps their values without needing an extra storage variable.

Using XOR for Swapping

Swapping using XOR is a bit complex and less intuitive but can be useful in contexts where you might want to avoid using extra storage space, such as in embedded systems programming.

  1. Start by defining two variables.

  2. Apply XOR operations to swap the variables without using additional space.

    python
    a = 5
    b = 10
    a = a ^ b
    b = a ^ b
    a = a ^ b
    print("a:", a, "b:", b)
    

    XOR operation is a binary operation that works on the bit-level representations of integers. In the sequence of operations, a initially contains the result of a XOR b. Then b is updated to the original value of a by b = (a XOR b) XOR b. Finally, a receives the original b value via a = (a XOR b) XOR a. This method is slick but should be used with understanding of its bitwise nature.

Conclusion

The ability to swap two variables in Python can be approached in several ways, each having its context where it might be more applicable. Whether using a temporary variable, employing Python's tuple unpacking, or delving into the bitwise XOR operation, Python provides the tools to perform such basic operations efficiently and elegantly. Adapt these methods as needed for more complex data manipulations or to enhance the readability and performance of your code.