Signup/Sign In

Swapping three Variables without using any Temporary Variable in Python

Posted in Tricks   LAST UPDATED: JUNE 27, 2023

    Suppose we have been given three variables, and we have been asked to swap them, with the condition that any temporary variable shouldn't be used, what would your approach be?

    We will show you a few approaches which can be used to achieve the same.

    Sample input: a = 3, b = 7, c = 0

    Sample output: a = 0, b=3, c = 7

    How to Swap three Variables without using any Temporary Variable in Python

    1. Using arithmetic operators

    The idea is to sum two numbers and store them in one variable. The other variable can be found by subtracting the value to be found from the sum.

    Let's see this with help of an example,

    a = 11
    b = 89
    c = 37
    print("Before swapping a = ",a,", b = ",b,", c = ",c) 
    a = a + b + c
    b = a - (b+c)
    c = a - (b+c)
    a = a - (b+c)
    print("After swapping a = ",a,", b = ",b,", c = ",c) 

    Output:

    Before swapping a = 11 , b = 89 , c = 37
    After swapping a = 37 , b = 11 , c = 89
    

    Note: An important point to note here is that when the variables hold large values, the above method causes the stack to overflow. The solution to this is to use a different method when encountering large values.


    2. Using bitwise XOR operator

    The only catch here is to store every integer value as a list data structure since every bit needs to be manipulated.

    a = [11]
    b = [89]
    c = [37]
    
    print("Before swapping a = ",a,", b = ",b,", c = ",c) 
    a[0] = a[0] ^ b[0] ^ c[0] 
    b[0] = a[0] ^ b[0] ^ c[0] 
    c[0] = a[0] ^ b[0] ^ c[0]  
    a[0] = a[0] ^ b[0] ^ c[0] 
    print("After swapping a = ",a,", b = ",b,", c = ",c) 

    Output:

    Before swapping a =  [11] , b = [89] , c =  [37]
    After swapping a =  [37] , b = [11] , c =  [89]
    

    Also, this technique works for integer values. If you add more than one integer value in the list only the first integer value will be swapped.


    Conclusion

    Swapping three variables without relying on temporary storage may seem like a puzzling task, but armed with the technique described in this article, you can accomplish it with grace and efficiency. By taking advantage of Python's elegant syntax and assignment operations, you've discovered a clever solution to this problem.

    In this post, we understood how three variables can be swapped without the usage of a temporary variable in Python. This is quite a frequent trick question in interviews. Do let us know in the comment section below about your method.

    Frequently Asked Questions(FAQs)

    1. Why would I need to swap three variables without using temporary storage?

    Swapping variables is a common task in programming, and the restriction of not using temporary storage is often imposed as a challenge or to optimize memory usage. It tests your problem-solving skills and ability to work with programming languages efficiently.

    2. How does the technique of swapping three variables without temporary storage work?

    In Python, you can use simultaneous assignment and arithmetic operations to swap variables without temporary storage. By carefully manipulating the values and leveraging the properties of addition and subtraction, you can achieve the desired swapping effect.

    3. Does this technique work for any number of variables?

    The technique described in this article specifically addresses swapping three variables. However, you can adapt the concept for swapping a different number of variables by extending the logic and applying the appropriate arithmetic operations.

    4. Are there any limitations or considerations to keep in mind when using this technique?

    While the technique is elegant, it may not be the most intuitive solution for variable swapping. It's crucial to ensure that your code remains readable and maintainable. If swapping variables without temporary storage compromises clarity, it's advisable to opt for a more conventional approach.

    5. Can this technique be used in languages other than Python?

    The specific syntax and assignment operations used in this technique may vary across programming languages. However, the underlying concept of simultaneous assignment and arithmetic operations can be adapted and applied in different languages to achieve similar results.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:python
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS