Home / Programs / Python program to swap two variables - By using arithmetic operators - addition and Subtraction operator
🚀 Programming Example

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

👁 272 Views
💻 Practical Program
📘 Step Learning
Python program to swap two variables - By using arithmetic operators - addition and Subtraction operator

📌 Information & Algorithm

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

  • Using addition and Subtraction operator:

💻 Program Code

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)  
                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.