Javaexercise.com

Updated On 2021-06-11 15:34:14

Python Program To Swap Two Numbers

Swapping is a technique of exchanging values between two variables. We can use this in our programming to exchange values between two variables. In, this article, we will see some solutions to swap values by using, two variables, three variables, or bitwise operator, etc. Let's see some examples

Python Program to Swap Two Numbers

To swap values, we used a temporary variable to hold value and then simply exchanged the values.

# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))

print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)

# Swaping the Variables
temp = number1
number1 = number2
number2 = temp

print("After Swapping...")
print("Number1",number1)
print("Number2",number2)

Output:

Enter First Number 20
Enter Second Number 30
Before Swapping...
Number1 20
Number2 30
After Swapping...
Number1 30
Number2 20

 

Python Program to Swap Two Numbers Without Using Third Variable

Here, we are using a single assignment operator to swap values between variables. It is a feature of python that allows assigning multiple variables at the same time.

# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))

print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)

# Swaping the Variables without third variable
number1, number2 = number2, number1

print("After Swapping...")
print("Number1",number1)
print("Number2",number2)

Output:

Enter First Number 20
Enter Second Number 30
Before Swapping...
Number1 20
Number2 30
After Swapping...
Number1 30
Number2 20

 

Python Program to Swap Two Numbers Without Using Third Variable

Here, we are using plus and minus operators to swap values between two variables. It is a nice approach if you want to arithmetic operators.

# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))

print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)

# Swaping the Variables without third variable
number1 = number1 + number2
number2 = number1 - number2
number1 = number1 - number2

print("After Swapping...")
print("Number1",number1)
print("Number2",number2)

Output:

Enter First Number 20
Enter Second Number 30
Before Swapping...
Number1 20
Number2 30
After Swapping...
Number1 30
Number2 20

 

Python Program to Swap Two Numbers By Using XOR Operator

# Python Program to Swap Variable
number1 = int(input("Enter First Number"))
number2 = int(input("Enter Second Number"))

print("Before Swapping...")
print("Number1",number1)
print("Number2",number2)

# Swaping the Variables without third variable
number1 = number1 ^ number2
number2 = number1 ^ number2
number1 = number1 ^ number2

print("After Swapping...")
print("Number1",number1)
print("Number2",number2)

Output:

Enter First Number 20
Enter Second Number 30
Before Swapping...
Number1 20
Number2 30
After Swapping...
Number1 30
Number2 20