Write a program to swap two numbers in python
Swapping By using the comma operator
Swapping By using Naive Approach
Swapping By using arithmetic operators
Swapping in Python By using the comma operator
T4tutorials_1 = int( input("Please enter a value for T4tutorials_1: "))
T4tutorials_2 = int( input("Please enter a value for T4tutorials_2: "))
# To Swap the values of two variables
T4tutorials_1, T4tutorials_2 = T4tutorials_2, T4tutorials_1
print ("The Value of T4tutorials_1 after swapping: ", T4tutorials_1)
print ("The Value of T4tutorials_2 after swapping: ", T4tutorials_2)
Output
Please enter a value for T4tutorials_1: 7
Please enter a value for T4tutorials_2: 9
The Value of T4tutorials_1 after swapping: 9
The Value of T4tutorials_2 after swapping: 7
Swapping in Python By using Naive Approach
T4tutorials_1 = int( input("Please enter a value for T4tutorials_1: "))
T4tutorials_2 = int( input("Please enter a value for T4tutorials_2: "))
# To swap the value of two variables
# we will user third variable which is a temporary variable
temp_1 = T4tutorials_1
T4tutorials_1 = T4tutorials_2
T4tutorials_2 = temp_1
print ("The Value of P after swapping: ", T4tutorials_1)
print ("The Value of T4tutorials_2 after swapping: ", T4tutorials_2)
Output
Please enter a value for T4tutorials_1: 2
Please enter a value for T4tutorials_2: 4
The Value of P after swapping: 4
The Value of T4tutorials_2 after swapping: 2
Swapping in Python By using arithmetic operators
T4tutorials_1 = int( input("Please enter a value for T4tutorials_1: "))
T4tutorials_2 = int( input("Please enter a value for T4tutorials_2: "))
# To Swap the values of two variables using Addition and subtraction operator
T4tutorials_1 = T4tutorials_1 + T4tutorials_2
T4tutorials_2 = T4tutorials_1 - T4tutorials_2
T4tutorials_1 = T4tutorials_1 - T4tutorials_2
print ("The Value of T4tutorials_1 after swapping: ", T4tutorials_1)
print ("The Value of T4tutorials_2 after swapping: ", T4tutorials_2)
Output
Please enter a value for T4tutorials_1: 9
Please enter a value for T4tutorials_2: 3
The Value of T4tutorials_1 after swapping: 3
The Value of T4tutorials_2 after swapping: 9
Swapping in Python By using XOR
T4tutorials_1 = int( input("Please enter a value for T4tutorials_1: "))
T4tutorials_2 = int( input("Please enter a value for T4tutorials_2: "))
# To Swap the values of two variables using XOR
T4tutorials_1 = T4tutorials_1 ^ T4tutorials_2
T4tutorials_2 = T4tutorials_1 ^ T4tutorials_2
T4tutorials_1 = T4tutorials_1 ^ T4tutorials_2
print ("The Value of T4tutorials_1 after swapping: ", T4tutorials_1)
print ("The Value of T4tutorials_2 after swapping: ", T4tutorials_2)
Output
Swapping in Python By using multiplication and division operator
T4tutorials_1 = int( input("Please enter a value for T4tutorials_1: "))
T4tutorials_2 = int( input("Please enter a value for T4tutorials_2: "))
# To Swap the values of two variables using Addition and subtraction operator
T4tutorials_1 = T4tutorials_1 * T4tutorials_2
T4tutorials_2 = T4tutorials_1 / T4tutorials_2
T4tutorials_1 = T4tutorials_1 / T4tutorials_2
print ("The Value of T4tutorials_1 after swapping: ", T4tutorials_1)
print ("The Value of T4tutorials_2 after swapping: ", T4tutorials_2)
Output
Please enter a value for T4tutorials_1: 55
Please enter a value for T4tutorials_2: 66
The Value of T4tutorials_1 after swapping: 66.0
The Value of T4tutorials_2 after swapping: 55.0