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, aAfter executing this line of code:
awill hold the original value ofb.bwill 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 = 5andb = 45. - The statement
a, b = b, aswaps the values ofaandb. - After the swap,
abecomes45, andbbecomes5.
Output:
a: 45
b: 5Example 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 = 7andb = 14. - A temporary variable
tempis introduced to hold the value ofa. - The value of
ais replaced withb, andbis replaced withtemp. - After the swap,
abecomes14, andbbecomes7.
Output:
a: 14
b: 7Example 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 ofabecomes the sum ofaandb.b = a - b: Subtracting the originalbfrom the sum gives the original value ofa, which is now assigned tob.a = a - b: Subtracting the new value ofbfrom the sum gives the original value ofb, which is now assigned toa.
Output:
a: 20
b: 10Example 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 onaandb, and stores the result ina.b = a ^ b: Performs XOR on the newaandb, restoring the original value ofaintob.a = a ^ b: Performs XOR on the newaandb, restoring the original value ofbintoa.
Output:
a: 15
b: 8Example 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 ofctoa,atob, andbtocin one step.
Output:
a: 3
b: 1
c: 2YouTube 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.