Python Program to swap two numbers without using third variable

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to swap two numbers
# without using another variable
  
  
x = 5
y = 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)

chevron_right


Output:



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) .

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to swap two numbers
# using Bitwise XOR method
  
  
x = 5  # x = 0101
y = 10 # y = 1010
  
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
  
# Swap code
x ^= y # x = 1111, y = 1010
y ^= x # y = 0101, x = 1111
x ^= y # x = 1010, y = 0101
  
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to swap two numbers
# using + and - operators
  
  
x = 5.4
y = 10.3
  
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
  
# Swap code
x = x + y # x = 15.7, y = 10.3
y = x - y # x = 15.7, y = 5.4
x = x - y # x = 10.3, y = 5.4
  
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)

chevron_right


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 .

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to swap two numbers
# using / and * operators
  
  
x = 5.4
y = 10.3
  
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
  
# Swap code
x = x * y # x = 55.62, y = 10.3 
y = x / y # x = 55.62, y = 5.4
x = x / y # x = 10.3, y = 5.4
  
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)

chevron_right


Output:

Before swapping: 
Value of x :  5.4  and y :  10.3
After swapping: 
Value of x :  10.3  and y :  5.4



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.