How to Swap Three Variables Without Using Temporary Variables in Python? [3 Methods]

Do you want to learn how to swap three variables without using temporary variables in Python?

Suppose we have given three variables in Python and have been asked to swap them with the condition that any temporary variable should not be used. What would your approach be?

Swapping involves exchanging the values held by the variables. When we swap three numbers, we can shift their values so that each number moves to the next or changes its position, with the last number going to the position of the first.

In this Python article, we will show a few approaches that can be used to swap three variables without using temporary variables in Python, as listed below.

  • Using comma assignment
  • Using arithmetic operator
  • Using XOR operator

I will also cover how to swap three numbers using a function in Python with an example.

Swapping of Three Variables Without Using a Temporary Variable in Python Using Comma Assignment

Here is the complete Python program to swap three variables without a temporary variable using a comma assignment in Python.

Product1_price = 107
Product2_price= 203
Product3_price = 530

print("Original values:")
print("Product1_price =", Product1_price)
print("Product2_price =", Product2_price)
print("Product3_price =", Product3_price)

Product3_price, Product2_price, Product1_price = Product1_price, Product2_price, Product3_price

print("\nSwapped values:")
print("Product1_price =", Product1_price)
print("Product2_price =", Product2_price)
print("Product3_price =", Product3_price)

Output: Here, we have assigned variables to other variables separated with commas to swap three numbers in Python.

Original values:
Product1_price = 107
Product2_price = 203
Product3_price = 530

Swapped values:
Product1_price = 530
Product2_price = 203
Product3_price = 107

The screenshot below shows the output of the Python Program to swap three variables without a temporary variable.

Python program to swap three variables without a temporary variable

How to Swap Three Variables Without Using Temporary Variables in Python with Arithmetic Operators

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

This Python program uses arithmetic operators to swap three variables without a temporary variable.

Sales_product1 = 2311
Sales_product2= 4189
Sales_product3 = 3337

print("Before swapping Sales_product1 = ",Sales_product1,", Sales_product2 = ",Sales_product2,", Sales_product3 = ",Sales_product3) 
Sales_product1= Sales_product1 + Sales_product2 + Sales_product3
Sales_product2 = Sales_product1- (Sales_product2+Sales_product3)
Sales_product3 = Sales_product1 - (Sales_product2+Sales_product3)
Sales_product1 = Sales_product1- (Sales_product2+Sales_product3)

print("After swapping Sales_product1 = ",Sales_product1,", Sales_product2 = ",Sales_product2,", Sales_product3 = ",Sales_product3) 
  • We have taken three Sales products and added the values assigned to variables Sales_product2 and Sales_product3. Then, assign the sum of the variables to Sales_product1.
  • Now, subtract the sum of values assigned to Sales_product2 and Sales product3 from the current value of Sales_product1, then assign the result to the variable Sales_product2 and similarly for Sales_product3.
  • Finally, the new value gets updated for Sales_product1, Sales_product2, and Sales_product3.

Output:

Before swapping Sales_product1 =  2311 , Sales_product2 =  4189 , Sales_product3 =  3337
After swapping Sales_product1 =  3337 , Sales_product2 =  2311 , Sales_product3 =  4189

Refer to the screenshot below after executing the Python Program to swap three variables without a temporary variable to see the output.

Swapping of three variables without a temporary variable using arithmetic operators

Another way to swap three variables without a temporary variable is using an arithmetic operator, i.e., multiplication and division operator.

This is the complete code of Python to swap three variables without a temporary variable using the multiplication and division operators.

Nike = 3
Old_Navy = 2
Gap = 1

print("Before swapping : Nike = ",Nike,", Old_Navy = ",Old_Navy,", Gap = ",Gap) 
Nike= Nike * Old_Navy * Gap
Old_Navy = Nike //Old_Navy // Gap
Gap = Nike // Old_Navy // Gap
Nike = Nike // Old_Navy // Gap

print("After swapping : Nike = ",Nike,", Old_Navy = ",Old_Navy,", Gap = ",Gap)

Output: This is a similar approach to the above example just instead of sum, we are doing multiplication, and instead of subtraction, we are doing division.

Before swapping : Nike =  3 , Old_Navy =  2 , Gap =  1
After swapping : Nike =  1 , Old_Navy =  3 , Gap =  2

Refer to the screenshot below to see the output of the Python code to swap three variables without a temporary variable using multiplication and division operators.

Swapping three variables without a temporary variable in Python using multiplication and division operators

Swap Three Variables Without a Temporary Variable Using the XOR Operator

In Python, XOR is a logical operator that gives the output as 1 when either of the operands is 0. XOR is a bitwise operator known as Exclusive OR in Python.

Let me make it clear with the table how XOR logically gets calculated.

ABA^B
000
011
101
110

The Python source code to swap three variables without a temporary variable using the XOR operator.

California = [1]
Texas = [2]
Florida = [3]

print("Before swapping:") 
print("California = ",California,", Texas = ",Texas,", Florida = ",Florida) 
California[0] = California[0] ^ Texas[0] ^ Florida[0] 
Texas[0] = California[0] ^ Texas[0] ^ Florida[0] 
Florida[0] = California[0] ^ Texas[0] ^ Florida[0]  
California[0] = California[0] ^ Texas[0] ^ Florida[0] 
print("After swapping:")
print("California = ",California,", Texas = ",Texas,", Florida = ",Florida)

In Python programs, to perform bitwise XOR operation [which means operations work at the level of individual bits] to swap values between variables. Then, it will update with the XOR of all variables, effectively swapping their values.

California[0] = California[0] ^ Texas[0] ^ Florida[0] 
Texas[0] = California[0] ^ Texas[0] ^ Florida[0] 
Florida[0] = California[0] ^ Texas[0] ^ Florida[0]  
California[0] = California[0] ^ Texas[0] ^ Florida[0]

Output:

Before swapping:
California = [1] , Texas = [2] , Florida = [3]
After swapping:
California = [3] , Texas = [1] , Florida = [2]

To see the output of the Python program to swap three variables without a temporary variable using the XOR operator, refer to the image below.

swap three variables without using temporary variables in Python using XOR operator

Swapping of Three Numbers Using a Function in Python

Here is the complete Python source code to swap three numbers using a function in Python.

def swapThree_num(num_one,num_two,num_three):
   
    num_one= num_one + num_two + num_three 
    num_two = num_one - (num_two+num_three) 
    num_three = num_one - (num_two+num_three)
    num_one = num_one - (num_two+num_three)
     
    print("After swapping num_one =",num_one,",num_two =",num_two,",  num_three =", num_three)

if __name__ == '__main__':
    num_one = 18
    num_two = 25
    num_three = 13
 
    print("Before swapping num_one =",num_one,", num_two =",num_two,",  num_three =", num_three)
 
    swapThree_num(18, 25, 13)

Output:

Before swapping num_one = 18 , num_two = 25 ,  num_three = 13
After swapping num_one = 13 ,num_two = 18 ,  num_three = 25

Refer to the image below for the Python code output to swap three numbers using a function.

Swapping of three numbers using a function in Python

Conclusion

We understood how to swap three variables without using temporary variables in Python with several approaches and examples, such as comma assignment, arithmetic operators: addition and subtraction or multiplication and division operators, and the XOR bitwise operator.

You may also like to read: