TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python swap two Variables

Python swap two Variables for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python program to swap two variables

In this tutorial, we will learn how to swap two variables in a Python program. Suppose we have two variables, P and Q; We have to write a Python program for swapping their values. We will also discuss the different methods in Python for doing this task.

Method 1: By using Naïve Approach

In this method, the naïve approach will store the value of the P variable in a temporary variable, and then it will assign the variable P with the value of the Q variable. Then, it will assign the value of the temporary variable to the Q variable, which will result in swapping the values of both the variable.

Example:

P = int( input("Please enter value for P: "))
Q = int( input("Please enter value for Q: "))
 
# To swap the value of two variables
# we will user third variable which is a temporary variable
temp_1 = P
P = Q
Q = temp_1
 
print ("The Value of P after swapping: ", P)
print ("The Value of Q after swapping: ", Q)

Output:

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

Method 2: By using comma operator

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.

Example:

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

Method 3: By using XOR method

We can also use the bitwise XOR method for swapping two variables. The XOR of two variables, P and Q, will return the number which has all the bits as 1 whenever the bits of the P and Q variables differ.

Such as XOR of 4 (in binary 0100) and 6 (in binary 0110) is 1010.

XOR of 2 (in binary 0010) and 8 (in binary 1000) is 1010.

Example:

P = int( input("Please enter value for P: "))
Q = int( input("Please enter value for Q: "))
 
# To Swap the values of two variables using XOR
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:  12
Please enter value for Q:  10
The Value of P after swapping: 10
The Value of Q after swapping: 12

Method 4: By using arithmetic operators

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

  • Using addition and Subtraction operator:

Example:

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
  • Using multiplication and division operator

Example:

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:  23
Please enter value for Q:  14
The Value of P after swapping: 14.0
The Value of Q after swapping: 23.0

Conclusion

In this tutorial, we have discussed various methods to swap the value of two variables in the Python program.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf