Python program to swap two variables - By using arithmetic operators - addition and Subtraction operator

Python Operators in Python (Article) Operators in Python (Program)

147

In this method, we can swap values of both the variable in two ways:

  • Using addition and Subtraction operator:

Program:

P = int( input("Please enter value for P: "))  
Q = int( input("Please enter value for Q: "))  
   
# To Swap the values of two variables using Addition and subtraction operator  
P = P + Q    
Q = P - Q   
P = P - Q  
   
print ("The Value of P after swapping: ", P)  
print ("The Value of Q after swapping: ", Q)  

Output:

Please enter value for P:  15
Please enter value for Q:  43
The Value of P after swapping: 43
The Value of Q after swapping: 15

Explanation:

No

This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.