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 ofb
.b
will hold the original value ofa
.
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:
- Initially,
a = 5
andb = 45
. - The statement
a, b = b, a
swaps the values ofa
andb
. - After the swap,
a
becomes45
, andb
becomes5
.
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:
- Initially,
a = 7
andb = 14
. - A temporary variable
temp
is introduced to hold the value ofa
. - The value of
a
is replaced withb
, andb
is replaced withtemp
. - After the swap,
a
becomes14
, andb
becomes7
.
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:
a = a + b
: The value ofa
becomes the sum ofa
andb
.b = a - b
: Subtracting the originalb
from the sum gives the original value ofa
, which is now assigned tob
.a = a - b
: Subtracting the new value ofb
from the sum gives the original value ofb
, which is now assigned toa
.
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:
a = a ^ b
: Performs XOR ona
andb
, and stores the result ina
.b = a ^ b
: Performs XOR on the newa
andb
, restoring the original value ofa
intob
.a = a ^ b
: Performs XOR on the newa
andb
, restoring the original value ofb
intoa
.
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:
a, b, c = c, a, b
: Assigns the value ofc
toa
,a
tob
, andb
toc
in one step.
Output:
a: 3
b: 1
c: 2
YouTube Video
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.