Swapping Four Variables Without a Temporary Variable in Python [3 Ways]

Are you struggling with swapping four variables without a temporary variable in Python? Let me tell you different ways to swap four variables in Python with some illustrative examples.

Swapping of numbers in Python means exchanging the values of the given variables.

In this Python article, I will show you how to swap four variables without a temporary variable in Python. We will cover three ways: comma assignment, arithmetic operation, and XOR operation.

How to swap four variables without a temporary variable in Python using comma assignment

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

city1 = 'San Francisco'
city2 = 'New York'
city3 = 'Dallas'
city4 = 'San Deigo'

city1, city2, city3, city4 = city4, city3, city2, city1

print('Print the city after swapping: ',city1,city2,city3,city4)

Output:

Print the city after swapping: San Deigo Dallas New York San Francisco

The below screenshot shows the output of the Python program after execution.

Python program to swap four variable without a temp variable using comma assignment

Swap Four Variables Without a Temporary Variable Using an Arithmetic Operation

Python Arithmetic Operators perform mathematical operations like addition, subtraction, multiplication, and division.

Here, I will use addition and subtraction operators in Python.

This is the complete code of Python to swap four variables without a temporary variable using addition and subtraction operators.

Chevrolet = 10
Ford= 20
Honda = 30
Toyota= 40

Chevrolet = Chevrolet + Ford + Honda + Toyota
Ford = Chevrolet - (Ford + Honda +Toyota)
Honda = Chevrolet - (Ford + Honda +Toyota)
Toyota = Chevrolet - (Ford + Honda + Toyota)
Chevrolet = Chevrolet - (Ford + Honda + Toyota)

print("Chevrolet =", Chevrolet)
print("Ford =", Ford)
print("Honda =", Honda)
print("Toyota =", Toyota)
  • We have taken four car brands: Chevrolet, Ford, Honda, and Toyota. Then add up the values assigned to variables Ford, Honda, and Toyota and assign the sum of the variables to Chevrolet.
  • Now, subtract the sum of values assigned to Ford, Honda, and Toyota from the current value of Chevrolet and then assign the result to the variable Ford in Python.
  • Similarly, we will retrieve the value for Honda, Toyota, and Chevrolet using the same calculation.
  • Finally, the new value gets updated for Chevrolet, Ford, Honda, and Toyota.
Chevrolet =Chevrolet + Ford + Honda + Toyota
Ford = Chervolet - (Ford + Honda +Toyota)
Honda = Chevrolet - (Ford + Honda +Toyota)
Toyota = Chevrolet - (Ford + Honda + Toyota)
Chevrolet = Chevrolet - (Ford + Honda + Toyota)

Output:

Chevrolet = 40
Ford = 10
Honda = 20
Toyota = 30

You can refer to the screenshot below, which provides the Python program’s output after execution.

Swap four variables without a temporary variable in Python

Using the multiplication and division operator in Python, you can swap four variables without a temporary variable. Follow the similar steps, written in the above example.

NewYork = 1
Austin = 2
Chicago = 3
Dallas = 4
 
NewYork = NewYork* Austin * Chicago * Dallas
Austin = NewYork// Austin// Chicago //Dallas
Chicago = NewYork //Austin //Chicago //Dallas
Dallas = NewYork //Austin // Chicago // Dallas
NewYork = NewYork //Austin // Chicago //Dallas
 
print("NewYork =", NewYork)
print("Austin =", Austin)
print("Chicago =", Chicago)
print("Dallas =", Dallas)

Output:

NewYork = 4
Austin = 1
Chicago = 2
Dallas = 3

Refer to the image below to see the output of the Python program to swap four variables without a temporary variable.

How to swap four variables without a temporary variable in Python

Swapping four variables without a temporary variable in Python using the XOR operator

XOR is a bitwise operator known as Exclusive OR in Python. It is a logical operator that gives the output as 1 when either of the operands is 0. For Example:

ABA^B
000
011
101
110

Here is the Python source code to swap four variables without a temporary variable using the XOR operator.

Harry = $170
Peter = $208
Thomas = $530
Lidia = $640


Harry= Harry ^ Peter ^ Thomas ^ Lidia
Peter = Harry ^ Peter ^ Thomas ^ Lidia
Thomas = Harry ^ Peter ^ Thomas ^ Lidia
Lidia = Harry ^ Peter ^ Thomas ^ Lidia
Harry = Harry ^ Peter ^ Thomas ^ Lidia


print("Harry =", Harry)
print("Peter =", Peter)
print("Thomas =", Thomas)
print("Lidia =", Lidia)

I have to perform bitwise XOR operation (which means operations work at the level of individual bits) to swap values between variables in Python. Then, update with the XOR of all variables, effectively swapping their values.

Harry= Harry ^ Peter ^ Thomas ^ Lidia
Peter = Harry ^ Peter ^ Thomas ^ Lidia
Thomas = Harry ^ Peter ^ Thomas ^ Lidia
Lidia = Harry ^ Peter ^ Thomas ^ Lidia
Harry = Harry ^ Peter ^ Thomas ^ Lidia

Output:

Harry = 640
Peter = 170
Thomas = 208
Lidia = 530

You can refer to the image below for the output after executing the Python program.

Swapping Four Variables Without Temporary Variable in Python using the XOR operator

Conclusion

I hope the information in the above Python post gives you a clearer picture of swapping four variables without a temporary variable in Python.

Moreover, I have provided a complete explanation with different examples to swap four variables without a temporary variable in Python, such as using comma assignment, arithmetic operator: addition and subtraction operator, and multiplication and division operator using the XOR operator.

You may also like to read: