Python - Swap Two Numbers


Python - Swap Two Numbers

Swapping two numbers in Python can be done effortlessly using the assignment operator. Python’s tuple unpacking allows you to exchange the values of two variables in a single step.

a, b = b, a

After executing this line of code:

  • a will hold the original value of b.
  • b will hold the original value of a.

Let’s dive into an example.


Example 1: Basic Swapping

In this example, two numbers are swapped using Python’s tuple unpacking.

Python Program

# Initial values
a = 5
b = 45

# Swap values
a, b = b, a

# Print results
print('a:', a)
print('b:', b)

Explanation:

  1. Initially, a = 5 and b = 45.
  2. The statement a, b = b, a swaps the values of a and b.
  3. After the swap, a becomes 45, and b becomes 5.

Output:

a: 45
b: 5

Example 2: Swapping Using a Temporary Variable

Another common way to swap numbers is by using a temporary variable.

Python Program

# Initial values
a = 7
b = 14

# Swap values using a temporary variable
temp = a
a = b
b = temp

# Print results
print('a:', a)
print('b:', b)

Explanation:

  1. Initially, a = 7 and b = 14.
  2. A temporary variable temp is introduced to hold the value of a.
  3. The value of a is replaced with b, and b is replaced with temp.
  4. After the swap, a becomes 14, and b becomes 7.

Output:

a: 14
b: 7

Example 3: Swapping Without a Temporary Variable (Arithmetic Method)

Swapping can also be performed using arithmetic operations like addition and subtraction.

Python Program

# Initial values
a = 10
b = 20

# Swap values without a temporary variable
a = a + b
b = a - b
a = a - b

# Print results
print('a:', a)
print('b:', b)

Explanation:

  1. a = a + b: The value of a becomes the sum of a and b.
  2. b = a - b: Subtracting the original b from the sum gives the original value of a, which is now assigned to b.
  3. a = a - b: Subtracting the new value of b from the sum gives the original value of b, which is now assigned to a.

Output:

a: 20
b: 10

Example 4: Swapping Using Bitwise XOR

Swapping can also be achieved using bitwise XOR, which is both space-efficient and avoids the need for a temporary variable.

Python Program

# Initial values
a = 8
b = 15

# Swap values using XOR
a = a ^ b
b = a ^ b
a = a ^ b

# Print results
print('a:', a)
print('b:', b)

Explanation:

  1. a = a ^ b: Performs XOR on a and b, and stores the result in a.
  2. b = a ^ b: Performs XOR on the new a and b, restoring the original value of a into b.
  3. a = a ^ b: Performs XOR on the new a and b, restoring the original value of b into a.

Output:

a: 15
b: 8

Example 5: Swapping Multiple Variables

Python allows you to swap multiple variables simultaneously.

Python Program

# Initial values
a, b, c = 1, 2, 3

# Swap values
a, b, c = c, a, b

# Print results
print('a:', a)
print('b:', b)
print('c:', c)

Explanation:

  1. a, b, c = c, a, b: Assigns the value of c to a, a to b, and b to c in one step.

Output:

a: 3
b: 1
c: 2

YouTube Video

https://youtu.be/lktpH-G6ZRw?si=biBJzzpJW94NNXWq

Summary

In this tutorial, we explored multiple ways to swap two numbers in Python:

  • Using tuple unpacking (most efficient).
  • Using a temporary variable.
  • Using arithmetic operations.
  • Using bitwise XOR.
  • Swapping multiple variables simultaneously.

Choose the method that best suits your use case for readability and performance.


Python Libraries