Home / Programs / Python program to swap two variables - By using comma operator
Programming Example

Python program to swap two variables - By using comma operator

👁 493 Views
💻 Practical Program
📘 Step by Step Learning
We can use the comma operator. We do not need to use a third variable for swapping the values of two variables for this method.

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  
P, Q = Q, P  
   
print ("The Value of P after swapping: ", P)  
print ("The Value of Q after swapping: ", Q)  

Output

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

Explanation

No

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.