Python Program to swap two numbers without using third variable

16 Nov, 2020
Given two variables n1 and n2. The task is to swap the values of both the variables without using third variable.Examples:  
X : 10
Y : 20
 
After swapping X and Y, we get :

X : 20
Y : 10



 
 A :  'Hello'
 B :  'World'
  
After swapping A and B, we get : 

 A :  'World'
 B :  'Hello'



Method 1 :- Using simple built-in method  
left , right = right , left 



This method works for any data type values like string, int, float and is easy to use.  
Python
# Python code to swap two numbers# without using another variable  x = 5y = 7 print ("Before swapping: ")print("Value of x : ", x, " and y : ", y) # code to swap 'x' and 'y'x, y = y, x print ("After swapping: ")print("Value of x : ", x, " and y : ", y)
Output:  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Before swapping: 
Value of x :  5  and y :  7
After swapping: 
Value of x :  7  and y :  5    



Method 2 :- Using Bitwise XOR operator  
x ^= y
y ^= x
x ^= y



This method only works for integers and works faster because this method uses bit operation (for same values, output = 0 and for different values, output = 1) . 
Python
# Python code to swap two numbers# using Bitwise XOR method  x = 5  # x = 0101y = 10 # y = 1010 print ("Before swapping: ")print("Value of x : ", x, " and y : ", y) # Swap codex ^= y # x = 1111, y = 1010y ^= x # y = 0101, x = 1111x ^= y # x = 1010, y = 0101 print ("After swapping: ")print("Value of x : ", x, " and y : ", y)
Output:  
Before swapping: 
Value of x :  5  and y :  10
After swapping: 
Value of x :  10  and y :  5



Method 3 :- Using Addition and Subtraction Operators  
x = x + y 
y = x - y
x = x - y



This method works for variables that have numeric values. 
Python
# Python code to swap two numbers# using + and - operators  x = 5.4y = 10.3 print ("Before swapping: ")print("Value of x : ", x, " and y : ", y) # Swap codex = x + y # x = 15.7, y = 10.3y = x - y # x = 15.7, y = 5.4x = x - y # x = 10.3, y = 5.4 print ("After swapping: ")print("Value of x : ", x, " and y : ", y)
Output:  
Before swapping: 
Value of x :  5.4  and y :  10.3
After swapping: 
Value of x :  10.3  and y :  5.4



Method 4 :- Using Division and Multiplication Operators  
x = x * y
y = x / y
x = x / y



This method works for variables that have numeric values other than 0 . 
Python
# Python code to swap two numbers# using / and * operators  x = 5.4y = 10.3 print ("Before swapping: ")print("Value of x : ", x, " and y : ", y) # Swap codex = x * y # x = 55.62, y = 10.3y = x / y # x = 55.62, y = 5.4x = x / y # x = 10.3, y = 5.4 print ("After swapping: ")print("Value of x : ", x, " and y : ", y)
Output:  
Before swapping: 
Value of x :  5.4  and y :  10.3
After swapping: 
Value of x :  10.3  and y :  5.4



Method 4: Using both bitwise operators and arithmetic operators: 
Python3
# python program to swap two numbers# using bitwise addition for swapping     x = 5;y = 10;   print ("Before swapping: ") ;print("Value of x : ", x, " and y : ", y) ;   # same as x = x + yx = (x & y) + (x|y) ;   #vsame as y = x - yy = x + (~y) + 1 ;   # same as x = x - yx = x + (~y) + 1 ;   print ("After swapping: ")print("Value of x : ", x, " and y : ", y) # This code is contributed by bunnyram19
Output:
Before swapping: 
Value of x :  5  and y :  10
After swapping: 
Value of x :  10  and y :  5

 
bunnyram19
python-basics
python-utility
Swap-Program
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Introduction To PYTHON
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Create a directory in Python