Python Program to Swap Two Variables

Swapping variables is a standard programming task that every programmer has faced and has been facing, Python provides multiple ways to swap two variables, each with its pros and cons.

Swapping two variables means exchanging their values so that the first variable holds the value of the second variable and vice versa which can be useful in many situations, such as sorting algorithms, numerical computations, and object-oriented programming.

Python Program to Swap Two Variables

In this blog post, we will explore three ways to swap two variables in Python and compare their performance and readability.

  • Using a Temporary Variable
  • Without Using a Temporary Variable
  • Using Arithmetic Operations

Swap Variables Using a Temporary Variable

This is the most commonly known way to swap two variables in Python, Where we will be using a temporary variable

Procedure:

  1. Assign the values of the two variables to a temporary variable
  2. Assign the value of the first variable to the second variable
  3. Assign the value of the temporary variable to the first variable

Example Code Snippet:

# Swapping variables using a temporary variable
x = "Learn"
y = "Share"

tmp = x
x = y
y = tmp

print("x =", x) 
print("y =", y) 

Output:

x = Share
y = Learn

In the above example, we assign the values of 5 and 10 to variables x and y, respectively. Then, we use a temporary variable tmp to store the value of x. Next, we assign the value of y to x, effectively swapping their values. Finally, we assign the value of temp to y, completing the swap.

Based on my knowledge, Below are the pros and cons of this approach

Pros:

  • Simple and easy to understand
  • Will work for any type of values
  • Can be scaled up to multiple variables

Cons:

  • Uses an extra variable, which may be undesirable in memory-constrained environments

Swap Variables Without Using a Temporary Variable

In the technique, We wanted to avoid using the extra variable to make sure, We are not using any extra memory.

Procedure:

  1. Assign the values of the two variables to a tuple, in the reverse order
  2. Unpack the tuple into the two variables

Example Code Snippet:

# Swapping variables without using a temporary variable
x = 5
y = 10

x, y = y, x

print("x =", x)
print("y =", y) 


Output:
x = 10
y = 5

In this above example, we assign the values of 5 and 10 to variables x and y, respectively. Then, we use tuple unpacking to assign the value of y to x and the value of x to y, effectively swapping their values.

Below are the pros and cons of this approach

Pros:

  • Will not use an extra variable
  • Can be scaled up to multiple variables
  • Will work for any type of values

Cons:

  • Will be less readable initially, Need some understanding before (But hope you got the information already in this blog :-))

Swap Variables Using Arithmetic Operations

In this technique, We are using an arithmetic operator to swap variables,

Procedure: (Using Addition and subtraction)

  1. Assign the sum of the two variables to the first variable
  2. Assign the difference between the first variable and the second variable to the second variable
  3. Assign the difference between the first variable and the second variable to the first variable

Example Code Snippet:

# Swapping variables using arithmetic operations
x = 5
y = 10

x = x + y
y = x - y
x = x - y

print("x =", x)

Output:

x = 10 
y = 5

In the above example, we first assign the values 5 and 10 to variables x and y, respectively. Then, we assign the sum of x and y to x. This step effectively stores the sum of the original values of x and y in x.

Next, we assign the difference between x and y to y. This step effectively stores the original value of x in y. Finally, we assign the difference between x and y to x. This step effectively stores the original value of y in x. At this point, the values of x and y have been swapped.

Below are the pros and cons of this approach

Pros:

  1. Will not use extra variables
  2. Works regardless of the values of the variables – This approach works for any values of the variables, including values that may cause issues with other methods.
  3. It will be faster in large data types. As we know arithmetic operators are faster than others

Cons:

  1. More complex than other approaches
  2. Values should be within the limit else we will see an Overflow error
  3. Values should be in numbers

Using Other operators

We can use other operators to swap variables, such as multiplication and division and it is similar to swapping variables using addition and subtraction

Swap variables using multiplication and division

Procedure:

  1. Assign the product of the two variables to the first variable
  2. Assign the quotient of the first variable and the second variable to the second variable
  3. Assign the quotient of the product and the second variable to the first variable

Example Code:

x = 5
y = 10

x = x * y
y = x / y
x = x / y

print("x =", x) 
print("y =", y)

Output:

x = 10.0
y = 5.0

Swap variables using XOR bitwise operator

Procedure:

  1. Assign the bitwise XOR of the two variables to the first variable
  2. Assign the bitwise XOR of the first variable and the second variable to the second variable
  3. Assign the bitwise XOR of the first variable and the second variable to the first variable

Example:

x = 5
y = 10

x = x ^ y
y = x ^ y
x = x ^ y

print("x =", x) 
print("y =", y)

Output:

x = 10

y = 5

Conclusion

In conclusion, swapping the values of two variables in Python can be achieved through several methods. The most common methods include using a temporary variable, unpacking variables, and using arithmetic operations. Each method has its pros and cons, and the best approach depends on the specific requirements of the program.

Good Luck with your Learning !!

Related Topics:

FIX: lib/zoneinfo_module.c:1:10: fatal error: ‘python.h’ file not found

How to Nest a Dictionary in a List?

How do I print an empty line in Python?

Coding Spell Python Journey | About Us 

Latest posts by Muhammad Usman Shafique (see all)